mirror of https://github.com/fiatjaf/nak.git
refactor(bunker): manage subscriptions dynamically for relay updates
- Refactor subscription management to allow dynamic relay updates. - Introduce cancelable context and lock mechanism to safely recreate subscriptions. - Automatically update subscriptions when relay list changes.
This commit is contained in:
parent
a4d4639e60
commit
32ca7ecb60
44
bunker.go
44
bunker.go
|
@ -235,16 +235,38 @@ var bunker = &cli.Command{
|
||||||
printLock := sync.Mutex{}
|
printLock := sync.Mutex{}
|
||||||
exitChan := make(chan bool, 1)
|
exitChan := make(chan bool, 1)
|
||||||
|
|
||||||
// subscribe to relays
|
// subscription management
|
||||||
events := sys.Pool.SubscribeMany(ctx, relayURLs, nostr.Filter{
|
var events chan nostr.RelayEvent
|
||||||
Kinds: []nostr.Kind{nostr.KindNostrConnect},
|
var cancelSubscription context.CancelFunc
|
||||||
Tags: nostr.TagMap{"p": []string{pubkey.Hex()}},
|
subscriptionMutex := sync.Mutex{}
|
||||||
Since: nostr.Now(),
|
|
||||||
LimitZero: true,
|
|
||||||
}, nostr.SubscriptionOptions{
|
|
||||||
Label: "nak-bunker",
|
|
||||||
})
|
|
||||||
|
|
||||||
|
// Function to create/recreate subscription
|
||||||
|
updateSubscription := func() {
|
||||||
|
subscriptionMutex.Lock()
|
||||||
|
defer subscriptionMutex.Unlock()
|
||||||
|
|
||||||
|
// Cancel existing subscription if it exists
|
||||||
|
if cancelSubscription != nil {
|
||||||
|
cancelSubscription()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new context for the subscription
|
||||||
|
subCtx, cancel := context.WithCancel(ctx)
|
||||||
|
cancelSubscription = cancel
|
||||||
|
|
||||||
|
// Create new subscription with current relay list
|
||||||
|
events = sys.Pool.SubscribeMany(subCtx, relayURLs, nostr.Filter{
|
||||||
|
Kinds: []nostr.Kind{nostr.KindNostrConnect},
|
||||||
|
Tags: nostr.TagMap{"p": []string{pubkey.Hex()}},
|
||||||
|
Since: nostr.Now(),
|
||||||
|
LimitZero: true,
|
||||||
|
}, nostr.SubscriptionOptions{
|
||||||
|
Label: "nak-bunker",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initial subscription to relays
|
||||||
|
updateSubscription()
|
||||||
handlerWg := sync.WaitGroup{}
|
handlerWg := sync.WaitGroup{}
|
||||||
|
|
||||||
// == SUBCOMMANDS ==
|
// == SUBCOMMANDS ==
|
||||||
|
@ -438,6 +460,10 @@ var bunker = &cli.Command{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log("Updated subscription to listen on %d relay(s)\n", len(relayURLs))
|
log("Updated subscription to listen on %d relay(s)\n", len(relayURLs))
|
||||||
|
|
||||||
|
// Cancel and recreate subscription with updated relay list
|
||||||
|
updateSubscription()
|
||||||
|
log("Subscription updated to include new relays\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
responsePayload := nip46.Response{
|
responsePayload := nip46.Response{
|
||||||
|
|
Loading…
Reference in New Issue