Skip to content
Permalink
Browse files

Many upgrades to the client around discovery of port-tagged services …

…via SRV records

Signed-off-by: Erik Hollensbe <github@hollensbe.org>
  • Loading branch information...
erikh committed Nov 19, 2019
1 parent 56f8b03 commit cd2eb5ee1520cf02ff1d36d2bde3fb3f00c9fd08
Showing with 36 additions and 0 deletions.
  1. +23 −0 client/client.go
  2. +13 −0 client/client_test.go
@@ -98,6 +98,29 @@ func (c *Client) ResolveAddress(ctx context.Context, addr string) ([]string, err
return c.dnsResolver.resolver.LookupHost(ctx, addr)
}

// ResolveService returns an address and port for the given service.
func (c *Client) ResolveService(ctx context.Context, service, domain string) (string, uint16, error) {
if err := c.dnsResolver.findService(sdd.ServiceName); err != nil {
return "", 0, err
}

c.dnsResolver.mutex.RLock()
defer c.dnsResolver.mutex.RUnlock()
_, addrs, err := c.dnsResolver.resolver.LookupSRV(ctx, service, "tcp", domain)
if err != nil {
return "", 0, errors.Wrapf(err, "during lookup of service %q", service)
}

target := addrs[0].Target

targetAddrs, err := c.ResolveAddress(ctx, target)
if err != nil {
return "", 0, errors.Wrapf(err, "while looking up target address for %q", service)
}

return targetAddrs[0], addrs[0].Port, nil
}

// Dial is a replacement for the net.Dial function useful for many transport
// implementations in golang.
func (c *Client) Dial(network, addr string) (net.Conn, error) {
@@ -86,6 +86,19 @@ func (cs *ClientSuite) TestClientBasic() {
if !cs.Equal(addrs[0], "127.0.0.10") {
cs.fail()
}

host, port, err := client.ResolveService(context.Background(), "test-svc", "sdd")
if !cs.Nil(err) {
cs.fail()
}

if !cs.Equal(host, "127.0.0.10") {
cs.fail()
}

if !cs.Equal(port, uint16(2222)) {
cs.fail()
}
}

if !cs.Nil(client.UnregisterService(context.Background(), "test", "test-svc")) {

0 comments on commit cd2eb5e

Please sign in to comment.
You can’t perform that action at this time.