60 Commits

Author SHA1 Message Date
fiatjaf
0c06788672
oops: stop replacing connectionContext with Connect(ctx). 2023-05-10 09:09:49 -03:00
fiatjaf
9dc674bc02
use different contexts for the relay connection lifetime and the Connect() call.
fixes https://github.com/nbd-wtf/go-nostr/pull/86
2023-05-09 17:08:08 -03:00
fiatjaf
ccbb44989f
get rid of WriteJSON() and replace calls with manually marshaled envelopes. 2023-05-09 17:02:22 -03:00
fiatjaf
d36fbb95b9
ParseMessage() returns an Envelope, use that on the main relay handler loop. 2023-05-09 16:55:21 -03:00
Marc Tarnutzer
c86e907142 enable compression by default 2023-05-05 22:00:25 +02:00
Marc Tarnutzer
9c01ead91f switch to gobwas/ws, add compression 2023-05-04 23:51:15 +02:00
fiatjaf
32ca737acf
remove unused ping handler (it is done automatically).
fixes https://github.com/nbd-wtf/go-nostr/pull/74, thanks @patdz
2023-04-26 08:12:50 -03:00
fiatjaf
3838ed7e91
guard ping writes with the mutex.
fixes https://github.com/nbd-wtf/go-nostr/issues/76#issuecomment-1521170395
2023-04-26 08:04:51 -03:00
Patrick Bennett
b077a41f83 Fix race condition on status in Relay.Publish method and failure to send
A race-condition exists between setting of the (unprotected) status and the callback which sets the status upon receiving an OK.
The message is sent which can receive an OK in separate goroutine (setting status) prior to the status being set to 'sent.'
The OK can be received prior to the status being set.

