Skip to content

Commit

Permalink
grpcutil.Resolver.Resolve: Take a service parameter instead of hardco…
Browse files Browse the repository at this point in the history
…ding grpclb

Co-authored-by: Peter Štibraný <peter.stibrany@grafana.com>

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
  • Loading branch information
aknuds1 committed Dec 23, 2021
1 parent 92f34b5 commit d3fb543
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [CHANGE] Memberlist: changed probe interval from `1s` to `5s` and probe timeout from `500ms` to `2s`. #90
* [CHANGE] Remove package `math`. #104
* [CHANGE] time: Remove time package. #103
* [CHANGE] grpcutil.Resolver.Resolve: Take a service parameter. #102
* [ENHANCEMENT] Add middleware package. #38
* [ENHANCEMENT] Add the ring package #45
* [ENHANCEMENT] Add limiter package. #41
Expand Down
40 changes: 25 additions & 15 deletions grpcutil/dns_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ func parseTarget(target string) (host, port string, err error) {
return "", "", fmt.Errorf("invalid target address %v", target)
}

// Resolve creates a watcher that watches the name resolution of the target.
func (r *dnsResolver) Resolve(target string) (Watcher, error) {
// Resolve creates a watcher that watches the SRV/hostname record resolution of the target.
//
// If service is not empty, the watcher will first attempt to resolve an SRV record.
// If that fails, or service is empty, hostname record resolution is attempted instead.
func (r *dnsResolver) Resolve(target, service string) (Watcher, error) {
host, port, err := parseTarget(target)
if err != nil {
return nil, err
Expand All @@ -119,22 +122,24 @@ func (r *dnsResolver) Resolve(target string) (Watcher, error) {

ctx, cancel := context.WithCancel(context.Background())
return &dnsWatcher{
r: r,
logger: r.logger,
host: host,
port: port,
ctx: ctx,
cancel: cancel,
t: time.NewTimer(0),
r: r,
logger: r.logger,
host: host,
port: port,
service: service,
ctx: ctx,
cancel: cancel,
t: time.NewTimer(0),
}, nil
}

// dnsWatcher watches for the name resolution update for a specific target
type dnsWatcher struct {
r *dnsResolver
logger log.Logger
host string
port string
r *dnsResolver
logger log.Logger
host string
port string
service string
// The latest resolved address set
curAddrs map[string]*Update
ctx context.Context
Expand Down Expand Up @@ -203,8 +208,13 @@ func (w *dnsWatcher) compileUpdate(newAddrs map[string]*Update) []*Update {
}

func (w *dnsWatcher) lookupSRV() map[string]*Update {
if w.service == "" {
level.Debug(w.logger).Log("msg", "not looking up DNS SRV record since w.service is empty")
return nil
}

newAddrs := make(map[string]*Update)
_, srvs, err := lookupSRV(w.ctx, "grpclb", "tcp", w.host)
_, srvs, err := lookupSRV(w.ctx, w.service, "tcp", w.host)
if err != nil {
level.Info(w.logger).Log("msg", "failed DNS SRV record lookup", "err", err)
return nil
Expand Down Expand Up @@ -251,7 +261,7 @@ func (w *dnsWatcher) lookupHost() map[string]*Update {
func (w *dnsWatcher) lookup() []*Update {
newAddrs := w.lookupSRV()
if newAddrs == nil {
// If failed to get any balancer address (either no corresponding SRV for the
// If we failed to get any balancer address (either no corresponding SRV for the
// target, or caused by failure during resolution/parsing of the balancer target),
// return any A record info available.
newAddrs = w.lookupHost()
Expand Down
6 changes: 3 additions & 3 deletions grpcutil/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ type Update struct {

// Resolver creates a Watcher for a target to track its resolution changes.
type Resolver interface {
// Resolve creates a Watcher for target.
Resolve(target string) (Watcher, error)
// Resolve creates a Watcher for target/service.
Resolve(target, service string) (Watcher, error)
}

// Watcher watches for the updates on the specified target.
// Watcher watches for the SRV updates on the specified target.
type Watcher interface {
// Next blocks until an update or error happens. It may return one or more
// updates. The first call should get the full set of the results. It should
Expand Down

0 comments on commit d3fb543

Please sign in to comment.