github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/helper/bufconndialer/bufconndialer.go (about) 1 // BufConnWrapper implements consul-template's TransportDialer using a 2 // bufconn listener, to provide a way to Dial the in-memory listener 3 // 4 // Copied from github.com/hashicorp/vault/internalshared/listenerutil/bufconn.go 5 6 package bufconndialer 7 8 import ( 9 "context" 10 "net" 11 12 "google.golang.org/grpc/test/bufconn" 13 ) 14 15 // BufConnWrapper implements consul-template's TransportDialer using a 16 // bufconn listener, to provide a way to Dial the in-memory listener 17 type BufConnWrapper struct { 18 listener *bufconn.Listener 19 } 20 21 // New returns a new BufConnWrapper with a new bufconn.Listener. The wrapper 22 // provides a dialer for creating connections to the listener. 23 func New() (net.Listener, *BufConnWrapper) { 24 ln := bufconn.Listen(1024 * 1024) 25 return ln, &BufConnWrapper{listener: ln} 26 } 27 28 // NewBufConnWrapper returns a new BufConnWrapper using an 29 // existing bufconn.Listener 30 func NewBufConnWrapper(bcl *bufconn.Listener) *BufConnWrapper { 31 return &BufConnWrapper{ 32 listener: bcl, 33 } 34 } 35 36 // Dial connects to the listening end of the bufconn (satisfies 37 // consul-template's TransportDialer interface). This is essentially the client 38 // side of the bufconn connection. 39 func (bcl *BufConnWrapper) Dial(_, _ string) (net.Conn, error) { 40 return bcl.listener.Dial() 41 } 42 43 // DialContext connects to the listening end of the bufconn (satisfies 44 // consul-template's TransportDialer interface). This is essentially the client 45 // side of the bufconn connection. 46 func (bcl *BufConnWrapper) DialContext(ctx context.Context, _, _ string) (net.Conn, error) { 47 return bcl.listener.DialContext(ctx) 48 }