From f3fa1161e975151bea87dd8f755f471911fb4737 Mon Sep 17 00:00:00 2001 From: Fionera Date: Thu, 29 Aug 2024 15:58:32 +0200 Subject: [PATCH] peers: allow access to underlying connection This is a first iteration for @awlx. I think this does require further investigation as I am also not very happy with the handler interface. As soon as we try to send data to HAProxy we do need a base implementation that tracks sticktable ids and allows us to define new one and reference known sticktables. This is required as we always have to send HAProxy all sticktables we want to send updates for and can't reference the ones we already got from that instance. --- peers/peers.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/peers/peers.go b/peers/peers.go index b1d13f2..2f01c18 100644 --- a/peers/peers.go +++ b/peers/peers.go @@ -56,7 +56,10 @@ func (a *Peer) Serve(l net.Listener) error { return fmt.Errorf("accepting conn: %w", err) } - p := newProtocolClient(a.BaseContext, nc, a.HandlerSource()) + // Wrap the context to provide access to the underlying connection. + // TODO(tim): Do we really want this? + ctx := context.WithValue(a.BaseContext, connectionKey, nc) + p := newProtocolClient(ctx, nc, a.HandlerSource()) go func() { defer nc.Close() defer p.Close() @@ -67,3 +70,15 @@ func (a *Peer) Serve(l net.Listener) error { }() } } + +type contextKey string + +const ( + connectionKey = contextKey("connection") +) + +// Connection returns the underlying connection used in calls +// to function in a Handler. +func Connection(ctx context.Context) net.Conn { + return ctx.Value(connectionKey).(net.Conn) +}