Skip to content

Commit

Permalink
fix(@desktop/wallet): Remove auto retries for connections errors. In …
Browse files Browse the repository at this point in the history
…case of an error there are two things that can happen

1. The user can manually click on "Retry now"
2. We have a 10 in timer on wallet, after whichb all the data shown is refreshed

fixes #10124
  • Loading branch information
Khushboo-dev-cpp committed Apr 4, 2023
1 parent ef4ffce commit 0426d7d
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 137 deletions.
2 changes: 1 addition & 1 deletion src/app/modules/main/network_connection/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ proc delete*(self: Controller) =
proc init*(self: Controller) =
self.events.on(SIGNAL_CONNECTION_UPDATE) do(e:Args):
let args = NetworkConnectionsArgs(e)
self.delegate.networkConnectionStatusUpdate(args.website, args.completelyDown, ord(args.connectionState), args.chainIds, args.lastCheckedAt, args.timeToAutoRetryInSecs)
self.delegate.networkConnectionStatusUpdate(args.website, args.completelyDown, ord(args.connectionState), args.chainIds, args.lastCheckedAt)

self.events.on(SIGNAL_NETWORK_CONNECTED) do(e: Args):
self.networkConnectionService.networkConnected(true)
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/main/network_connection/io_interface.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ method isLoaded*(self: AccessInterface): bool {.base.} =
method viewDidLoad*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")

method networkConnectionStatusUpdate*(self: AccessInterface, website: string, completelyDown: bool, connectionState: int, chainIds: string, lastCheckedAt: int, timeToAutoRetryInSecs: int) {.base.} =
method networkConnectionStatusUpdate*(self: AccessInterface, website: string, completelyDown: bool, connectionState: int, chainIds: string, lastCheckedAt: int) {.base.} =
raise newException(ValueError, "No implementation available")

method refreshBlockchainValues*(self: AccessInterface) {.base.} =
Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/main/network_connection/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ proc checkIfModuleDidLoad(self: Module) =
method viewDidLoad*(self: Module) =
self.checkIfModuleDidLoad()

method networkConnectionStatusUpdate*(self: Module, website: string, completelyDown: bool, connectionState: int, chainIds: string, lastCheckedAt: int, timeToAutoRetryInSecs: int) =
self.view.updateNetworkConnectionStatus(website, completelyDown, connectionState, chainIds, lastCheckedAt, timeToAutoRetryInSecs)
method networkConnectionStatusUpdate*(self: Module, website: string, completelyDown: bool, connectionState: int, chainIds: string, lastCheckedAt: int) =
self.view.updateNetworkConnectionStatus(website, completelyDown, connectionState, chainIds, lastCheckedAt)

method refreshBlockchainValues*(self: Module) =
self.controller.refreshBlockchainValues()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@ QtObject:
connectionState: int
chainIds: string
lastCheckedAt: int
timeToAutoRetryInSecs: int

proc delete*(self: NetworkConnectionItem) =
self.QObject.delete

proc newNetworkConnectionItem*(completelyDown = false, connectionState = 0, chainIds = "", lastCheckedAt = 0, timeToAutoRetryInSecs = 0): NetworkConnectionItem =
proc newNetworkConnectionItem*(completelyDown = false, connectionState = 0, chainIds = "", lastCheckedAt = 0): NetworkConnectionItem =
new(result, delete)
result.QObject.setup
result.completelyDown = completelyDown
result.connectionState = connectionState
result.chainIds = chainIds
result.lastCheckedAt = lastCheckedAt
result.timeToAutoRetryInSecs = timeToAutoRetryInSecs

proc `$`*(self: NetworkConnectionItem): string =
result = fmt"""NetworkConnectionItem[
completelyDown: {self.completelyDown},
connectionState: {self.connectionState},
chainIds: {self.chainIds},
lastCheckedAt: {self.lastCheckedAt},
timeToAutoRetryInSecs: {self.timeToAutoRetryInSecs}
lastCheckedAt: {self.lastCheckedAt}
]"""

proc completelyDownChanged*(self: NetworkConnectionItem) {.signal.}
Expand Down Expand Up @@ -57,15 +54,8 @@ QtObject:
read = getLastCheckedAt
notify = lastCheckedAtChanged

