Skip to content

Conversation

kozlovic
Copy link
Member

We recently added a Dialer option, however, we made it a *net.Dialer,
which is very restrictive (since *net.Dialer is a struct, not an
interface).
One could imagine creating a Dialer that would create pipes between
the client and an embedded server. Since Dial() returns a net.Conn,
which is an interface, everything would work the same.

Resolves #329

We recently added a Dialer option, however, we made it a *net.Dialer,
which is very restrictive (since *net.Dialer is a struct, not an
interface).
One could imagine creating a Dialer that would create pipes between
the client and an embedded server. Since Dial() returns a net.Conn,
which is an interface, everything would work the same.

Resolves #329
@coveralls
Copy link

Coverage Status

Changes Unknown when pulling a2b049b on add_custom_dialer into ** on master**.

@kozlovic
Copy link
Member Author

This is also quite useful. For instance, one could create a custom dialer to return a connection that can trace Read (and/or Write), or introduce some delay, errors, etc... that could be useful in tests.

Here is an example of a CustomDialer to create a traceable connection:

type MyDialer struct{}

type TracedConn struct {
	net.Conn
}

func (tc *TracedConn) Read(b []byte) (n int, err error) {
	n, err = tc.Conn.Read(b)
	if err != nil {
		fmt.Printf("@@ conn err: %v\n", err)
	} else {
		fmt.Printf("@@ conn read: %s\n", b[:n])
	}
	return n, err
}

func (md *MyDialer) Dial(network, address string) (net.Conn, error) {
	d := &net.Dialer{}
	c, err := d.Dial(network, address)
	if err != nil {
		return nil, err
	}
	return &TracedConn{Conn: c}, nil
}

func main() {
	myDialer := &MyDialer{}
	nc, err := nats.Connect(*urls, nats.SetCustomDialer(myDialer))
       (...)
}

@kozlovic kozlovic requested review from wallyqs and derekcollison and removed request for petemiron and ColinSullivan1 December 19, 2017 01:43
Copy link
Member

@derekcollison derekcollison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants