forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addr_set.go
103 lines (91 loc) · 2.71 KB
/
addr_set.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
// Copyright 2014 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
package gossip
import (
"math/rand"
"net"
)
// Keeps a set of addresses and provides simple address-matched
// management functions. addrSet is not thread safe.
type addrSet struct {
addrs map[string]net.Addr // Map from net.Addr.String() to net.Addr
maxSize int // Maximum size of set
}
func newAddrSet(maxSize int) *addrSet {
return &addrSet{
addrs: make(map[string]net.Addr),
maxSize: maxSize,
}
}
// hasSpace returns whether there are fewer than maxSize addrs
// in the addrs slice.
func (as *addrSet) hasSpace() bool {
return len(as.addrs) < as.maxSize
}
// len returns the number of addresses in the set.
func (as *addrSet) len() int {
return len(as.addrs)
}
// asSlice returns the addresses as a slice.
func (as *addrSet) asSlice() []net.Addr {
arr := make([]net.Addr, len(as.addrs))
var count int
for _, addr := range as.addrs {
arr[count] = addr
count++
}
return arr
}
// selectRandom returns a random address from the set. Returns nil if
// there are no addresses to select.
func (as *addrSet) selectRandom() net.Addr {
idx := rand.Intn(len(as.addrs))
var count = 0
for _, addr := range as.addrs {
if count == idx {
return addr
}
count++
}
return nil
}
// filter returns an addrSet of addresses which return true when
// passed to the supplied filter function filterFn. filterFn should
// return true to keep an address and false to remove an address.
func (as *addrSet) filter(filterFn func(addr net.Addr) bool) *addrSet {
avail := newAddrSet(as.maxSize)
for _, addr := range as.addrs {
if filterFn(addr) {
avail.addAddr(addr)
}
}
return avail
}
// hasAddr verifies that the supplied address matches a addr
// in the slice.
func (as *addrSet) hasAddr(addr net.Addr) bool {
_, ok := as.addrs[addr.String()]
return ok
}
// addAddr adds the addr to the addrs set.
func (as *addrSet) addAddr(addr net.Addr) {
as.addrs[addr.String()] = addr
}
// removeAddr removes the addr from the addrs set.
func (as *addrSet) removeAddr(addr net.Addr) {
delete(as.addrs, addr.String())
}