forked from uber/tchannel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root_peer_list.go
117 lines (98 loc) · 3.32 KB
/
root_peer_list.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
110
111
112
113
114
115
116
117
// Copyright (c) 2015 Uber Technologies, Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tchannel
import "sync"
// RootPeerList is the root peer list which is only used to connect to
// peers and share peers between subchannels.
type RootPeerList struct {
sync.RWMutex
channel Connectable
peersByHostPort map[string]*Peer
}
func newRootPeerList(ch Connectable) *RootPeerList {
return &RootPeerList{
channel: ch,
peersByHostPort: make(map[string]*Peer),
}
}
// newChild returns a new isolated peer list that shares the underlying peers
// with the root peer list.
func (l *RootPeerList) newChild() *PeerList {
return newPeerList(l)
}
// Add adds a peer to the root peer list if it does not exist, or return
// an existing peer if it exists.
func (l *RootPeerList) Add(hostPort string) *Peer {
l.RLock()
if p, ok := l.peersByHostPort[hostPort]; ok {
l.RUnlock()
return p
}
l.RUnlock()
l.Lock()
defer l.Unlock()
if p, ok := l.peersByHostPort[hostPort]; ok {
return p
}
var p *Peer
// To avoid duplicate connections, only the root list should create new
// peers. All other lists should keep refs to the root list's peers.
p = newPeer(l.channel, hostPort, l.onConnChange)
l.peersByHostPort[hostPort] = p
return p
}
// GetOrAdd returns a peer for the given hostPort, creating one if it doesn't yet exist.
func (l *RootPeerList) GetOrAdd(hostPort string) *Peer {
peer, ok := l.Get(hostPort)
if ok {
return peer
}
return l.Add(hostPort)
}
// Get returns a peer for the given hostPort if it exists.
func (l *RootPeerList) Get(hostPort string) (*Peer, bool) {
l.RLock()
p, ok := l.peersByHostPort[hostPort]
l.RUnlock()
return p, ok
}
func (l *RootPeerList) onConnChange(peer *Peer) {
hostPort := peer.HostPort()
p, ok := l.Get(hostPort)
if !ok {
l.channel.Logger().Error("got connection state change for a peer not in the root peer list")
return
}
if p.canRemove() {
l.Lock()
delete(l.peersByHostPort, hostPort)
l.Unlock()
l.channel.Logger().Infof("Removed peer %s from root peer list", hostPort)
}
}
// Copy returns a map of the peer list. This method should only be used for testing.
func (l *RootPeerList) Copy() map[string]*Peer {
l.RLock()
defer l.RUnlock()
listCopy := make(map[string]*Peer)
for k, v := range l.peersByHostPort {
listCopy[k] = v
}
return listCopy
}