Skip to content

Commit cdf3635

Browse files
Disable upstream if ns private and 0 peers
1 parent 0d9da54 commit cdf3635

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

client/internal/dns/upstream.go

+23-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"net"
8+
"strings"
89
"sync"
910
"sync/atomic"
1011
"time"
@@ -73,13 +74,12 @@ func newUpstreamResolverBase(ctx context.Context, statusRecorder *peer.Status) *
7374
func (u *upstreamResolverBase) watchPeersConnStatusChanges() {
7475
var probeRunning atomic.Bool
7576
var cancelBackOff context.CancelFunc
76-
7777
exponentialBackOff := &backoff.ExponentialBackOff{
7878
InitialInterval: 200 * time.Millisecond,
7979
RandomizationFactor: 0.5,
8080
Multiplier: 1.1,
81-
MaxInterval: 5 * time.Second,
82-
MaxElapsedTime: 15 * time.Second,
81+
MaxInterval: 1 * time.Second,
82+
MaxElapsedTime: 10 * time.Second,
8383
Stop: backoff.Stop,
8484
Clock: backoff.SystemClock,
8585
}
@@ -98,16 +98,19 @@ func (u *upstreamResolverBase) watchPeersConnStatusChanges() {
9898
}
9999

100100
continualProbe := func() {
101-
// probe continually for 30s when peer count >= 1
102-
if u.statusRecorder.GetConnectedPeersCount() == 0 {
103-
log.Debug("O peers connected, running one more DNS probe")
101+
// probe continually for 10s when peer count >= 1
102+
connectedPeersCount := u.statusRecorder.GetConnectedPeersCount()
103+
if connectedPeersCount == 0 {
104104
// cancel backoff operation
105105
if cancelBackOff != nil {
106106
cancelBackOff()
107107
cancelBackOff = nil
108108
}
109-
u.probeAvailability()
110-
return
109+
if u.areNameServersAllPrivate(u.upstreamServers) {
110+
log.Infof("O peers connected, disabling upstream servers %#v", u.upstreamServers)
111+
u.disable(fmt.Errorf("0 peers connected"))
112+
return
113+
}
111114
}
112115

113116
if probeRunning.Load() {
@@ -152,6 +155,18 @@ func (u *upstreamResolverBase) watchPeersConnStatusChanges() {
152155
}
153156
}
154157

158+
func (u *upstreamResolverBase) areNameServersAllPrivate(nameServers []string) bool {
159+
u.mutex.Lock()
160+
defer u.mutex.Unlock()
161+
for _, n := range nameServers {
162+
ip := net.ParseIP(strings.Split(n, ":")[0])
163+
if !ip.IsPrivate() {
164+
return false
165+
}
166+
}
167+
return true
168+
}
169+
155170
func (u *upstreamResolverBase) stop() {
156171
log.Debugf("stopping serving DNS for upstreams %s", u.upstreamServers)
157172
u.cancel()

0 commit comments

Comments
 (0)