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:
Anthony Accioly 2025-07-04 18:06:46 +01:00
parent a4d4639e60
commit 32ca7ecb60
No known key found for this signature in database
GPG Key ID: 1BADB4682C8DDB3C
1 changed files with 35 additions and 9 deletions

View File

@ -235,8 +235,27 @@ var bunker = &cli.Command{
printLock := sync.Mutex{}
exitChan := make(chan bool, 1)
// subscribe to relays
events := sys.Pool.SubscribeMany(ctx, relayURLs, nostr.Filter{
// subscription management
var events chan nostr.RelayEvent
var cancelSubscription context.CancelFunc
subscriptionMutex := sync.Mutex{}
// 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(),
@ -244,7 +263,10 @@ var bunker = &cli.Command{
}, nostr.SubscriptionOptions{
Label: "nak-bunker",
})
}
// Initial subscription to relays
updateSubscription()
handlerWg := sync.WaitGroup{}
// == SUBCOMMANDS ==
@ -438,6 +460,10 @@ var bunker = &cli.Command{
}
}
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{