-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhostname.go
69 lines (53 loc) · 1.53 KB
/
hostname.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
package ipv6ddns
import (
"sync"
"time"
"github.com/miguelangel-nubla/ipv6disc"
)
type Hostname struct {
ipv6disc.AddrCollection
mutex sync.RWMutex
updatedTime time.Time
nextUpdateTime time.Time
nextUpdateTimer *time.Timer
updateRunning bool
updateError error
updateAction func(*ipv6disc.AddrCollection) error
updateDebounceTime time.Duration
updateRetryInterval time.Duration
}
func (h *Hostname) SetAddrCollection(addrCollection *ipv6disc.AddrCollection) {
if !h.AddrCollection.Equal(addrCollection) {
h.AddrCollection = *addrCollection.Copy()
h.ScheduleUpdate(h.updateDebounceTime)
}
}
func (h *Hostname) ScheduleUpdate(timeout time.Duration) {
h.mutex.Lock()
defer h.mutex.Unlock()
// stop the current update timer if it exists
if h.nextUpdateTimer != nil {
h.nextUpdateTimer.Stop()
h.nextUpdateTime = time.Time{}
}
h.nextUpdateTimer = time.AfterFunc(timeout, h.update)
h.nextUpdateTime = time.Now().Add(timeout)
}
func (h *Hostname) update() {
h.updateRunning = true
h.updateError = h.updateAction(&h.AddrCollection)
if h.updateError == nil {
h.updatedTime = time.Now()
} else {
h.ScheduleUpdate(h.updateRetryInterval)
}
h.updateRunning = false
}
func NewHostname(updateAction func(*ipv6disc.AddrCollection) error, updateDebounceTime time.Duration, updateRetryInterval time.Duration) *Hostname {
return &Hostname{
AddrCollection: *ipv6disc.NewAddrCollection(),
updateAction: updateAction,
updateDebounceTime: updateDebounceTime,
updateRetryInterval: updateRetryInterval,
}
}