Skip to content

Commit

Permalink
support context cancellation when dialing new connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Tantalor93 committed Jul 8, 2023
1 parent a4dd832 commit f9b834a
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions doq/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,34 @@ func (c *Client) dial(ctx context.Context) error {
c.lock.Lock()
defer c.lock.Unlock()
if c.conn != nil {
c.conn.ConnectionState()
if err := c.conn.Context().Err(); err == nil {
// somebody else created the connection in the meantime, no need to do anything
return nil
}
}
conn, err := quic.DialAddrEarly(ctx, c.addr, c.tlsconfig, nil)
if err != nil {
return err
}
done := make(chan interface{})

c.conn = conn
go func() {
conn, err := quic.DialAddrEarly(ctx, c.addr, c.tlsconfig, nil)
if err != nil {
done <- err
return
}
done <- conn
}()

select {
case <-ctx.Done():
return ctx.Err()
case res := <-done:
switch r := res.(type) {
case error:
return r
case quic.Connection:
c.conn = r
}
}

return nil
}
Expand Down

0 comments on commit f9b834a

Please sign in to comment.