44 Commits

Author SHA1 Message Date
fiatjaf
304b0a0477
use gobwas/ws instead now. 2023-04-07 07:37:34 -03:00
fiatjaf
7360fdd349
disable compression. 2023-04-06 17:50:20 -03:00
fiatjaf
f4454c1bb3
replace gorilla websockets with nhooyr.io 2023-04-06 17:41:42 -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
fiatjaf
2d01aa8630
prevent sending on closed channel for subscription. 2022-11-26 09:25:51 -03:00
fiatjaf
67d8f26d8a
some stringifiers. 2022-11-26 09:25:31 -03:00
fiatjaf
b0ae497656
allow more fine-grained control over subscription filters. 2022-11-19 14:00:29 -03:00
fiatjaf
480ee0ef87
PublishEvent() fixes. 2022-11-17 09:28:45 -03:00
fiatjaf
69ccfbaa08
protect against faulty relays that send more than one EOSE halting us
using sync.Once{} to only emit to the EndOfStoredEvents channel once
(it has capacity 1 so anything over that would halt).
2022-11-16 10:07:37 -03:00
fiatjaf
7538f1108d
stop halting at EOSE. 2022-11-16 10:05:28 -03:00
fiatjaf
dd0571229b
RelayConnect() ensures there will be a connection, and handle connection errors better. 2022-11-15 07:53:50 -03:00
fiatjaf
2b8807d699
fix relay .Connect() and .Sub() 2022-11-14 19:48:02 -03:00
fiatjaf
2641327c28
support for EOSE and OK messages on relay/subscription. 2022-11-12 21:49:57 -03:00
fiatjaf
c4d52e516f
change relaypool and subscription such that a Relay can have an independent existence. 2022-11-06 21:15:42 -03:00