doggo/internal/client/client.go

54 lines
1.4 KiB
Go

package client
import (
"math/rand"
"time"
"github.com/miekg/dns"
"github.com/mr-karan/doggo/internal/models"
"github.com/mr-karan/doggo/internal/resolvers"
"github.com/mr-karan/logf"
)
// Client represents the structure for all app wide configuration.
type Client struct {
Log *logf.Logger
Version string
QueryFlags models.QueryFlags
Questions []dns.Question
Resolvers []resolvers.Resolver
Nameservers []models.Nameserver
}
// New initializes an instance of App which holds app wide configuration.
func New(logger *logf.Logger, buildVersion string) Client {
return Client{
Log: logger,
Version: buildVersion,
QueryFlags: models.QueryFlags{
QNames: []string{},
QTypes: []string{},
QClasses: []string{},
Nameservers: []string{},
},
Nameservers: []models.Nameserver{},
}
}
// LookupWithRetry attempts a DNS Lookup with retries for a given Question.
func (hub *Client) LookupWithRetry(attempts int, resolver resolvers.Resolver, ques dns.Question) (resolvers.Response, error) {
resp, err := resolver.Lookup(ques)
if err != nil {
// Retry lookup.
attempts--
if attempts > 0 {
// Add some random delay.
time.Sleep(time.Millisecond*300 + (time.Duration(rand.Int63n(int64(time.Millisecond*100))))/2)
hub.Log.Debug("retrying lookup")
return hub.LookupWithRetry(attempts, resolver, ques)
}
return resolvers.Response{}, err
}
return resp, nil
}