mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-23 09:07:12 +00:00
Compare commits
2 Commits
462fa0a4b1
...
7009533c8d
Author | SHA1 | Date | |
---|---|---|---|
7009533c8d | |||
3526d2937e |
@ -16,55 +16,55 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func HandleEvent(ws *websocket.Conn, message []interface{}) {
|
func HandleEvent(ws *websocket.Conn, message []interface{}) {
|
||||||
config.LimitedGoRoutine(func() {
|
|
||||||
if len(message) != 2 {
|
|
||||||
fmt.Println("Invalid EVENT message format")
|
|
||||||
response.SendNotice(ws, "", "Invalid EVENT message format")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
eventData, ok := message[1].(map[string]interface{})
|
if len(message) != 2 {
|
||||||
if !ok {
|
fmt.Println("Invalid EVENT message format")
|
||||||
fmt.Println("Invalid event data format")
|
response.SendNotice(ws, "", "Invalid EVENT message format")
|
||||||
response.SendNotice(ws, "", "Invalid event data format")
|
return
|
||||||
return
|
}
|
||||||
}
|
|
||||||
eventBytes, err := json.Marshal(eventData)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error marshaling event data:", err)
|
|
||||||
response.SendNotice(ws, "", "Error marshaling event data")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var evt nostr.Event
|
eventData, ok := message[1].(map[string]interface{})
|
||||||
err = json.Unmarshal(eventBytes, &evt)
|
if !ok {
|
||||||
if err != nil {
|
fmt.Println("Invalid event data format")
|
||||||
fmt.Println("Error unmarshaling event data:", err)
|
response.SendNotice(ws, "", "Invalid event data format")
|
||||||
response.SendNotice(ws, "", "Error unmarshaling event data")
|
return
|
||||||
return
|
}
|
||||||
}
|
eventBytes, err := json.Marshal(eventData)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error marshaling event data:", err)
|
||||||
|
response.SendNotice(ws, "", "Error marshaling event data")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Signature check moved here
|
var evt nostr.Event
|
||||||
if !utils.CheckSignature(evt) {
|
err = json.Unmarshal(eventBytes, &evt)
|
||||||
response.SendOK(ws, evt.ID, false, "invalid: signature verification failed")
|
if err != nil {
|
||||||
return
|
fmt.Println("Error unmarshaling event data:", err)
|
||||||
}
|
response.SendNotice(ws, "", "Error unmarshaling event data")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
eventSize := len(eventBytes) // Calculate event size
|
// Signature check moved here
|
||||||
|
if !utils.CheckSignature(evt) {
|
||||||
|
response.SendOK(ws, evt.ID, false, "invalid: signature verification failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if !handleBlacklistAndWhitelist(ws, evt) {
|
eventSize := len(eventBytes) // Calculate event size
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !handleRateAndSizeLimits(ws, evt, eventSize) {
|
if !handleBlacklistAndWhitelist(ws, evt) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is where I'll handle storage for multiple database types in the future
|
if !handleRateAndSizeLimits(ws, evt, eventSize) {
|
||||||
db.StoreMongoEvent(context.TODO(), evt, ws)
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is where I'll handle storage for multiple database types in the future
|
||||||
|
db.StoreMongoEvent(context.TODO(), evt, ws)
|
||||||
|
|
||||||
|
fmt.Println("Event processed:", evt.ID)
|
||||||
|
|
||||||
fmt.Println("Event processed:", evt.ID)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleBlacklistAndWhitelist(ws *websocket.Conn, evt nostr.Event) bool {
|
func handleBlacklistAndWhitelist(ws *websocket.Conn, evt nostr.Event) bool {
|
||||||
|
@ -16,98 +16,135 @@ import (
|
|||||||
var subscriptions = make(map[string]relay.Subscription)
|
var subscriptions = make(map[string]relay.Subscription)
|
||||||
var mu sync.Mutex // Protect concurrent access to subscriptions map
|
var mu sync.Mutex // Protect concurrent access to subscriptions map
|
||||||
|
|
||||||
func HandleReq(ws *websocket.Conn, message []interface{}, subscriptions map[string][]relay.Filter) {
|
// RequestQueue holds incoming requests
|
||||||
config.LimitedGoRoutine(func() {
|
var RequestQueue = make(chan Request, 1000) // Adjust buffer size as needed
|
||||||
if len(message) < 3 {
|
|
||||||
fmt.Println("Invalid REQ message format")
|
|
||||||
response.SendClosed(ws, "", "invalid: invalid REQ message format")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
subID, ok := message[1].(string)
|
type Request struct {
|
||||||
if !ok {
|
Ws *websocket.Conn
|
||||||
fmt.Println("Invalid subscription ID format")
|
Message []interface{}
|
||||||
response.SendClosed(ws, "", "invalid: invalid subscription ID format")
|
}
|
||||||
return
|
|
||||||
}
|
// StartWorkerPool initializes a pool of worker goroutines
|
||||||
|
func StartWorkerPool(numWorkers int) {
|
||||||
mu.Lock()
|
for i := 0; i < numWorkers; i++ {
|
||||||
defer mu.Unlock()
|
go worker()
|
||||||
|
}
|
||||||
// Check the current number of subscriptions for the client
|
}
|
||||||
if len(subscriptions) >= config.GetConfig().Server.MaxSubscriptionsPerClient {
|
|
||||||
// Find and remove the oldest subscription (FIFO)
|
// worker processes requests from the RequestQueue
|
||||||
var oldestSubID string
|
func worker() {
|
||||||
for id := range subscriptions {
|
for req := range RequestQueue {
|
||||||
oldestSubID = id
|
processRequest(req.Ws, req.Message)
|
||||||
break
|
}
|
||||||
}
|
}
|
||||||
delete(subscriptions, oldestSubID)
|
|
||||||
fmt.Println("Dropped oldest subscription:", oldestSubID)
|
// HandleReq now just adds the request to the queue
|
||||||
}
|
func HandleReq(ws *websocket.Conn, message []interface{}, subscriptions map[string][]relay.Filter) {
|
||||||
|
select {
|
||||||
// Prepare filters based on the incoming message
|
case RequestQueue <- Request{Ws: ws, Message: message}:
|
||||||
filters := make([]relay.Filter, len(message)-2)
|
// Request added to queue
|
||||||
for i, filter := range message[2:] {
|
default:
|
||||||
filterData, ok := filter.(map[string]interface{})
|
// Queue is full, log the dropped request
|
||||||
if !ok {
|
fmt.Println("Warning: Request queue is full, dropping request")
|
||||||
fmt.Println("Invalid filter format")
|
}
|
||||||
response.SendClosed(ws, subID, "invalid: invalid filter format")
|
}
|
||||||
return
|
|
||||||
}
|
// processRequest handles the actual processing of each request
|
||||||
|
func processRequest(ws *websocket.Conn, message []interface{}) {
|
||||||
var f relay.Filter
|
if len(message) < 3 {
|
||||||
f.IDs = utils.ToStringArray(filterData["ids"])
|
fmt.Println("Invalid REQ message format")
|
||||||
f.Authors = utils.ToStringArray(filterData["authors"])
|
response.SendClosed(ws, "", "invalid: invalid REQ message format")
|
||||||
f.Kinds = utils.ToIntArray(filterData["kinds"])
|
return
|
||||||
f.Tags = utils.ToTagsMap(filterData["tags"])
|
}
|
||||||
f.Since = utils.ToTime(filterData["since"])
|
|
||||||
f.Until = utils.ToTime(filterData["until"])
|
subID, ok := message[1].(string)
|
||||||
f.Limit = utils.ToInt(filterData["limit"])
|
if !ok {
|
||||||
|
fmt.Println("Invalid subscription ID format")
|
||||||
filters[i] = f
|
response.SendClosed(ws, "", "invalid: invalid subscription ID format")
|
||||||
}
|
return
|
||||||
|
}
|
||||||
// Add the new subscription or update the existing one
|
|
||||||
subscriptions[subID] = filters
|
mu.Lock()
|
||||||
fmt.Printf("Subscription updated: %s with %d filters\n", subID, len(filters))
|
defer mu.Unlock()
|
||||||
|
|
||||||
// Query the database with filters and send back the results
|
// Check the current number of subscriptions for the client
|
||||||
queriedEvents, err := db.QueryEvents(filters, db.GetClient(), "grain")
|
if len(subscriptions) >= config.GetConfig().Server.MaxSubscriptionsPerClient {
|
||||||
if err != nil {
|
// Find and remove the oldest subscription (FIFO)
|
||||||
fmt.Println("Error querying events:", err)
|
var oldestSubID string
|
||||||
response.SendClosed(ws, subID, "error: could not query events")
|
for id := range subscriptions {
|
||||||
return
|
oldestSubID = id
|
||||||
}
|
break
|
||||||
|
}
|
||||||
// Log the number of events retrieved
|
delete(subscriptions, oldestSubID)
|
||||||
fmt.Printf("Retrieved %d events for subscription %s\n", len(queriedEvents), subID)
|
fmt.Println("Dropped oldest subscription:", oldestSubID)
|
||||||
if len(queriedEvents) == 0 {
|
}
|
||||||
fmt.Printf("No matching events found for subscription %s\n", subID)
|
|
||||||
}
|
// Prepare filters based on the incoming message
|
||||||
|
filters := make([]relay.Filter, len(message)-2)
|
||||||
// Send each event back to the client
|
for i, filter := range message[2:] {
|
||||||
for _, evt := range queriedEvents {
|
filterData, ok := filter.(map[string]interface{})
|
||||||
msg := []interface{}{"EVENT", subID, evt}
|
if !ok {
|
||||||
msgBytes, _ := json.Marshal(msg)
|
fmt.Println("Invalid filter format")
|
||||||
err = websocket.Message.Send(ws, string(msgBytes))
|
response.SendClosed(ws, subID, "invalid: invalid filter format")
|
||||||
if err != nil {
|
return
|
||||||
fmt.Println("Error sending event:", err)
|
}
|
||||||
response.SendClosed(ws, subID, "error: could not send event")
|
|
||||||
return
|
var f relay.Filter
|
||||||
}
|
f.IDs = utils.ToStringArray(filterData["ids"])
|
||||||
}
|
f.Authors = utils.ToStringArray(filterData["authors"])
|
||||||
|
f.Kinds = utils.ToIntArray(filterData["kinds"])
|
||||||
// Indicate end of stored events
|
f.Tags = utils.ToTagsMap(filterData["tags"])
|
||||||
eoseMsg := []interface{}{"EOSE", subID}
|
f.Since = utils.ToTime(filterData["since"])
|
||||||
eoseBytes, _ := json.Marshal(eoseMsg)
|
f.Until = utils.ToTime(filterData["until"])
|
||||||
err = websocket.Message.Send(ws, string(eoseBytes))
|
f.Limit = utils.ToInt(filterData["limit"])
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error sending EOSE:", err)
|
filters[i] = f
|
||||||
response.SendClosed(ws, subID, "error: could not send EOSE")
|
}
|
||||||
return
|
|
||||||
}
|
// Add the new subscription or update the existing one
|
||||||
|
subscriptions[subID] = relay.Subscription{Filters: filters}
|
||||||
fmt.Println("Subscription handling completed, keeping connection open.")
|
fmt.Printf("Subscription updated: %s with %d filters\n", subID, len(filters))
|
||||||
})
|
|
||||||
|
// Query the database with filters and send back the results
|
||||||
|
queriedEvents, err := db.QueryEvents(filters, db.GetClient(), "grain")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error querying events:", err)
|
||||||
|
response.SendClosed(ws, subID, "error: could not query events")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
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))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error sending EOSE:", err)
|
||||||
|
response.SendClosed(ws, subID, "error: could not send EOSE")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Subscription handling completed, keeping connection open.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize the worker pool when your server starts
|
||||||
|
func init() {
|
||||||
|
StartWorkerPool(10) // Adjust the number of workers as needed
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user