expose GetSubscriptionID(ctx)

This commit is contained in:
fiatjaf
2023-12-25 09:14:09 -03:00
parent 9f635e4e41
commit 77600dc05c
3 changed files with 14 additions and 7 deletions

View File

@@ -56,7 +56,7 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithCancel(
context.WithValue(
context.Background(),
WS_KEY, ws,
wsKey, ws,
),
)
@@ -171,6 +171,9 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
// a context just for the "stored events" request handler
reqCtx, cancelReqCtx := context.WithCancelCause(ctx)
// expose subscription id in the context
reqCtx = context.WithValue(reqCtx, subscriptionIdKey, env.SubscriptionID)
// handle each filter separately -- dispatching events as they're loaded from databases
for _, filter := range env.Filters {
err := rl.handleRequest(reqCtx, env.SubscriptionID, &eose, ws, filter)

View File

@@ -10,11 +10,6 @@ import (
"github.com/nbd-wtf/go-nostr"
)
const (
AUTH_CONTEXT_KEY = iota
WS_KEY
)
func pointerHasher[V any](_ maphash.Seed, k *V) uint64 {
return uint64(uintptr(unsafe.Pointer(k)))
}

View File

@@ -7,8 +7,13 @@ import (
"github.com/sebest/xff"
)
const (
wsKey = iota
subscriptionIdKey
)
func GetConnection(ctx context.Context) *WebSocket {
return ctx.Value(WS_KEY).(*WebSocket)
return ctx.Value(wsKey).(*WebSocket)
}
func GetAuthed(ctx context.Context) string {
@@ -19,6 +24,10 @@ func GetIP(ctx context.Context) string {
return xff.GetRemoteAddr(GetConnection(ctx).Request)
}
func GetSubscriptionID(ctx context.Context) string {
return ctx.Value(subscriptionIdKey).(string)
}
func GetOpenSubscriptions(ctx context.Context) []nostr.Filter {
if subs, ok := listeners.Load(GetConnection(ctx)); ok {
res := make([]nostr.Filter, 0, listeners.Size()*2)