From 03973952619f8f0d3169b139ec1b6938d756b98a Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 3 Jan 2023 14:47:21 -0300 Subject: [PATCH] force a deadline of 7 seconds when connecting to relays (if not set). must add documentation to these calls. --- relay.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/relay.go b/relay.go index 9fec740..444df4e 100644 --- a/relay.go +++ b/relay.go @@ -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)