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.
We replace the bare websocket.Conn type with a new Connection type which
implements `WriteJSON`, `WriteMessage`, and `Close`. The Connection
type adds mutexes around writes since gorilla doesn't support concurrent
writes to websockets.
Signed-off-by: Honza Pokorny <honza@pokorny.ca>
The `Sub()` function retrieves the list of filters from the
`Subscripton` object itself, and therefore it's unnecessary to pass it
in. The argument is unused.
Here, we remove the function argument from the definition, and from the
single call site. We then pass the filters to the initializer.
This makes it so that filters are in fact passed to the relay on the wire.