This fix also sets the status to PublishStatusFailed if the WriteJSON call fails.
2023-04-21 07:48:08 -03:00
fiatjaf
9b89a49e5e
cancel relay context when calling .Close().
fixing https://github.com/nbd-wtf/go-nostr/issues/76#issuecomment-1517251898
2023-04-21 07:45:28 -03:00
fiatjaf
af52f8d490
debug log events received. 2023-04-14 10:07:10 -03:00
fiatjaf
326d2790de
rename interface{} to any. 2023-04-12 12:14:24 -03:00
fiatjaf
2314db57a1
tweak timeouts on Publish() 2023-04-11 15:33:29 -03:00
fiatjaf
32768b1a5b
improve debug logging, fix stringer interface, debuglog events sent, fix debuglogging affecting the actual values. 2023-04-11 11:02:35 -03:00
fiatjaf
7f64f2f65e
improve logic for subscribing to just-published events. 2023-04-11 10:38:30 -03:00
fiatjaf
df7abe72fa
fix: return PublishStatusFailed if connection can't be established etc. 2023-04-11 09:52:01 -03:00
fiatjaf
6f74d284c4
improved logging thing with the "debug" build tag. 2023-04-11 09:35:37 -03:00
fiatjaf
4507477486
slight improvements to debug logging. 2023-04-11 07:10:05 -03:00
fiatjaf
e103c99bb8
a pluggable logging interface and more debug logging. 2023-04-11 00:32:32 -03:00
fiatjaf
ef428ff39f
Subscription.Fire() can error, so Relay.Subscribe() must also. 2023-04-06 16:21:25 -03:00
fiatjaf
4c9db5928a
log relay url too when filter doesn't match. 2023-04-06 12:57:15 -03:00
fiatjaf
abfb7de394
add a mutex lock around Challenges and Notices channel. 2023-03-31 22:09:01 -03:00
fiatjaf
30e0e1040a
use context passed to .Connect() for the live of the relay. 2023-03-30 18:26:43 -03:00
fiatjaf
e84a7936d5
fix some concurrency things. 2023-03-29 14:55:52 -03:00
fiatjaf
d3a1624880
ping every 29 seconds.
fixes https://github.com/nbd-wtf/go-nostr/issues/72
2023-03-25 14:59:38 -03:00
fiatjaf
4b1f69ec27
subscriptions receive their context on Prepare(). 2023-03-21 14:50:34 -03:00
fiatjaf
e9ad77c822
asynchronous event parsing on received. 2023-03-18 15:09:58 -03:00
fiatjaf
85fc74fd22
fixing subscription labels and ids. 2023-03-18 15:09:49 -03:00
fiatjaf
3f66c60b5f
subscription labels: GetID() and SetLabel(). 2023-03-18 08:40:12 -03:00
fiatjaf
fdc99d61b6
subscription ids as sequential numbers and sub.GetID() 2023-03-17 16:43:48 -03:00
fiatjaf
cef5892bce
return status from publish when relay loses connectivity. 2023-03-17 16:21:04 -03:00
fiatjaf
c982ad0ab1
guard against closed channels in subscriptions. 2023-03-16 15:53:24 -03:00
fiatjaf
ec34d4eb10
return error message from Publish() 2023-03-16 14:27:33 -03:00
fiatjaf
0765f7b91b
context cancelation for relay connections and subscriptions. 2023-03-16 14:16:46 -03:00
fiatjaf
02759120ea
Relay.AssumeValid 2023-03-14 17:07:32 -03:00
Jasper Rädisch
74c646fe21 add RequestHeader option for websocket conn 2023-02-25 16:06:46 -03:00
fiatjaf
a0d9f3a392
send notices on a goroutine. 2023-02-14 11:25:49 -03:00
Wayback Archiver
ab2db2dfc5 Fix unblock mutex
- if `receivedEvent.ID` not match `event.ID`, may trigger an error `fatal error: sync: unlock of unlocked mutex`.
- if context cancled, it does not needs mutex.
2023-02-05 17:10:56 -03:00
Wayback Archiver
a16e2a28d5 Update relay.go 2023-02-05 11:19:26 -03:00
Wayback Archiver
d7d4c62602 Fix execution sequence 2023-02-05 11:19:26 -03:00
fiatjaf
92c0143762
make sub.Events a channel of pointers. 2023-01-26 09:04:27 -03:00
Dylan Cant
67813257df stopping data races with sync.mutex to Publish() in relay.go 2023-01-16 11:32:00 -05:00
barkyq
87b6280299
Added some NIP-42 functionality to the client (relay.go) (#38) 2023-01-16 08:27:11 -03:00
barkyq
a37ffacc74
added sub.mutex handling in the relay Connect() function (#37) 2023-01-15 09:19:00 -03:00
fiatjaf
0397395261
force a deadline of 7 seconds when connecting to relays (if not set).
must add documentation to these calls.
2023-01-03 14:47:21 -03:00
fiatjaf
4a62a753e6
contexts everywhere. 2023-01-01 20:58:43 -03:00
alex
435579dc75 publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:

    ["OK", event-id, false, "blocked"]

this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:

    func (r Relay) Publish(event Event) chan Status {
      // uses a **copy** of statusChans here:
      r.statusChans.Store(event.ID, statusChan)
      ...
    }

the bugfix is a one character change:

    func (r *Relay) Publish(event Event) chan Status

but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:

    // ok, which is a command status from earlier, is shadowed here:
    if statusChan, ok := r.statusChans.Load(eventId); ok {
      statusChan <- ...
    }

as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 14:44:40 -03:00
mlctrez
72ef03f238 use crypto/rand instead of math/rand 2022-12-20 08:00:22 -03:00
alex
c327f622f3 relay: introduce ConnectContext for better control over network latency
A websocket dial may hand for an unreasonably long time and a nostr client
has no control over this when trying to connect to a relay.

Go started introducing context in networking since 2014 -
see https://go.dev/blog/context - and by now many net functions have
XxxContext equivalent, such as DialContext.

Example usage of the change introduced by this commit:

    ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    defer cancel()
    r, err := nostr.RelayConnectContext(ctx, "ws://relay.example.org")

The code above makes RelayConnectContext last at most 3 sec, returning
an error if a connection cannot be established in the given time.
This helps whenever a tight control over connection latency is required,
such as distributed systems.

The change is backwards-compatible except the case where RelayPool.Add
sent an error over the returned channel without actually closing said
channel. I believe it was a bug.
2022-12-17 22:33:05 -03:00
fiatjaf
0c39530d57
QuerySync() relay method. 2022-11-26 19:32:03 -03:00