imporve requests to the relay

This commit is contained in:
0ceanSlim 2024-09-17 08:44:18 -04:00
parent 7009533c8d
commit bfa3e6b08e
4 changed files with 179 additions and 126 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@
config.yml config.yml
relay_metadata.json relay_metadata.json
grain.exe grain.exe
/build /build
/logs

View File

@ -13,11 +13,12 @@ import (
// QueryEvents queries events from the MongoDB collection(s) based on filters // QueryEvents queries events from the MongoDB collection(s) based on filters
func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName string) ([]relay.Event, error) { func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName string) ([]relay.Event, error) {
var results []relay.Event var results []relay.Event
var combinedFilters []bson.M
// Build MongoDB filters for each relay.Filter
for _, filter := range filters { for _, filter := range filters {
filterBson := bson.M{} filterBson := bson.M{}
// Construct the BSON query based on the filters
if len(filter.IDs) > 0 { if len(filter.IDs) > 0 {
filterBson["id"] = bson.M{"$in": filter.IDs} filterBson["id"] = bson.M{"$in": filter.IDs}
} }
@ -45,62 +46,70 @@ func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName stri
} }
} }
opts := options.Find().SetSort(bson.D{{Key: "created_at", Value: -1}}) combinedFilters = append(combinedFilters, filterBson)
}
// Combine all filter conditions using the $or operator
query := bson.M{}
if len(combinedFilters) > 0 {
query["$or"] = combinedFilters
}
opts := options.Find().SetSort(bson.D{{Key: "created_at", Value: -1}})
// Apply limit if set for initial query
for _, filter := range filters {
if filter.Limit != nil { if filter.Limit != nil {
opts.SetLimit(int64(*filter.Limit)) opts.SetLimit(int64(*filter.Limit))
} }
}
// If no specific kinds are specified, query all collections in the database // If no specific kinds are specified, query all collections
if len(filter.Kinds) == 0 { if len(filters[0].Kinds) == 0 {
collections, err := client.Database(databaseName).ListCollectionNames(context.TODO(), bson.D{}) collections, err := client.Database(databaseName).ListCollectionNames(context.TODO(), bson.D{})
if err != nil {
return nil, fmt.Errorf("error listing collections: %v", err)
}
for _, collectionName := range collections {
collection := client.Database(databaseName).Collection(collectionName)
cursor, err := collection.Find(context.TODO(), query, opts)
if err != nil { if err != nil {
return nil, fmt.Errorf("error listing collections: %v", err) return nil, fmt.Errorf("error querying collection %s: %v", collectionName, err)
} }
defer cursor.Close(context.TODO())
for _, collectionName := range collections { for cursor.Next(context.TODO()) {
fmt.Printf("Querying collection: %s with query: %v\n", collectionName, filterBson) var event relay.Event
if err := cursor.Decode(&event); err != nil {
collection := client.Database(databaseName).Collection(collectionName) return nil, fmt.Errorf("error decoding event from collection %s: %v", collectionName, err)
cursor, err := collection.Find(context.TODO(), filterBson, opts)
if err != nil {
return nil, fmt.Errorf("error querying collection %s: %v", collectionName, err)
}
defer cursor.Close(context.TODO())
for cursor.Next(context.TODO()) {
var event relay.Event
if err := cursor.Decode(&event); err != nil {
return nil, fmt.Errorf("error decoding event from collection %s: %v", collectionName, err)
}
results = append(results, event)
}
if err := cursor.Err(); err != nil {
return nil, fmt.Errorf("cursor error in collection %s: %v", collectionName, err)
} }
results = append(results, event)
} }
} else { if err := cursor.Err(); err != nil {
// Query specific collections based on kinds return nil, fmt.Errorf("cursor error in collection %s: %v", collectionName, err)
for _, kind := range filter.Kinds { }
collectionName := fmt.Sprintf("event-kind%d", kind) }
fmt.Printf("Querying collection: %s with query: %v\n", collectionName, filterBson) } else {
// Query specific collections based on kinds
for _, kind := range filters[0].Kinds {
collectionName := fmt.Sprintf("event-kind%d", kind)
collection := client.Database(databaseName).Collection(collectionName)
cursor, err := collection.Find(context.TODO(), query, opts)
if err != nil {
return nil, fmt.Errorf("error querying collection %s: %v", collectionName, err)
}
defer cursor.Close(context.TODO())
collection := client.Database(databaseName).Collection(collectionName) for cursor.Next(context.TODO()) {
cursor, err := collection.Find(context.TODO(), filterBson, opts) var event relay.Event
if err != nil { if err := cursor.Decode(&event); err != nil {
return nil, fmt.Errorf("error querying collection %s: %v", collectionName, err) return nil, fmt.Errorf("error decoding event from collection %s: %v", collectionName, err)
}
defer cursor.Close(context.TODO())
for cursor.Next(context.TODO()) {
var event relay.Event
if err := cursor.Decode(&event); err != nil {
return nil, fmt.Errorf("error decoding event from collection %s: %v", collectionName, err)
}
results = append(results, event)
}
if err := cursor.Err(); err != nil {
return nil, fmt.Errorf("cursor error in collection %s: %v", collectionName, err)
} }
results = append(results, event)
}
if err := cursor.Err(); err != nil {
return nil, fmt.Errorf("cursor error in collection %s: %v", collectionName, err)
} }
} }
} }

