Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remote_addr to context #70

Merged
merged 3 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wire

import (
"context"
"net"

"github.com/jackc/pgx/v5/pgtype"
)
Expand All @@ -12,6 +13,7 @@ const (
ctxTypeMap ctxKey = iota
ctxClientMetadata
ctxServerMetadata
ctxRemoteAddr
)

// setTypeInfo constructs a new Postgres type connection info for the given value
Expand All @@ -30,6 +32,21 @@ func TypeMap(ctx context.Context) *pgtype.Map {
return val.(*pgtype.Map)
}

func setRemoteAddress(ctx context.Context, addr net.Addr) context.Context {
return context.WithValue(ctx, ctxRemoteAddr, addr)
}

// RemoteAddress returns the Postgres remote address connection info if it has been set inside
// the given context.
func RemoteAddress(ctx context.Context) net.Addr {
val := ctx.Value(ctxRemoteAddr)
if val == nil {
return nil
}

return val.(net.Addr)
}

// Parameters represents a parameters collection of parameter status keys and
// their values.
type Parameters map[ParameterStatus]string
Expand Down
1 change: 1 addition & 0 deletions wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (srv *Server) Serve(listener net.Listener) error {

func (srv *Server) serve(ctx context.Context, conn net.Conn) error {
ctx = setTypeInfo(ctx, srv.types)
ctx = setRemoteAddress(ctx, conn.RemoteAddr())
defer conn.Close()

srv.logger.Debug("serving a new client connection")
Expand Down
Loading