Compare commits

..

2 Commits

Author SHA1 Message Date
7009533c8d handle requests in a que 2024-09-15 20:06:22 -04:00
3526d2937e undo limited routine for event handler 2024-09-15 18:53:28 -04:00
2 changed files with 171 additions and 134 deletions

View File

@ -16,7 +16,7 @@ import (
)
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")
@ -64,7 +64,7 @@ func HandleEvent(ws *websocket.Conn, message []interface{}) {
db.StoreMongoEvent(context.TODO(), evt, ws)
fmt.Println("Event processed:", evt.ID)
})
}
func handleBlacklistAndWhitelist(ws *websocket.Conn, evt nostr.Event) bool {

View File

@ -16,8 +16,41 @@ import (
var subscriptions = make(map[string]relay.Subscription)
var mu sync.Mutex // Protect concurrent access to subscriptions map
// RequestQueue holds incoming requests
var RequestQueue = make(chan Request, 1000) // Adjust buffer size as needed
type Request struct {
Ws *websocket.Conn
Message []interface{}
}
// StartWorkerPool initializes a pool of worker goroutines
func StartWorkerPool(numWorkers int) {
for i := 0; i < numWorkers; i++ {
go worker()
}
}
// worker processes requests from the RequestQueue
func worker() {
for req := range RequestQueue {
processRequest(req.Ws, req.Message)
}
}
// HandleReq now just adds the request to the queue
func HandleReq(ws *websocket.Conn, message []interface{}, subscriptions map[string][]relay.Filter) {
config.LimitedGoRoutine(func() {
select {
case RequestQueue <- Request{Ws: ws, Message: message}:
// Request added to queue
default:
// Queue is full, log the dropped request
fmt.Println("Warning: Request queue is full, dropping request")
}
}
// processRequest handles the actual processing of each request
func processRequest(ws *websocket.Conn, message []interface{}) {
if len(message) < 3 {
fmt.Println("Invalid REQ message format")
response.SendClosed(ws, "", "invalid: invalid REQ message format")
@ -69,7 +102,7 @@ func HandleReq(ws *websocket.Conn, message []interface{}, subscriptions map[stri
}
// Add the new subscription or update the existing one
subscriptions[subID] = filters
subscriptions[subID] = relay.Subscription{Filters: filters}
fmt.Printf("Subscription updated: %s with %d filters\n", subID, len(filters))
// Query the database with filters and send back the results
@ -109,5 +142,9 @@ func HandleReq(ws *websocket.Conn, message []interface{}, subscriptions map[stri
}
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
}