proc timeToAutoRetryInSecsChanged*(self: NetworkConnectionItem) {.signal.}
proc getTimeToAutoRetryInSecs*(self: NetworkConnectionItem): int {.slot.} =
return self.timeToAutoRetryInSecs
QtProperty[int] timeToAutoRetryInSecs:
read = getTimeToAutoRetryInSecs
notify = timeToAutoRetryInSecsChanged

proc updateValues*(self: NetworkConnectionItem, completelyDown: bool, connectionState: int,
chainIds: string, lastCheckedAt: int, timeToAutoRetryInSecs: int) =
chainIds: string, lastCheckedAt: int) =
if self.completelyDown != completelyDown :
self.completelyDown = completelyDown
self.completelyDownChanged()
Expand All @@ -81,7 +71,3 @@ QtObject:
if self.lastCheckedAt != lastCheckedAt :
self.lastCheckedAt = lastCheckedAt
self.lastCheckedAtChanged()

if self.timeToAutoRetryInSecs != timeToAutoRetryInSecs :
self.timeToAutoRetryInSecs = timeToAutoRetryInSecs
self.timeToAutoRetryInSecsChanged()
12 changes: 6 additions & 6 deletions src/app/modules/main/network_connection/view.nim
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ QtObject:
proc refreshCollectiblesValues*(self: View) {.slot.} =
self.delegate.refreshCollectiblesValues()

proc networkConnectionStatusUpdate*(self: View, website: string, completelyDown: bool, connectionState: int, chainIds: string, lastCheckedAt: int, timeToAutoRetryInSecs: int) {.signal.}
proc networkConnectionStatusUpdate*(self: View, website: string, completelyDown: bool, connectionState: int, chainIds: string, lastCheckedAt: int) {.signal.}

proc updateNetworkConnectionStatus*(self: View, website: string, completelyDown: bool, connectionState: int, chainIds: string, lastCheckedAt: int, timeToAutoRetryInSecs: int) =
proc updateNetworkConnectionStatus*(self: View, website: string, completelyDown: bool, connectionState: int, chainIds: string, lastCheckedAt: int) =
case website:
of BLOCKCHAINS:
self.blockchainNetworkConnection.updateValues(completelyDown, connectionState, chainIds, lastCheckedAt, timeToAutoRetryInSecs)
self.blockchainNetworkConnection.updateValues(completelyDown, connectionState, chainIds, lastCheckedAt)
self.blockchainNetworkConnectionChanged()
of COLLECTIBLES:
self.collectiblesNetworkConnection.updateValues(completelyDown, connectionState, chainIds, lastCheckedAt, timeToAutoRetryInSecs)
self.collectiblesNetworkConnection.updateValues(completelyDown, connectionState, chainIds, lastCheckedAt)
self.collectiblesNetworkConnectionChanged()
of MARKET:
self.marketValuesNetworkConnection.updateValues(completelyDown, connectionState, chainIds, lastCheckedAt, timeToAutoRetryInSecs)
self.marketValuesNetworkConnection.updateValues(completelyDown, connectionState, chainIds, lastCheckedAt)
self.marketValuesNetworkConnectionChanged()
self.networkConnectionStatusUpdate(website, completelyDown, connectionState, chainIds, lastCheckedAt, timeToAutoRetryInSecs)
self.networkConnectionStatusUpdate(website, completelyDown, connectionState, chainIds, lastCheckedAt)

62 changes: 8 additions & 54 deletions src/app_service/service/network_connection/service.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ type ConnectionStatus* = ref object of RootObj
completelyDown*: bool
chainIds*: seq[int]
lastCheckedAt*: int
timeToAutoRetryInSecs*: int
timer*: QTimer

const SIGNAL_CONNECTION_UPDATE* = "signalConnectionUpdate"
const SIGNAL_REFRESH_COLLECTIBLES* = "signalRefreshCollectibles"
Expand All @@ -35,12 +33,10 @@ type NetworkConnectionsArgs* = ref object of Args
connectionState*: ConnectionState
chainIds*: string
lastCheckedAt*: int
timeToAutoRetryInSecs*: int

const BLOCKCHAINS* = "blockchains"
const MARKET* = "market"
const COLLECTIBLES* = "collectibles"
const BACKOFF_TIMERS* = [30, 60, 180, 600, 3600, 10800]

include ../../common/json_utils

