-
Notifications
You must be signed in to change notification settings - Fork 664
/
updater.go
109 lines (95 loc) · 2.73 KB
/
updater.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package dynamicip
import (
"context"
"time"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/utils/ips"
"github.com/ava-labs/avalanchego/utils/logging"
)
const ipResolutionTimeout = 10 * time.Second
var _ Updater = (*updater)(nil)
// Updater periodically updates this node's public IP.
// Dispatch() and Stop() should only be called once.
type Updater interface {
// Start periodically resolving and updating our public IP.
// Doesn't return until after Stop() is called.
// Should be called in a goroutine.
Dispatch(log logging.Logger)
// Stop resolving and updating our public IP.
Stop()
}
type updater struct {
// The IP we periodically modify.
dynamicIP ips.DynamicIPPort
// Used to find out what our public IP is.
resolver Resolver
// The parent of all contexts passed into resolver.Resolve().
// Cancelling causes Dispatch() to eventually return.
rootCtx context.Context
// Cancelling causes Dispatch() to eventually return.
// All in-flight calls to resolver.Resolve() will be cancelled.
rootCtxCancel context.CancelFunc
// Closed when Dispatch() has returned.
doneChan chan struct{}
// How often we update the public IP.
updateFreq time.Duration
}
// Returns a new Updater that updates [dynamicIP]
// every [updateFreq]. Uses [resolver] to find
// out what our public IP is.
func NewUpdater(
dynamicIP ips.DynamicIPPort,
resolver Resolver,
updateFreq time.Duration,
) Updater {
ctx, cancel := context.WithCancel(context.Background())
return &updater{
dynamicIP: dynamicIP,
resolver: resolver,
rootCtx: ctx,
rootCtxCancel: cancel,
doneChan: make(chan struct{}),
updateFreq: updateFreq,
}
}
// Start updating [u.dynamicIP] every [u.updateFreq].
// Stops when [dynamicIP.stopChan] is closed.
func (u *updater) Dispatch(log logging.Logger) {
ticker := time.NewTicker(u.updateFreq)
defer func() {
ticker.Stop()
close(u.doneChan)
}()
for {
select {
case <-ticker.C:
oldIP := u.dynamicIP.IPPort().IP
ctx, cancel := context.WithTimeout(u.rootCtx, ipResolutionTimeout)
newIP, err := u.resolver.Resolve(ctx)
cancel()
if err != nil {
log.Warn("couldn't resolve public IP. If this machine's IP recently changed, it may be sharing the wrong public IP with peers",
zap.Error(err),
)
continue
}
if !newIP.Equal(oldIP) {
u.dynamicIP.SetIP(newIP)
log.Info("updated public IP",
zap.Stringer("newIP", newIP),
)
}
case <-u.rootCtx.Done():
return
}
}
}
func (u *updater) Stop() {
// Cause Dispatch() to return and cancel all
// in-flight calls to resolver.Resolve().
u.rootCtxCancel()
// Wait until Dispatch() has returned.
<-u.doneChan
}