Skip to content

Commit

Permalink
Disable upstream if ns private and 0 peers
Browse files Browse the repository at this point in the history
  • Loading branch information
hurricanehrndz committed Aug 29, 2024
1 parent 5e39369 commit 443ce47
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions client/internal/dns/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -73,13 +74,12 @@ func newUpstreamResolverBase(ctx context.Context, statusRecorder *peer.Status) *
func (u *upstreamResolverBase) watchPeersConnStatusChanges() {
var probeRunning atomic.Bool
var cancelBackOff context.CancelFunc

exponentialBackOff := &backoff.ExponentialBackOff{
InitialInterval: 200 * time.Millisecond,
RandomizationFactor: 0.5,
Multiplier: 1.1,
MaxInterval: 5 * time.Second,
MaxElapsedTime: 15 * time.Second,
MaxInterval: 1 * time.Second,
MaxElapsedTime: 10 * time.Second,
Stop: backoff.Stop,
Clock: backoff.SystemClock,
}
Expand All @@ -98,16 +98,19 @@ func (u *upstreamResolverBase) watchPeersConnStatusChanges() {
}

continualProbe := func() {
// probe continually for 30s when peer count >= 1
if u.statusRecorder.GetConnectedPeersCount() == 0 {
log.Debug("O peers connected, running one more DNS probe")
// probe continually for 10s when peer count >= 1
connectedPeersCount := u.statusRecorder.GetConnectedPeersCount()
if connectedPeersCount == 0 {
// cancel backoff operation
if cancelBackOff != nil {
cancelBackOff()
cancelBackOff = nil
}
u.probeAvailability()
return
if u.areNameServersAllPrivate(u.upstreamServers) {
log.Infof("O peers connected, disabling upstream servers %#v", u.upstreamServers)
u.disable(fmt.Errorf("0 peers connected"))
return
}
}

if probeRunning.Load() {
Expand Down Expand Up @@ -152,6 +155,18 @@ func (u *upstreamResolverBase) watchPeersConnStatusChanges() {
}
}

func (u *upstreamResolverBase) areNameServersAllPrivate(nameServers []string) bool {
u.mutex.Lock()
defer u.mutex.Unlock()
for _, n := range nameServers {
ip := net.ParseIP(strings.Split(n, ":")[0])
if !ip.IsPrivate() {
return false
}
}
return true
}

func (u *upstreamResolverBase) stop() {
log.Debugf("stopping serving DNS for upstreams %s", u.upstreamServers)
u.cancel()
Expand Down

0 comments on commit 443ce47

Please sign in to comment.