12 Commits

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