Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions wsnet/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ func DialWebsocket(ctx context.Context, broker string, netOpts *DialOptions, wsO
// We should close the socket intentionally.
_ = conn.Close(websocket.StatusInternalError, "an error occurred")
}()
return Dial(nconn, netOpts)
return Dial(ctx, nconn, netOpts)
}

// Dial negotiates a connection to a listener.
func Dial(conn net.Conn, options *DialOptions) (*Dialer, error) {
func Dial(ctx context.Context, conn net.Conn, options *DialOptions) (*Dialer, error) {
if options == nil {
options = &DialOptions{}
}
Expand Down Expand Up @@ -121,7 +121,7 @@ func Dial(conn net.Conn, options *DialOptions) (*Dialer, error) {
connClosers: []io.Closer{ctrl},
}

return dialer, dialer.negotiate()
return dialer, dialer.negotiate(ctx)
}

// Dialer enables arbitrary dialing to any network and address
Expand All @@ -138,7 +138,7 @@ type Dialer struct {
pingMut sync.Mutex
}

func (d *Dialer) negotiate() (err error) {
func (d *Dialer) negotiate(ctx context.Context) (err error) {
var (
decoder = json.NewDecoder(d.conn)
errCh = make(chan error)
Expand Down Expand Up @@ -173,6 +173,19 @@ func (d *Dialer) negotiate() (err error) {
})
}()

go func() {
// If a connection is opened but the other end may not be, negotiation
// can get stuck forever. We don't want this, so we must listen to the
// context as well.
<-ctx.Done()
select {
case <-errCh:
default:
errCh <- ctx.Err()
close(errCh)
}
}()

for {
var msg BrokerMessage
err = decoder.Decode(&msg)
Expand Down