Skip to content

Commit

Permalink
transport: fixed mis-referred context in
Browse files Browse the repository at this point in the history
ReuseConnTransport.getNewConn

add more comments
  • Loading branch information
IrineSistiana committed Oct 26, 2023
1 parent 74ab9cc commit 91f149b
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions pkg/upstream/transport/reuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (t *ReuseConnTransport) ExchangeContext(ctx context.Context, m []byte) (*[]
}
if c == nil {
isNewConn = true
c, err = t.wantNewConn(ctx)
c, err = t.getNewConn(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -128,19 +128,21 @@ func (t *ReuseConnTransport) ExchangeContext(ctx context.Context, m []byte) (*[]
}
}

func (t *ReuseConnTransport) wantNewConn(ctx context.Context) (*reusableConn, error) {
// getNewConn dial a *reusableConn.
// The caller must call releaseReusableConn to release the reusableConn.
func (t *ReuseConnTransport) getNewConn(ctx context.Context) (*reusableConn, error) {
type dialRes struct {
c *reusableConn
err error
}

dialChan := make(chan dialRes)
go func() {
ctx, cancel := context.WithTimeout(t.ctx, t.dialTimeout)
defer cancel()
dialCtx, cancelDial := context.WithTimeout(t.ctx, t.dialTimeout)
defer cancelDial()

var rc *reusableConn
c, err := t.dialFunc(ctx)
c, err := t.dialFunc(dialCtx)
if err != nil {
t.logger.Check(zap.WarnLevel, "fail to dial reusable conn").Write(zap.Error(err))
}
Expand All @@ -151,8 +153,8 @@ func (t *ReuseConnTransport) wantNewConn(ctx context.Context) (*reusableConn, er

select {
case dialChan <- dialRes{c: rc, err: err}:
case <-ctx.Done():
if rc != nil {
case <-ctx.Done(): // caller canceled getNewConn() call
if rc != nil { // put this conn to pool
t.releaseReusableConn(rc, false)
}
}
Expand Down

0 comments on commit 91f149b

Please sign in to comment.