@@ -129,6 +129,41 @@ func generateSecureKey(identity substrate.Identity) (*secp256k1.PrivateKey, erro
129
129
return priv , nil
130
130
}
131
131
132
+ // getRelayConnections tries to connect to all relays and returns only the successful ones
133
+ func getRelayConnections (relayURLs []string , identity substrate.Identity , session string , twinID uint32 ) ([]string , []InnerConnection , error ) {
134
+ var successfulRelayURLs []string
135
+ var successfulConnections []InnerConnection
136
+
137
+ for _ , relayURL := range relayURLs {
138
+ parsedURL , err := url .Parse (relayURL )
139
+ if err != nil {
140
+ log .Warn ().Err (err ).Str ("url" , relayURL ).Msg ("failed to parse relay URL" )
141
+ continue
142
+ }
143
+
144
+ conn := NewConnection (identity , relayURL , session , twinID )
145
+ if conn .TryConnect () {
146
+ log .Info ().Str ("url" , relayURL ).Msg ("connected" )
147
+ successfulRelayURLs = append (successfulRelayURLs , parsedURL .Host )
148
+ successfulConnections = append (successfulConnections , conn )
149
+ continue
150
+ }
151
+
152
+ log .Warn ().Str ("url" , relayURL ).Msg ("failed to connect" )
153
+ }
154
+
155
+ if len (successfulRelayURLs ) == 0 {
156
+ return nil , nil , errors .New ("failed to connect to any relay" )
157
+ }
158
+
159
+ sort .Slice (successfulRelayURLs , func (i , j int ) bool {
160
+ return strings .ToLower (successfulRelayURLs [i ]) < strings .ToLower (successfulRelayURLs [j ])
161
+ })
162
+ successfulRelayURLs = slices .Compact (successfulRelayURLs )
163
+
164
+ return successfulRelayURLs , successfulConnections , nil
165
+ }
166
+
132
167
func getIdentity (keytype string , mnemonics string ) (substrate.Identity , error ) {
133
168
var identity substrate.Identity
134
169
var err error
@@ -220,21 +255,12 @@ func NewPeer(
220
255
publicKey = privKey .PubKey ().SerializeCompressed ()
221
256
}
222
257
223
- var relayURLs []string
224
- for _ , relayURL := range cfg .relayURLs {
225
- url , err := url .Parse (relayURL )
226
- if err != nil {
227
- return nil , errors .Wrapf (err , "failed to parse url: %s" , relayURL )
228
- }
229
- relayURLs = append (relayURLs , url .Host )
258
+ relayURLs , conns , err := getRelayConnections (cfg .relayURLs , identity , cfg .session , twin .ID )
259
+ if err != nil {
260
+ return nil , err
230
261
}
231
262
232
- // sort and remove duplicates
233
- sort .Slice (relayURLs , func (i , j int ) bool { return strings .ToLower (relayURLs [i ]) < strings .ToLower (relayURLs [j ]) })
234
- relayURLs = slices .Compact (relayURLs )
235
-
236
263
joinURLs := strings .Join (relayURLs , "_" )
237
-
238
264
if ! bytes .Equal (twin .E2EKey , publicKey ) || twin .Relay == nil || joinURLs != * twin .Relay {
239
265
log .Info ().Str ("Relay url/s" , joinURLs ).Msg ("twin relay/public key didn't match, updating on chain ..." )
240
266
if _ , err = subConn .UpdateTwin (identity , joinURLs , publicKey ); err != nil {
@@ -243,10 +269,8 @@ func NewPeer(
243
269
}
244
270
245
271
reader := make (chan []byte )
246
-
247
- weightCons := make ([]WeightItem [InnerConnection ], len (cfg .relayURLs ))
248
- for _ , url := range cfg .relayURLs {
249
- conn := NewConnection (identity , url , cfg .session , twin .ID )
272
+ weightCons := make ([]WeightItem [InnerConnection ], 0 , len (conns ))
273
+ for _ , conn := range conns {
250
274
conn .Start (ctx , reader )
251
275
weightCons = append (weightCons , WeightItem [InnerConnection ]{Item : conn , Weight : 1 })
252
276
}
0 commit comments