@@ -19,6 +19,7 @@ package discovery
19
19
import (
20
20
"bytes"
21
21
"fmt"
22
+ "math"
22
23
"strconv"
23
24
"strings"
24
25
"sync"
@@ -94,13 +95,14 @@ type gossipDiscoveryImpl struct {
94
95
95
96
toDieChan chan struct {}
96
97
toDieFlag int32
98
+ port int
97
99
logger * logging.Logger
98
100
disclosurePolicy DisclosurePolicy
99
101
pubsub * util.PubSub
100
102
}
101
103
102
104
// NewDiscoveryService returns a new discovery service with the comm module passed and the crypto service passed
103
- func NewDiscoveryService (bootstrapPeers [] string , self NetworkMember , comm CommService , crypt CryptoService , disPol DisclosurePolicy ) Discovery {
105
+ func NewDiscoveryService (self NetworkMember , comm CommService , crypt CryptoService , disPol DisclosurePolicy ) Discovery {
104
106
d := & gossipDiscoveryImpl {
105
107
self : self ,
106
108
incTime : uint64 (time .Now ().UnixNano ()),
@@ -120,6 +122,7 @@ func NewDiscoveryService(bootstrapPeers []string, self NetworkMember, comm CommS
120
122
pubsub : util .NewPubSub (),
121
123
}
122
124
125
+ d .validateSelfConfig ()
123
126
d .msgStore = newAliveMsgStore (d )
124
127
125
128
go d .periodicalSendAlive ()
@@ -128,8 +131,6 @@ func NewDiscoveryService(bootstrapPeers []string, self NetworkMember, comm CommS
128
131
go d .periodicalReconnectToDead ()
129
132
go d .handlePresumedDeadPeers ()
130
133
131
- go d .connect2BootstrapPeers (bootstrapPeers )
132
-
133
134
d .logger .Info ("Started" , self , "incTime is" , d .incTime )
134
135
135
136
return d
@@ -147,6 +148,13 @@ func (d *gossipDiscoveryImpl) Lookup(PKIID common.PKIidType) *NetworkMember {
147
148
}
148
149
149
150
func (d * gossipDiscoveryImpl ) Connect (member NetworkMember , id identifier ) {
151
+ for _ , endpoint := range []string {member .InternalEndpoint , member .Endpoint } {
152
+ if d .isMyOwnEndpoint (endpoint ) {
153
+ d .logger .Debug ("Skipping connecting to myself" )
154
+ return
155
+ }
156
+ }
157
+
150
158
d .logger .Debug ("Entering" , member )
151
159
defer d .logger .Debug ("Exiting" )
152
160
go func () {
@@ -175,58 +183,41 @@ func (d *gossipDiscoveryImpl) Connect(member NetworkMember, id identifier) {
175
183
}()
176
184
}
177
185
178
- func (d * gossipDiscoveryImpl ) sendUntilAcked (peer * NetworkMember , message * proto.SignedGossipMessage ) {
179
- nonce := message .Nonce
180
- for i := 0 ; i < maxConnectionAttempts && ! d .toDie (); i ++ {
181
- sub := d .pubsub .Subscribe (fmt .Sprintf ("%d" , nonce ), time .Second * 5 )
182
- d .comm .SendToPeer (peer , message )
183
- if _ , timeoutErr := sub .Listen (); timeoutErr == nil {
184
- return
185
- }
186
- time .Sleep (getReconnectInterval ())
187
- }
186
+ func (d * gossipDiscoveryImpl ) isMyOwnEndpoint (endpoint string ) bool {
187
+ return endpoint == fmt .Sprintf ("127.0.0.1:%d" , d .port ) || endpoint == fmt .Sprintf ("localhost:%d" , d .port ) ||
188
+ endpoint == d .self .InternalEndpoint || endpoint == d .self .Endpoint
188
189
}
189
190
190
- func (d * gossipDiscoveryImpl ) connect2BootstrapPeers (endpoints []string ) {
191
- if len (d .self .InternalEndpoint ) == 0 {
192
- d .logger .Panic ("Internal endpoint is empty:" , d .self .InternalEndpoint )
191
+ func (d * gossipDiscoveryImpl ) validateSelfConfig () {
192
+ endpoint := d .self .InternalEndpoint
193
+ if len (endpoint ) == 0 {
194
+ d .logger .Panic ("Internal endpoint is empty:" , endpoint )
193
195
}
194
196
195
- if len (strings .Split (d .self .InternalEndpoint , ":" )) != 2 {
196
- d .logger .Panicf ("Self endpoint %s isn't formatted as 'host:port'" , d .self .InternalEndpoint )
197
+ internalEndpointSplit := strings .Split (endpoint , ":" )
198
+ if len (internalEndpointSplit ) != 2 {
199
+ d .logger .Panicf ("Self endpoint %s isn't formatted as 'host:port'" , endpoint )
197
200
}
198
-
199
- myPort , err := strconv .ParseInt (strings .Split (d .self .InternalEndpoint , ":" )[1 ], 10 , 64 )
201
+ myPort , err := strconv .ParseInt (internalEndpointSplit [1 ], 10 , 64 )
200
202
if err != nil {
201
- d .logger .Panicf ("Self endpoint %s has not valid port'" , d . self . InternalEndpoint )
203
+ d .logger .Panicf ("Self endpoint %s has not valid port'" , endpoint )
202
204
}
203
205
204
- d .logger .Info ("Entering:" , endpoints )
205
- defer d .logger .Info ("Exiting" )
206
- endpoints = filterOutLocalhost (endpoints , int (myPort ))
207
- if len (endpoints ) == 0 {
208
- return
206
+ if myPort > int64 (math .MaxUint16 ) {
207
+ d .logger .Panicf ("Self endpoint %s's port takes more than 16 bits" , endpoint )
209
208
}
210
209
211
- for i := 0 ; i < maxConnectionAttempts && ! d .somePeerIsKnown () && ! d .toDie (); i ++ {
212
- var wg sync.WaitGroup
213
- req := d .createMembershipRequest (true ).NoopSign ()
214
- wg .Add (len (endpoints ))
215
- for _ , endpoint := range endpoints {
216
- go func (endpoint string ) {
217
- defer wg .Done ()
218
- peer := & NetworkMember {
219
- Endpoint : endpoint ,
220
- InternalEndpoint : endpoint ,
221
- }
222
- if ! d .comm .Ping (peer ) {
223
- d .logger .Warning ("Failed connecting to bootstrap peer" , endpoint )
224
- return
225
- }
226
- d .comm .SendToPeer (peer , req )
227
- }(endpoint )
210
+ d .port = int (myPort )
211
+ }
212
+
213
+ func (d * gossipDiscoveryImpl ) sendUntilAcked (peer * NetworkMember , message * proto.SignedGossipMessage ) {
214
+ nonce := message .Nonce
215
+ for i := 0 ; i < maxConnectionAttempts && ! d .toDie (); i ++ {
216
+ sub := d .pubsub .Subscribe (fmt .Sprintf ("%d" , nonce ), time .Second * 5 )
217
+ d .comm .SendToPeer (peer , message )
218
+ if _ , timeoutErr := sub .Listen (); timeoutErr == nil {
219
+ return
228
220
}
229
- wg .Wait ()
230
221
time .Sleep (getReconnectInterval ())
231
222
}
232
223
}
@@ -962,17 +953,6 @@ func getReconnectInterval() time.Duration {
962
953
return util .GetDurationOrDefault ("peer.gossip.reconnectInterval" , getAliveExpirationTimeout ())
963
954
}
964
955
965
- func filterOutLocalhost (endpoints []string , port int ) []string {
966
- var returnedEndpoints []string
967
- for _ , endpoint := range endpoints {
968
- if endpoint == fmt .Sprintf ("127.0.0.1:%d" , port ) || endpoint == fmt .Sprintf ("localhost:%d" , port ) {
969
- continue
970
- }
971
- returnedEndpoints = append (returnedEndpoints , endpoint )
972
- }
973
- return returnedEndpoints
974
- }
975
-
976
956
type aliveMsgStore struct {
977
957
msgstore.MessageStore
978
958
}
0 commit comments