force a deadline of 7 seconds when connecting to relays (if not set).

must add documentation to these calls.
This commit is contained in:
fiatjaf 2023-01-03 14:47:21 -03:00
parent 3349b2a52b
commit 0397395261
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1

View File

@ -65,6 +65,13 @@ func (r *Relay) Connect(ctx context.Context) error {
return fmt.Errorf("invalid relay URL '%s'", r.URL)
}
if _, ok := ctx.Deadline(); !ok {
// if no timeout is set, force it to 7 seconds
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, 7*time.Second)
defer cancel()
}
socket, _, err := websocket.DefaultDialer.DialContext(ctx, r.URL, nil)
if err != nil {
return fmt.Errorf("error opening websocket to '%s': %w", r.URL, err)