View File

@ -49,99 +49,104 @@ func HandleReq(ws *websocket.Conn, message []interface{}, subscriptions map[stri
} }
} }
// processRequest handles the actual processing of each request // processRequest handles the actual processing of each request
func processRequest(ws *websocket.Conn, message []interface{}) { func processRequest(ws *websocket.Conn, message []interface{}) {
if len(message) < 3 { if len(message) < 3 {
fmt.Println("Invalid REQ message format") fmt.Println("Invalid REQ message format")
response.SendClosed(ws, "", "invalid: invalid REQ message format") response.SendClosed(ws, "", "invalid: invalid REQ message format")
return return
} }
subID, ok := message[1].(string) subID, ok := message[1].(string)
if !ok { if !ok || len(subID) == 0 || len(subID) > 64 {
fmt.Println("Invalid subscription ID format") fmt.Println("Invalid subscription ID format or length")
response.SendClosed(ws, "", "invalid: invalid subscription ID format") response.SendClosed(ws, "", "invalid: subscription ID must be between 1 and 64 characters long")
return return
} }
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
// Check the current number of subscriptions for the client // Remove oldest subscription if needed
if len(subscriptions) >= config.GetConfig().Server.MaxSubscriptionsPerClient { if len(subscriptions) >= config.GetConfig().Server.MaxSubscriptionsPerClient {
// Find and remove the oldest subscription (FIFO) var oldestSubID string
var oldestSubID string for id := range subscriptions {
for id := range subscriptions { oldestSubID = id
oldestSubID = id break
break }
} delete(subscriptions, oldestSubID)
delete(subscriptions, oldestSubID) fmt.Println("Dropped oldest subscription:", oldestSubID)
fmt.Println("Dropped oldest subscription:", oldestSubID) }
}
// Prepare filters based on the incoming message // Parse and validate filters
filters := make([]relay.Filter, len(message)-2) filters := make([]relay.Filter, len(message)-2)
for i, filter := range message[2:] { for i, filter := range message[2:] {
filterData, ok := filter.(map[string]interface{}) filterData, ok := filter.(map[string]interface{})
if !ok { if !ok {
fmt.Println("Invalid filter format") fmt.Println("Invalid filter format")
response.SendClosed(ws, subID, "invalid: invalid filter format") response.SendClosed(ws, subID, "invalid: invalid filter format")
return return
} }
var f relay.Filter var f relay.Filter
f.IDs = utils.ToStringArray(filterData["ids"]) f.IDs = utils.ToStringArray(filterData["ids"])
f.Authors = utils.ToStringArray(filterData["authors"]) f.Authors = utils.ToStringArray(filterData["authors"])
f.Kinds = utils.ToIntArray(filterData["kinds"]) f.Kinds = utils.ToIntArray(filterData["kinds"])
f.Tags = utils.ToTagsMap(filterData["tags"]) f.Tags = utils.ToTagsMap(filterData["tags"])
f.Since = utils.ToTime(filterData["since"]) f.Since = utils.ToTime(filterData["since"])
f.Until = utils.ToTime(filterData["until"]) f.Until = utils.ToTime(filterData["until"])
f.Limit = utils.ToInt(filterData["limit"]) f.Limit = utils.ToInt(filterData["limit"])
filters[i] = f filters[i] = f
} }
// Add the new subscription or update the existing one // Validate filters
subscriptions[subID] = relay.Subscription{Filters: filters} if !utils.ValidateFilters(filters) {
fmt.Printf("Subscription updated: %s with %d filters\n", subID, len(filters)) fmt.Println("Invalid filters: hex values not valid")
response.SendClosed(ws, subID, "invalid: filters contain invalid hex values")
return
}
// Query the database with filters and send back the results // Add subscription
queriedEvents, err := db.QueryEvents(filters, db.GetClient(), "grain") subscriptions[subID] = relay.Subscription{Filters: filters}
if err != nil { fmt.Printf("Subscription updated: %s with %d filters\n", subID, len(filters))
fmt.Println("Error querying events:", err)
response.SendClosed(ws, subID, "error: could not query events")
return
}
// Log the number of events retrieved // Query the database with filters and send back the results
fmt.Printf("Retrieved %d events for subscription %s\n", len(queriedEvents), subID) queriedEvents, err := db.QueryEvents(filters, db.GetClient(), "grain")
if len(queriedEvents) == 0 { if err != nil {
fmt.Printf("No matching events found for subscription %s\n", subID) fmt.Println("Error querying events:", err)
} response.SendClosed(ws, subID, "error: could not query events")
return
}
// Send each event back to the client fmt.Printf("Retrieved %d events for subscription %s\n", len(queriedEvents), subID)
for _, evt := range queriedEvents { if len(queriedEvents) == 0 {
msg := []interface{}{"EVENT", subID, evt} fmt.Printf("No matching events found for subscription %s\n", subID)
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 for _, evt := range queriedEvents {
eoseMsg := []interface{}{"EOSE", subID} msg := []interface{}{"EVENT", subID, evt}
eoseBytes, _ := json.Marshal(eoseMsg) msgBytes, _ := json.Marshal(msg)
err = websocket.Message.Send(ws, string(eoseBytes)) err = websocket.Message.Send(ws, string(msgBytes))
if err != nil { if err != nil {
fmt.Println("Error sending EOSE:", err) fmt.Println("Error sending event:", err)
response.SendClosed(ws, subID, "error: could not send EOSE") response.SendClosed(ws, subID, "error: could not send event")
return return
} }
}
fmt.Println("Subscription handling completed, keeping connection open.") // Send EOSE message
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 // Initialize the worker pool when your server starts

View File

@ -0,0 +1,38 @@
package utils
import (
relay "grain/server/types"
"regexp"
)
// isValidHex validates if the given string is a 64-character lowercase hex string
func isValidHex(str string) bool {
return len(str) == 64 && regexp.MustCompile(`^[a-f0-9]{64}$`).MatchString(str)
}
// ValidateFilters ensures the IDs, Authors, and Tags follow the correct hex format
func ValidateFilters(filters []relay.Filter) bool {
for _, f := range filters {
// Validate IDs
for _, id := range f.IDs {
if !isValidHex(id) {
return false
}
}
// Validate Authors
for _, author := range f.Authors {
if !isValidHex(author) {
return false
}
}
// Validate Tags
for _, tags := range f.Tags {
for _, tag := range tags {
if !isValidHex(tag) {
return false
}
}
}
}
return true
}