Expand All @@ -50,8 +46,6 @@ proc newConnectionStatus(): ConnectionStatus =
completelyDown: false,
chainIds: @[],
lastCheckedAt: 0,
timeToAutoRetryInSecs: BACKOFF_TIMERS[0],
timer: newQTimer()
)

QtObject:
Expand Down Expand Up @@ -157,67 +151,39 @@ QtObject:
completelyDown: connectionStatus.completelyDown,
connectionState: connectionStatus.connectionState,
chainIds: self.getFormattedStringForChainIds(connectionStatus.chainIds),
lastCheckedAt: connectionStatus.lastCheckedAt,
timeToAutoRetryInSecs: connectionStatus.timeToAutoRetryInSecs
lastCheckedAt: connectionStatus.lastCheckedAt
)

proc updateConnectionStatus(self: Service,
website: string,
connectionState: ConnectionState,
completelyDown: bool,
chainIds: seq[int],
lastCheckedAt: int,
timeToAutoRetryInSecs: int
lastCheckedAt: int
) =
if self.connectionStatus.hasKey(website):
self.connectionStatus[website].connectionState = connectionState
self.connectionStatus[website].completelyDown = completelyDown
self.connectionStatus[website].chainIds = chainIds
self.connectionStatus[website].lastCheckedAt = lastCheckedAt
self.connectionStatus[website].timeToAutoRetryInSecs = timeToAutoRetryInSecs

proc increaseTimer(self: Service, connectionStatus: ConnectionStatus): int =
var backOffTimer: int = connectionStatus.timeToAutoRetryInSecs
# Is down even after retry we need to increase the timer duration
if connectionStatus.connectionState == ConnectionState.Retrying:
let index = BACKOFF_TIMERS.find(backOffTimer)
if index != -1 and index < BACKOFF_TIMERS.len:
backOffTimer = BACKOFF_TIMERS[index + 1]
return backOffTimer

proc updateMarketOrCollectibleStatus(self: Service, website: string, isDown: bool, at: int) =
if self.connectionStatus.hasKey(website):
if isDown:
self.updateConnectionStatus(website, ConnectionState.Failed, true, @[], at, self.increaseTimer(self.connectionStatus[website]))
# restart timer
signalConnect(self.connectionStatus[website].timer, "timeout()", self, website&"Retry()", 2)
self.connectionStatus[website].timer.setInterval(self.connectionStatus[website].timeToAutoRetryInSecs*1000)
self.connectionStatus[website].timer.start()

# trigger event
self.updateConnectionStatus(website, ConnectionState.Failed, true, @[], at)
self.events.emit(SIGNAL_CONNECTION_UPDATE, self.convertConnectionStatusToNetworkConnectionsArgs(website, self.connectionStatus[website]))
else:
# site was completely down and is back up now
# if site was completely down and is back up now, trigger event
if self.connectionStatus[website].completelyDown:
self.connectionStatus[website] = newConnectionStatus()
# trigger event
self.events.emit(SIGNAL_CONNECTION_UPDATE, self.convertConnectionStatusToNetworkConnectionsArgs(website, self.connectionStatus[website]))


proc updateBlockchainsStatus(self: Service, completelyDown: bool, chaindIdsDown: seq[int], at: int) =
if self.connectionStatus.hasKey(BLOCKCHAINS):
# if all the networks are down for the BLOCKCHAINS
# if all the networks are down for the BLOCKCHAINS, trigger event
if completelyDown:
var backOffTimer: int = self.connectionStatus[BLOCKCHAINS].timeToAutoRetryInSecs
if self.connectionStatus[BLOCKCHAINS].completelyDown:
backOffTimer = self.increaseTimer(self.connectionStatus[BLOCKCHAINS])
self.updateConnectionStatus(BLOCKCHAINS, ConnectionState.Failed, true, chaindIdsDown, at, backOffTimer)
# restart timer
signalConnect(self.connectionStatus[BLOCKCHAINS].timer, "timeout()", self, BLOCKCHAINS&"Retry()", 2)
self.connectionStatus[BLOCKCHAINS].timer.setInterval(self.connectionStatus[BLOCKCHAINS].timeToAutoRetryInSecs*1000)
self.connectionStatus[BLOCKCHAINS].timer.start()

