File tree Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package wire
2
2
3
3
import (
4
4
"context"
5
+ "net"
5
6
6
7
"github.com/jackc/pgx/v5/pgtype"
7
8
)
@@ -12,6 +13,7 @@ const (
12
13
ctxTypeMap ctxKey = iota
13
14
ctxClientMetadata
14
15
ctxServerMetadata
16
+ ctxRemoteAddr
15
17
)
16
18
17
19
// setTypeInfo constructs a new Postgres type connection info for the given value
@@ -30,6 +32,21 @@ func TypeMap(ctx context.Context) *pgtype.Map {
30
32
return val .(* pgtype.Map )
31
33
}
32
34
35
+ func setRemoteAddress (ctx context.Context , addr net.Addr ) context.Context {
36
+ return context .WithValue (ctx , ctxRemoteAddr , addr )
37
+ }
38
+
39
+ // RemoteAddress returns the Postgres remote address connection info if it has been set inside
40
+ // the given context.
41
+ func RemoteAddress (ctx context.Context ) net.Addr {
42
+ val := ctx .Value (ctxRemoteAddr )
43
+ if val == nil {
44
+ return nil
45
+ }
46
+
47
+ return val .(net.Addr )
48
+ }
49
+
33
50
// Parameters represents a parameters collection of parameter status keys and
34
51
// their values.
35
52
type Parameters map [ParameterStatus ]string
Original file line number Diff line number Diff line change @@ -123,6 +123,7 @@ func (srv *Server) Serve(listener net.Listener) error {
123
123
124
124
func (srv * Server ) serve (ctx context.Context , conn net.Conn ) error {
125
125
ctx = setTypeInfo (ctx , srv .types )
126
+ ctx = setRemoteAddress (ctx , conn .RemoteAddr ())
126
127
defer conn .Close ()
127
128
128
129
srv .logger .Debug ("serving a new client connection" )
You can’t perform that action at this time.
0 commit comments