2024-07-23 17:57:21 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-08-09 12:38:18 +00:00
|
|
|
"grain/config"
|
2024-07-31 18:12:33 +00:00
|
|
|
"grain/server/db"
|
|
|
|
"grain/server/handlers/response"
|
|
|
|
relay "grain/server/types"
|
|
|
|
"grain/server/utils"
|
2024-08-09 12:38:18 +00:00
|
|
|
"sync"
|
2024-07-23 17:57:21 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
var subscriptions = make(map[string]relay.Subscription)
|
2024-08-09 12:38:18 +00:00
|
|
|
var mu sync.Mutex // Protect concurrent access to subscriptions map
|
2024-07-23 17:57:21 +00:00
|
|
|
|
2024-08-01 00:09:47 +00:00
|
|
|
func HandleReq(ws *websocket.Conn, message []interface{}, subscriptions map[string][]relay.Filter) {
|
2024-09-05 15:01:24 +00:00
|
|
|
config.LimitedGoRoutine(func() {
|
2024-08-19 17:47:23 +00:00
|
|
|
if len(message) < 3 {
|
|
|
|
fmt.Println("Invalid REQ message format")
|
|
|
|
response.SendClosed(ws, "", "invalid: invalid REQ message format")
|
|
|
|
return
|
2024-08-09 12:38:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 17:47:23 +00:00
|
|
|
subID, ok := message[1].(string)
|
2024-07-23 17:57:21 +00:00
|
|
|
if !ok {
|
2024-08-19 17:47:23 +00:00
|
|
|
fmt.Println("Invalid subscription ID format")
|
|
|
|
response.SendClosed(ws, "", "invalid: invalid subscription ID format")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
// Check the current number of subscriptions for the client
|
|
|
|
if len(subscriptions) >= config.GetConfig().Server.MaxSubscriptionsPerClient {
|
|
|
|
// Find and remove the oldest subscription (FIFO)
|
|
|
|
var oldestSubID string
|
|
|
|
for id := range subscriptions {
|
|
|
|
oldestSubID = id
|
|
|
|
break
|
|
|
|
}
|
|
|
|
delete(subscriptions, oldestSubID)
|
|
|
|
fmt.Println("Dropped oldest subscription:", oldestSubID)
|
|
|
|
}
|
|
|
|
|
2024-08-22 20:12:12 +00:00
|
|
|
// Prepare filters based on the incoming message
|
2024-08-19 17:47:23 +00:00
|
|
|
filters := make([]relay.Filter, len(message)-2)
|
|
|
|
for i, filter := range message[2:] {
|
|
|
|
filterData, ok := filter.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
fmt.Println("Invalid filter format")
|
|
|
|
response.SendClosed(ws, subID, "invalid: invalid filter format")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f relay.Filter
|
|
|
|
f.IDs = utils.ToStringArray(filterData["ids"])
|
|
|
|
f.Authors = utils.ToStringArray(filterData["authors"])
|
|
|
|
f.Kinds = utils.ToIntArray(filterData["kinds"])
|
|
|
|
f.Tags = utils.ToTagsMap(filterData["tags"])
|
|
|
|
f.Since = utils.ToTime(filterData["since"])
|
|
|
|
f.Until = utils.ToTime(filterData["until"])
|
|
|
|
f.Limit = utils.ToInt(filterData["limit"])
|
|
|
|
|
|
|
|
filters[i] = f
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new subscription or update the existing one
|
|
|
|
subscriptions[subID] = filters
|
2024-08-22 20:12:12 +00:00
|
|
|
fmt.Printf("Subscription updated: %s with %d filters\n", subID, len(filters))
|
2024-08-19 17:47:23 +00:00
|
|
|
|
|
|
|
// Query the database with filters and send back the results
|
2024-09-02 15:56:52 +00:00
|
|
|
queriedEvents, err := db.QueryEvents(filters, db.GetClient(), "grain")
|
2024-08-19 17:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error querying events:", err)
|
|
|
|
response.SendClosed(ws, subID, "error: could not query events")
|
2024-07-23 17:57:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-22 20:12:12 +00:00
|
|
|
// Log the number of events retrieved
|
|
|
|
fmt.Printf("Retrieved %d events for subscription %s\n", len(queriedEvents), subID)
|
|
|
|
if len(queriedEvents) == 0 {
|
|
|
|
fmt.Printf("No matching events found for subscription %s\n", subID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send each event back to the client
|
2024-08-19 17:47:23 +00:00
|
|
|
for _, evt := range queriedEvents {
|
|
|
|
msg := []interface{}{"EVENT", subID, evt}
|
|
|
|
msgBytes, _ := json.Marshal(msg)
|
|
|
|
err = websocket.Message.Send(ws, string(msgBytes))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error sending event:", err)
|
|
|
|
response.SendClosed(ws, subID, "error: could not send event")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Indicate end of stored events
|
|
|
|
eoseMsg := []interface{}{"EOSE", subID}
|
|
|
|
eoseBytes, _ := json.Marshal(eoseMsg)
|
|
|
|
err = websocket.Message.Send(ws, string(eoseBytes))
|
2024-07-23 17:57:21 +00:00
|
|
|
if err != nil {
|
2024-08-19 17:47:23 +00:00
|
|
|
fmt.Println("Error sending EOSE:", err)
|
|
|
|
response.SendClosed(ws, subID, "error: could not send EOSE")
|
2024-07-23 17:57:21 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-22 20:12:12 +00:00
|
|
|
|
|
|
|
fmt.Println("Subscription handling completed, keeping connection open.")
|
2024-08-19 17:47:23 +00:00
|
|
|
})
|
2024-07-23 17:57:21 +00:00
|
|
|
}
|