Skip to content

Commit

Permalink
adding mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 committed Aug 23, 2023
1 parent 16b7186 commit 05e8a6d
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions conn/connpool/onetimepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package connpool
import (
"context"
"net"
"sync"
)

type Dialer interface {
Expand All @@ -17,6 +18,7 @@ type OneTimePool struct {
ctx context.Context
cancel context.CancelFunc
Dialer Dialer
mx sync.RWMutex
}

func NewOneTimePool(ctx context.Context, address string, poolSize int) (*OneTimePool, error) {
Expand Down Expand Up @@ -60,8 +62,14 @@ func (p *OneTimePool) Run() error {
conn net.Conn
err error
)
if p.Dialer != nil {
p.mx.RLock()
hasDialer := p.Dialer != nil
p.mx.RUnlock()

if hasDialer {
p.mx.RLock()
conn, err = p.Dialer.Dial(p.ctx, "tcp", p.address)
p.mx.RUnlock()
} else {
conn, err = net.Dial("tcp", p.address)
}
Expand All @@ -75,9 +83,11 @@ func (p *OneTimePool) Run() error {

func (p *OneTimePool) Close() error {
p.cancel()

// remove dialer references
if p.Dialer != nil {
p.Dialer = nil
}
p.mx.Lock()
p.Dialer = nil
p.mx.Unlock()

return p.InFlightConns.Close()
}

0 comments on commit 05e8a6d

Please sign in to comment.