Skip to content

Commit

Permalink
Remove the need to close conninfo (#3376)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Sep 19, 2023
1 parent 9f8f895 commit c844bcc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 24 deletions.
6 changes: 2 additions & 4 deletions internal/clientconn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,12 @@ func (c *conn) run(ctx context.Context) (err error) {
cancel(lazyerrors.Errorf("run exits: %w", err))
}()

connInfo := conninfo.NewConnInfo()
defer connInfo.Close()

connInfo := conninfo.New()
if c.netConn.RemoteAddr().Network() != "unix" {
connInfo.PeerAddr = c.netConn.RemoteAddr().String()
}

ctx = conninfo.WithConnInfo(ctx, connInfo)
ctx = conninfo.Ctx(ctx, connInfo)

done := make(chan struct{})

Expand Down
24 changes: 5 additions & 19 deletions internal/clientconn/conninfo/conn_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package conninfo
import (
"context"
"sync"

"github.com/FerretDB/FerretDB/internal/util/resource"
)

// contextKey is a named unexported type for the safe use of context.WithValue.
Expand All @@ -32,26 +30,14 @@ var connInfoKey = contextKey{}
type ConnInfo struct {
PeerAddr string

token *resource.Token

rw sync.RWMutex
username string
password string
}

// NewConnInfo return a new ConnInfo.
func NewConnInfo() *ConnInfo {
connInfo := &ConnInfo{
token: resource.NewToken(),
}
resource.Track(connInfo, connInfo.token)

return connInfo
}

// Close frees resources.
func (connInfo *ConnInfo) Close() {
resource.Untrack(connInfo, connInfo.token)
// New returns a new ConnInfo.
func New() *ConnInfo {
return new(ConnInfo)
}

// Auth returns stored username and password.
Expand All @@ -71,8 +57,8 @@ func (connInfo *ConnInfo) SetAuth(username, password string) {
connInfo.password = password
}

// WithConnInfo returns a new context with the given ConnInfo.
func WithConnInfo(ctx context.Context, connInfo *ConnInfo) context.Context {
// Ctx returns a derived context with the given ConnInfo.
func Ctx(ctx context.Context, connInfo *ConnInfo) context.Context {
return context.WithValue(ctx, connInfoKey, connInfo)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/clientconn/conninfo/conn_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestGet(t *testing.T) {
connInfo := &ConnInfo{
PeerAddr: tc.peerAddr,
}
ctx = WithConnInfo(ctx, connInfo)
ctx = Ctx(ctx, connInfo)
actual := Get(ctx)
assert.Equal(t, connInfo, actual)
})
Expand Down

0 comments on commit c844bcc

Please sign in to comment.