Skip to content

Commit

Permalink
peers: allow access to underlying connection
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fionera committed Aug 29, 2024
1 parent 278b6f1 commit 89fe89b
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
12 changes: 12 additions & 0 deletions peers/DESIGN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# HAProxy Peers Design
## Connection
By default, the peers package only opens listeners and waits for HAProxy to connect to it.

## Sticktables
A user can create a sticktable definition which can be used to send to a HAProxy peer. Additionally a user can use a
sent definition to send updates for that specific sticktable. For this, the table has to be registered in a table
registry to allow proper referencing.

## Handler
A handler is the interface to implement when wanting to react to sticktable updates. A user created handler can implement
specific sub interfaces to also interact with handshakes.
22 changes: 22 additions & 0 deletions peers/example/complex/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module github.com/dropmorepackets/haproxy-go/peers/example/complex

go 1.21

replace github.com/dropmorepackets/haproxy-go => ../../../

require (
github.com/dropmorepackets/haproxy-go v0.0.2
github.com/prometheus/client_golang v1.17.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
golang.org/x/sys v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
37 changes: 37 additions & 0 deletions peers/example/complex/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=
github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY=
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 h1:v7DLqVdK4VrYkVD5diGdl4sxJurKJEMnODWRJlxV9oM=
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU=
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdOOfY=
github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY=
github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI=
github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
31 changes: 31 additions & 0 deletions peers/example/complex/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"log"
"net/http"

"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/dropmorepackets/haproxy-go/peers"
"github.com/dropmorepackets/haproxy-go/peers/sticktable"
)

type myHandler struct {
peers.PeerHandler
}

func (e *PeerHandler) HandleUpdate(ctx context.Context, update *sticktable.EntryUpdate) {}

func (e *PeerHandler) HandleHandshake(ctx context.Context, handshake *Handshake) {}

func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)

go http.ListenAndServe(":8081", promhttp.Handler())

err := peers.ListenAndServe(":21000", &myHandler{})

if err != nil {
log.Fatal(err)
}
}
17 changes: 16 additions & 1 deletion peers/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}

0 comments on commit 89fe89b

Please sign in to comment.