diff options
| author | bndw <ben@bdw.to> | 2026-02-07 21:22:51 -0800 |
|---|---|---|
| committer | bndw <ben@bdw.to> | 2026-02-07 21:22:51 -0800 |
| commit | e79f9ad89556000521b43ce5ff4eb59dd00768b0 (patch) | |
| tree | 9f8750e3c5431faf43fa672cb78a733a85d9dcfe /examples/basic/main.go | |
| parent | d4fd2467d691a69a0ba75348086424b9fb33a297 (diff) | |
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
Diffstat (limited to 'examples/basic/main.go')
| -rw-r--r-- | examples/basic/main.go | 39 |
1 files changed, 7 insertions, 32 deletions
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() { | |||
| 53 | ExampleRelay() | 53 | ExampleRelay() |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | // ExampleRelay demonstrates connecting to a relay (requires network). | ||
| 57 | // This is a documentation example - run with: go test -v -run ExampleRelay | ||
| 58 | func ExampleRelay() { | 56 | func ExampleRelay() { |
| 59 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | 57 | ctx := context.Background() |
| 60 | defer cancel() | ||
| 61 | 58 | ||
| 62 | // Connect to a public relay | 59 | // Connect to a public relay |
| 63 | relay, err := nostr.Connect(ctx, "wss://relay.damus.io") | 60 | relay, err := nostr.Connect(ctx, "wss://relay.damus.io") |
| @@ -66,38 +63,16 @@ func ExampleRelay() { | |||
| 66 | return | 63 | return |
| 67 | } | 64 | } |
| 68 | defer relay.Close() | 65 | defer relay.Close() |
| 69 | |||
| 70 | fmt.Println("Connected to relay!") | 66 | fmt.Println("Connected to relay!") |
| 71 | 67 | ||
| 72 | // Subscribe to recent text notes | 68 | ctx, cancel := context.WithTimeout(ctx, 25*time.Second) |
| 73 | since := time.Now().Add(-1 * time.Hour).Unix() | 69 | defer cancel() |
| 74 | sub, err := relay.Subscribe(ctx, "my-sub", nostr.Filter{ | 70 | |
| 71 | filter := nostr.Filter{ | ||
| 75 | Kinds: []int{nostr.KindTextNote}, | 72 | Kinds: []int{nostr.KindTextNote}, |
| 76 | Since: &since, | ||
| 77 | Limit: 5, | 73 | Limit: 5, |
| 78 | }) | ||
| 79 | if err != nil { | ||
| 80 | fmt.Printf("Failed to subscribe: %v\n", err) | ||
| 81 | os.Exit(1) | ||
| 82 | } | 74 | } |
| 83 | 75 | for event := range relay.Fetch(ctx, filter).Events { | |
| 84 | // Listen for events in the background | 76 | fmt.Printf("Received event from %s...\n", event) |
| 85 | go relay.Listen(ctx) | ||
| 86 | |||
| 87 | // Collect events until EOSE | ||
| 88 | eventCount := 0 | ||
| 89 | for { | ||
| 90 | select { | ||
| 91 | case event := <-sub.Events: | ||
| 92 | eventCount++ | ||
| 93 | fmt.Printf("Received event from %s...\n", event) | ||
| 94 | case <-sub.EOSE: | ||
| 95 | fmt.Printf("Received %d events before EOSE\n", eventCount) | ||
| 96 | sub.Close(ctx) | ||
| 97 | return | ||
| 98 | case <-ctx.Done(): | ||
| 99 | fmt.Println("Timeout") | ||
| 100 | return | ||
| 101 | } | ||
| 102 | } | 77 | } |
| 103 | } | 78 | } |
