-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
limit BufConnListenerDialer to dialing functions
Uses a local transportDialer interface in config.Cache{}. Adds DialContext() to BufConnListenerDialer.
- Loading branch information
Showing
6 changed files
with
47 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,36 @@ | ||
package listenerutil | ||
|
||
import ( | ||
"context" | ||
"net" | ||
|
||
ctconfig "github.com/hashicorp/consul-template/config" | ||
"google.golang.org/grpc/test/bufconn" | ||
) | ||
|
||
var ( | ||
_ net.Listener = (*BufConnListenerDialer)(nil) | ||
_ ctconfig.TransportDialer = (*BufConnListenerDialer)(nil) | ||
) | ||
|
||
// BufConnListenerDialer implements both a net.Listener and consul | ||
// TransportDialer, to serve both ends of an in-process connection (Dial and | ||
// Accept). | ||
// BufConnListenerDialer implements consul-template's TransportDialer using a | ||
// bufconn listener, to provide a way to Dial the in-memory listener | ||
type BufConnListenerDialer struct { | ||
listener *bufconn.Listener | ||
} | ||
|
||
// NewBufConnListenerDialer returns a new BufConnListenerDialer | ||
func NewBufConnListenerDialer() *BufConnListenerDialer { | ||
buffer := 1024 * 1024 | ||
listener := bufconn.Listen(buffer) | ||
|
||
return &BufConnListenerDialer{listener: listener} | ||
} | ||
|
||
// Accept incoming connections to the bufconn listener | ||
func (bcl *BufConnListenerDialer) Accept() (net.Conn, error) { | ||
return bcl.listener.Accept() | ||
} | ||
|
||
// Close the bufconn | ||
func (bcl *BufConnListenerDialer) Close() error { | ||
return bcl.listener.Close() | ||
// NewBufConnListenerDialer returns a new BufConnListenerDialer using an | ||
// existing bufconn.Listener | ||
func NewBufConnListenerDialer(bcl *bufconn.Listener) *BufConnListenerDialer { | ||
return &BufConnListenerDialer{ | ||
listener: bcl, | ||
} | ||
} | ||
|
||
// Addr returns the net.Addr of the bufconn listener | ||
func (bcl *BufConnListenerDialer) Addr() net.Addr { | ||
return bcl.listener.Addr() | ||
} | ||
|
||
// Dial and connect to the listening end of the bufconn (satisfies | ||
// Dial connects to the listening end of the bufconn (satisfies | ||
// consul-template's TransportDialer interface). This is essentially the client | ||
// side of the bufconn connection. | ||
func (bcl *BufConnListenerDialer) Dial(network, addr string) (net.Conn, error) { | ||
return bcl.listener.Dial() | ||
} | ||
|
||
// DialContext connects to the listening end of the bufconn (satisfies | ||
// consul-template's TransportDialer interface). This is essentially the client | ||
// side of the bufconn connection. | ||
func (bcl *BufConnListenerDialer) DialContext(_ context.Context, network, addr string) (net.Conn, error) { | ||
return bcl.Dial(network, addr) | ||
} |