# trigger event
self.updateConnectionStatus(BLOCKCHAINS, ConnectionState.Failed, true, chaindIdsDown, at)
self.events.emit(SIGNAL_CONNECTION_UPDATE, self.convertConnectionStatusToNetworkConnectionsArgs(BLOCKCHAINS, self.connectionStatus[BLOCKCHAINS]))

# if all the networks are not down for the website
Expand All @@ -227,34 +193,25 @@ QtObject:
self.connectionStatus[BLOCKCHAINS] = newConnectionStatus()
self.events.emit(SIGNAL_CONNECTION_UPDATE, self.convertConnectionStatusToNetworkConnectionsArgs(BLOCKCHAINS, self.connectionStatus[BLOCKCHAINS]))

# case where a some of networks on the website are down
# case where a some of networks on the website are down, trigger event
if chaindIdsDown.len > 0:
self.updateConnectionStatus(BLOCKCHAINS, ConnectionState.Failed, false, chaindIdsDown, at, self.increaseTimer(self.connectionStatus[BLOCKCHAINS]))
# restart timer
signalConnect(self.connectionStatus[BLOCKCHAINS].timer, "timeout()", self, BLOCKCHAINS&"Retry()", 2)
self.connectionStatus[BLOCKCHAINS].timer.setInterval(self.connectionStatus[BLOCKCHAINS].timeToAutoRetryInSecs*1000)
self.connectionStatus[BLOCKCHAINS].timer.start()

# trigger event
self.updateConnectionStatus(BLOCKCHAINS, ConnectionState.Failed, false, chaindIdsDown, at)
self.events.emit(SIGNAL_CONNECTION_UPDATE, self.convertConnectionStatusToNetworkConnectionsArgs(BLOCKCHAINS, self.connectionStatus[BLOCKCHAINS]))

proc blockchainsRetry*(self: Service) {.slot.} =
if(self.connectionStatus.hasKey(BLOCKCHAINS)):
self.connectionStatus[BLOCKCHAINS].timer.stop()
self.connectionStatus[BLOCKCHAINS].connectionState = ConnectionState.Retrying
self.events.emit(SIGNAL_CONNECTION_UPDATE, self.convertConnectionStatusToNetworkConnectionsArgs(BLOCKCHAINS, self.connectionStatus[BLOCKCHAINS]))
self.walletService.reloadAccountTokens()

proc marketRetry*(self: Service) {.slot.} =
if(self.connectionStatus.hasKey(MARKET)):
self.connectionStatus[MARKET].timer.stop()
self.connectionStatus[MARKET].connectionState = ConnectionState.Retrying
self.events.emit(SIGNAL_CONNECTION_UPDATE, self.convertConnectionStatusToNetworkConnectionsArgs(MARKET, self.connectionStatus[MARKET]))
self.walletService.reloadAccountTokens()

proc collectiblesRetry*(self: Service) {.slot.} =
if(self.connectionStatus.hasKey(COLLECTIBLES)):
self.connectionStatus[COLLECTIBLES].timer.stop()
self.connectionStatus[COLLECTIBLES].connectionState = ConnectionState.Retrying
self.events.emit(SIGNAL_CONNECTION_UPDATE, self.convertConnectionStatusToNetworkConnectionsArgs(COLLECTIBLES, self.connectionStatus[COLLECTIBLES]))
self.events.emit(SIGNAL_REFRESH_COLLECTIBLES, Args())
Expand All @@ -265,13 +222,10 @@ QtObject:
self.events.emit(SIGNAL_REFRESH_COLLECTIBLES, Args())
else:
if(self.connectionStatus.hasKey(BLOCKCHAINS)):
self.connectionStatus[BLOCKCHAINS].timer.stop()
self.connectionStatus[BLOCKCHAINS] = newConnectionStatus()
if(self.connectionStatus.hasKey(MARKET)):
self.connectionStatus[MARKET].timer.stop()
self.connectionStatus[MARKET] = newConnectionStatus()
if(self.connectionStatus.hasKey(COLLECTIBLES)):
self.connectionStatus[COLLECTIBLES].timer.stop()
self.connectionStatus[COLLECTIBLES] = newConnectionStatus()

proc checkIfConnected*(self: Service, website: string): bool =
Expand Down
Loading

0 comments on commit 0426d7d

Please sign in to comment.