@@ -111,12 +111,10 @@ func action(cli *cli.Context) error {
111111 bo .MaxElapsedTime = 0
112112
113113 // this ctx is used to allow the node to restart the peer without leaving any unwanted open connections
114- peerCtx , cancel := context .WithCancel (ctx )
115- defer cancel ()
116-
114+ currentPeerCtx , cancel := context .WithCancel (ctx )
117115 backoff .Retry (func () error {
118116 _ , err = peer .NewPeer (
119- peerCtx ,
117+ currentPeerCtx ,
120118 hex .EncodeToString (pair .Seed ()),
121119 manager ,
122120 router .Serve ,
@@ -125,6 +123,9 @@ func action(cli *cli.Context) error {
125123 peer .WithInMemoryExpiration (6 * 60 * 60 ), // 6 hours
126124 )
127125 if err != nil {
126+ if cancel != nil {
127+ cancel ()
128+ }
128129 return fmt .Errorf ("failed to start a new rmb peer: %w" , err )
129130 }
130131
@@ -143,16 +144,6 @@ func action(cli *cli.Context) error {
143144 return nil
144145 // check if we need to run an update on the peer and only do the update if all the changes are done successfully
145146 case <- time .After (10 * time .Minute ):
146-
147- log .Debug ().Msg ("checking if node is maintaining a healty substrate connection" )
148- cl , _ , err := manager .Raw ()
149- if err == nil {
150- // skip update if the connection is working properly
151- log .Debug ().Msg ("the open connection is healty, no update needed" )
152- cl .Client .Close ()
153- continue
154- }
155-
156147 env , err := environment .Get ()
157148 if err != nil {
158149 // skip update if we can't get env
@@ -176,42 +167,42 @@ func action(cli *cli.Context) error {
176167 }
177168
178169 log .Debug ().Strs ("relays_urls" , updatedRelayURLs ).Strs ("substrate_urls" , updatedSubURLs ).Msg ("detected new update in configuration" )
179- newPeerCtx , newCancel := context .WithCancel (ctx )
180170
181- newManager , err : = environment .GetSubstrate ()
171+ manager , err = environment .GetSubstrate ()
182172 if err != nil {
183173 // skip update if can't get sub manager
184174 log .Debug ().Err (err ).Msg ("failed to get substrate manager" )
185175 continue
186176 }
187-
188- if _ , err = peer .NewPeer (
189- newPeerCtx ,
190- hex .EncodeToString (pair .Seed ()),
191- newManager ,
192- router .Serve ,
193- peer .WithKeyType (peer .KeyTypeEd25519 ),
194- peer .WithRelay (updatedRelayURLs ... ),
195- peer .WithInMemoryExpiration (6 * 60 * 60 ), // 6 hours
196- ); err != nil {
197- log .Debug ().Err (err ).Msg ("failed to start a new rmb peer" )
198- continue
177+ // cancel the current peer to create new one with updated urls
178+ if cancel != nil {
179+ log .Debug ().Msg ("cancelling current peer context to create a new one with updated urls" )
180+ cancel ()
199181 }
200182
201- if ! slices .Equal (subURLs , updatedSubURLs ) {
202- err = gw .UpdateSubstrateGatewayConnection (newManager )
183+ currentPeerCtx , cancel = context .WithCancel (ctx )
184+ backoff .Retry (func () error {
185+ _ , err = peer .NewPeer (
186+ currentPeerCtx ,
187+ hex .EncodeToString (pair .Seed ()),
188+ manager ,
189+ router .Serve ,
190+ peer .WithKeyType (peer .KeyTypeEd25519 ),
191+ peer .WithRelay (updatedRelayURLs ... ),
192+ peer .WithInMemoryExpiration (6 * 60 * 60 ), // 6 hours
193+ )
203194 if err != nil {
204- log .Debug ().Err (err ).Msg ("failed to update substrate gateway with new manager" )
205- newCancel ()
206- continue
207- }
208- }
195+ if cancel != nil {
196+
197+ // cancel the context to avoid any unwanted open connections
198+ cancel ()
199+ }
200+ return fmt .Errorf ("failed to start a new rmb peer: %w" , err )
209201
210- cancel ()
211- peerCtx = newPeerCtx
212- cancel = newCancel
202+ }
203+ return nil
204+ }, bo )
213205
214- manager = newManager
215206 relayURLs = updatedRelayURLs
216207 subURLs = updatedSubURLs
217208
0 commit comments