66 Commits

Author SHA1 Message Date
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