-
Notifications
You must be signed in to change notification settings - Fork 670
/
dynamic_ip_port.go
56 lines (44 loc) · 949 Bytes
/
dynamic_ip_port.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package ips
import (
"encoding/json"
"net"
"sync"
)
var _ DynamicIPPort = (*dynamicIPPort)(nil)
// An IPPort that can change.
// Safe for use by multiple goroutines.
type DynamicIPPort interface {
// Returns the IP + port pair.
IPPort() IPPort
// Changes the IP.
SetIP(ip net.IP)
}
type dynamicIPPort struct {
lock sync.RWMutex
ipPort IPPort
}
func NewDynamicIPPort(ip net.IP, port uint16) DynamicIPPort {
return &dynamicIPPort{
ipPort: IPPort{
IP: ip,
Port: port,
},
}
}
func (i *dynamicIPPort) IPPort() IPPort {
i.lock.RLock()
defer i.lock.RUnlock()
return i.ipPort
}
func (i *dynamicIPPort) SetIP(ip net.IP) {
i.lock.Lock()
defer i.lock.Unlock()
i.ipPort.IP = ip
}
func (i *dynamicIPPort) MarshalJSON() ([]byte, error) {
i.lock.RLock()
defer i.lock.RUnlock()
return json.Marshal(i.ipPort)
}