From e79f9ad89556000521b43ce5ff4eb59dd00768b0 Mon Sep 17 00:00:00 2001 From: bndw Date: Sat, 7 Feb 2026 21:22:51 -0800 Subject: refactor: race-safe Subscribe/Fetch API with channel-based Publish - Add mutex-guarded send/stop on Subscription to prevent send-on-closed-channel panics and data races - Split Subscribe (streams after EOSE) and Fetch (closes on EOSE) per NIP-01 - Rewrite Publish to use channel-based OK dispatch instead of calling Receive directly, which raced with the auto-started Listen goroutine - Clean up all subscriptions when Listen exits so range loops terminate - Update tests and examples for new API --- examples/basic/main.go | 39 +++++++-------------------------------- 1 file changed, 7 insertions(+), 32 deletions(-) (limited to 'examples/basic/main.go') diff --git a/examples/basic/main.go b/examples/basic/main.go index 0c99dd9..1a4061a 100644 --- a/examples/basic/main.go +++ b/examples/basic/main.go @@ -53,11 +53,8 @@ func main() { ExampleRelay() } -// ExampleRelay demonstrates connecting to a relay (requires network). -// This is a documentation example - run with: go test -v -run ExampleRelay func ExampleRelay() { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() + ctx := context.Background() // Connect to a public relay relay, err := nostr.Connect(ctx, "wss://relay.damus.io") @@ -66,38 +63,16 @@ func ExampleRelay() { return } defer relay.Close() - fmt.Println("Connected to relay!") - // Subscribe to recent text notes - since := time.Now().Add(-1 * time.Hour).Unix() - sub, err := relay.Subscribe(ctx, "my-sub", nostr.Filter{ + ctx, cancel := context.WithTimeout(ctx, 25*time.Second) + defer cancel() + + filter := nostr.Filter{ Kinds: []int{nostr.KindTextNote}, - Since: &since, Limit: 5, - }) - if err != nil { - fmt.Printf("Failed to subscribe: %v\n", err) - os.Exit(1) } - - // Listen for events in the background - go relay.Listen(ctx) - - // Collect events until EOSE - eventCount := 0 - for { - select { - case event := <-sub.Events: - eventCount++ - fmt.Printf("Received event from %s...\n", event) - case <-sub.EOSE: - fmt.Printf("Received %d events before EOSE\n", eventCount) - sub.Close(ctx) - return - case <-ctx.Done(): - fmt.Println("Timeout") - return - } + for event := range relay.Fetch(ctx, filter).Events { + fmt.Printf("Received event from %s...\n", event) } } -- cgit v1.2.3