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

1
.gitignore vendored
View File

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

View File

@ -13,11 +13,12 @@ import (
// QueryEvents queries events from the MongoDB collection(s) based on filters
func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName string) ([]relay.Event, error) {
var results []relay.Event
var combinedFilters []bson.M
// Build MongoDB filters for each relay.Filter
for _, filter := range filters {
filterBson := bson.M{}
// Construct the BSON query based on the filters
if len(filter.IDs) > 0 {
filterBson["id"] = bson.M{"$in": filter.IDs}
}
@ -45,23 +46,34 @@ func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName stri
}
}
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 {
opts.SetLimit(int64(*filter.Limit))
}
}
// If no specific kinds are specified, query all collections in the database
if len(filter.Kinds) == 0 {
// If no specific kinds are specified, query all collections
if len(filters[0].Kinds) == 0 {
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 {
fmt.Printf("Querying collection: %s with query: %v\n", collectionName, filterBson)
collection := client.Database(databaseName).Collection(collectionName)
cursor, err := collection.Find(context.TODO(), filterBson, opts)
cursor, err := collection.Find(context.TODO(), query, opts)
if err != nil {
return nil, fmt.Errorf("error querying collection %s: %v", collectionName, err)
}
@ -80,12 +92,10 @@ func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName stri
}
} else {
// Query specific collections based on kinds
for _, kind := range filter.Kinds {
for _, kind := range filters[0].Kinds {
collectionName := fmt.Sprintf("event-kind%d", kind)
fmt.Printf("Querying collection: %s with query: %v\n", collectionName, filterBson)
collection := client.Database(databaseName).Collection(collectionName)
cursor, err := collection.Find(context.TODO(), filterBson, opts)
cursor, err := collection.Find(context.TODO(), query, opts)
if err != nil {
return nil, fmt.Errorf("error querying collection %s: %v", collectionName, err)
}
@ -103,7 +113,6 @@ func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName stri
}
}
}
}
return results, nil
}

View File

@ -49,6 +49,7 @@ func HandleReq(ws *websocket.Conn, message []interface{}, subscriptions map[stri
}
}
// processRequest handles the actual processing of each request
func processRequest(ws *websocket.Conn, message []interface{}) {
if len(message) < 3 {
@ -58,18 +59,17 @@ func processRequest(ws *websocket.Conn, message []interface{}) {
}
subID, ok := message[1].(string)
if !ok {
fmt.Println("Invalid subscription ID format")
response.SendClosed(ws, "", "invalid: invalid subscription ID format")
if !ok || len(subID) == 0 || len(subID) > 64 {
fmt.Println("Invalid subscription ID format or length")
response.SendClosed(ws, "", "invalid: subscription ID must be between 1 and 64 characters long")
return
}
mu.Lock()
defer mu.Unlock()
// Check the current number of subscriptions for the client
// Remove oldest subscription if needed
if len(subscriptions) >= config.GetConfig().Server.MaxSubscriptionsPerClient {
// Find and remove the oldest subscription (FIFO)
var oldestSubID string
for id := range subscriptions {
oldestSubID = id
@ -79,7 +79,7 @@ func processRequest(ws *websocket.Conn, message []interface{}) {
fmt.Println("Dropped oldest subscription:", oldestSubID)
}
// Prepare filters based on the incoming message
// Parse and validate filters
filters := make([]relay.Filter, len(message)-2)
for i, filter := range message[2:] {
filterData, ok := filter.(map[string]interface{})
@ -101,7 +101,14 @@ func processRequest(ws *websocket.Conn, message []interface{}) {
filters[i] = f
}
// Add the new subscription or update the existing one
// Validate filters
if !utils.ValidateFilters(filters) {
fmt.Println("Invalid filters: hex values not valid")
response.SendClosed(ws, subID, "invalid: filters contain invalid hex values")
return
}
// Add subscription
subscriptions[subID] = relay.Subscription{Filters: filters}
fmt.Printf("Subscription updated: %s with %d filters\n", subID, len(filters))
@ -113,13 +120,11 @@ func processRequest(ws *websocket.Conn, message []interface{}) {
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)
@ -131,7 +136,7 @@ func processRequest(ws *websocket.Conn, message []interface{}) {
}
}
// Indicate end of stored events
// Send EOSE message
eoseMsg := []interface{}{"EOSE", subID}
eoseBytes, _ := json.Marshal(eoseMsg)
err = websocket.Message.Send(ws, string(eoseBytes))

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
}