Skip to content

Commit

Permalink
fix_: canceling route generation before the route gets generated does…
Browse files Browse the repository at this point in the history
…n't allow subscribing for events
  • Loading branch information
saledjenic committed Oct 2, 2024
1 parent 7a73743 commit 04784d1
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion services/wallet/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ type Router struct {
activeRoutesMutex sync.Mutex
activeRoutes *SuggestedRoutes

routeCanceledMutex sync.Mutex
routeCanceled bool

lastInputParamsMutex sync.Mutex
lastInputParams *requests.RouteInputParams

Expand Down Expand Up @@ -199,7 +202,14 @@ func (r *Router) SuggestedRoutesAsync(input *requests.RouteInputParams) {
})
}

func (r *Router) markRouteCanceled(value bool) {
r.routeCanceledMutex.Lock()
r.routeCanceled = value
r.routeCanceledMutex.Unlock()
}

func (r *Router) StopSuggestedRoutesAsyncCalculation() {
r.markRouteCanceled(true)
r.unsubscribeFeesUpdateAccrossAllChains()
r.scheduler.Stop()
}
Expand All @@ -211,6 +221,7 @@ func (r *Router) StopSuggestedRoutesCalculation() {
func (r *Router) SuggestedRoutes(ctx context.Context, input *requests.RouteInputParams) (suggestedRoutes *SuggestedRoutes, err error) {
// unsubscribe from updates
r.unsubscribeFeesUpdateAccrossAllChains()
r.markRouteCanceled(false)

// clear all processors
for _, processor := range r.pathProcessors {
Expand All @@ -227,12 +238,14 @@ func (r *Router) SuggestedRoutes(ctx context.Context, input *requests.RouteInput
r.activeRoutesMutex.Lock()
r.activeRoutes = suggestedRoutes
r.activeRoutesMutex.Unlock()
if suggestedRoutes != nil && err == nil {
r.routeCanceledMutex.Lock()
if suggestedRoutes != nil && err == nil && !r.routeCanceled {
// subscribe for updates
for _, path := range suggestedRoutes.Best {
err = r.subscribeForUdates(path.FromChain.ChainID)
}
}
r.routeCanceledMutex.Unlock()
}()

testnetMode, err := r.rpcClient.NetworkManager.GetTestNetworksEnabled()
Expand Down

0 comments on commit 04784d1

Please sign in to comment.