mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-10-29 17:16:31 +00:00
handle requests in a que
This commit is contained in:
parent
3526d2937e
commit
7009533c8d
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user