Skip to content

Commit

Permalink
transport/quic.go: use the dial context for opening streams
Browse files Browse the repository at this point in the history
  • Loading branch information
cooldogedev committed Aug 2, 2024
1 parent ddc50d8 commit d165c61
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions transport/quic.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ type session struct {
timer *time.Timer
}

func (s *session) openStream() (quic.Stream, error) {
func (s *session) openStream(ctx context.Context) (quic.Stream, error) {
s.mu.Lock()
defer s.mu.Unlock()

select {
case <-ctx.Done():
return nil, ctx.Err()
case <-s.conn.Context().Done():
return nil, net.ErrClosed
default:
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

stream, err := s.conn.OpenStreamSync(ctx)
if err != nil {
_ = s.conn.CloseWithError(0, "failed to open stream")
Expand Down Expand Up @@ -100,7 +99,7 @@ func (q *QUIC) Dial(ctx context.Context, addr string) (io.ReadWriteCloser, error
defer q.mu.Unlock()

if s, ok := q.sessions[addr]; ok {
stream, err := s.openStream()
stream, err := s.openStream(ctx)
if err == nil {
return stream, nil
}
Expand Down Expand Up @@ -146,5 +145,5 @@ func (q *QUIC) Dial(ctx context.Context, addr string) (io.ReadWriteCloser, error
q.logger.Debug("closed connection", "addr", addr)
}
}()
return s.openStream()
return s.openStream(ctx)
}

0 comments on commit d165c61

Please sign in to comment.