Skip to content

Commit

Permalink
make closeOnceConn private, it is wrapped
Browse files Browse the repository at this point in the history
  • Loading branch information
ainghazal committed Jan 10, 2024
1 parent 83e6a25 commit 4dc55c2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 7 additions & 7 deletions internal/networkio/closeonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ import (
"sync"
)

// CloseOnceConn is a [net.Conn] where the Close method has once semantics.
// closeOnceConn is a [net.Conn] where the Close method has once semantics.
//
// The zero value is invalid; use [NewCloseOnceConn].
type CloseOnceConn struct {
type closeOnceConn struct {
// once ensures we close just once.
once sync.Once

// Conn is the underlying conn.
net.Conn
}

var _ net.Conn = &CloseOnceConn{}
var _ net.Conn = &closeOnceConn{}

// NewCloseOnceConn creates a [CloseOnceConn].
func NewCloseOnceConn(conn net.Conn) *CloseOnceConn {
return &CloseOnceConn{
// newCloseOnceConn creates a [CloseOnceConn].
func newCloseOnceConn(conn net.Conn) *closeOnceConn {
return &closeOnceConn{
once: sync.Once{},
Conn: conn,
}
}

// Close implements net.Conn
func (c *CloseOnceConn) Close() (err error) {
func (c *closeOnceConn) Close() (err error) {
c.once.Do(func() {
err = c.Conn.Close()
})
Expand Down
2 changes: 1 addition & 1 deletion internal/networkio/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (d *Dialer) DialContext(ctx context.Context, network, address string) (Fram
d.logger.Infof("connected to %s/%s", address, network)

// make sure the conn has close once semantics
conn = NewCloseOnceConn(conn)
conn = newCloseOnceConn(conn)

// wrap the conn and return
switch conn.LocalAddr().Network() {
Expand Down

0 comments on commit 4dc55c2

Please sign in to comment.