Skip to content

Commit

Permalink
feat(helpers): add server hook to insert into each request context it…
Browse files Browse the repository at this point in the history
…s underlying net connection
  • Loading branch information
jeamon committed Nov 13, 2023
1 parent 680c624 commit 2e7ea27
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions helpers.common.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
RequestIDPrefix string = "r"
ContextRequestID ContextKey = "request.id"
ContextRequestNumber ContextKey = "request.number"
ConnContextKey ContextKey = "http-conn"
)

func (m missingFieldError) Error() string {
Expand Down Expand Up @@ -132,3 +133,15 @@ func IsAppRunningInDocker() bool {
}
return false
}

// SaveConnInContext is the hook used by the server under ConnContext.
// It sets the underlying connection into the request context for later
// use by ReadDeadline or WriteDeadline method on *CustomResponseWriter.
func SaveConnInContext(ctx context.Context, c net.Conn) context.Context {
return context.WithValue(ctx, ConnContextKey, c)
}

// GetConnFromContext returns the connection saved into the context.
func GetConnFromContext(ctx context.Context) net.Conn {
return ctx.Value(ConnContextKey).(net.Conn)
}

0 comments on commit 2e7ea27

Please sign in to comment.