Skip to content

Commit

Permalink
Merge branch opbnb/develep into merge-upstream-v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoieh committed Mar 8, 2024
2 parents 36d34ab + 2fbabc7 commit ba7fb82
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
15 changes: 10 additions & 5 deletions op-service/client/fallback_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,13 @@ func (l *FallbackClient) switchCurrentClient() {
if l.lastMinuteFail.Load() <= l.fallbackThreshold {
return
}
//Use defer to ensure that recoverIfFirstRpcHealth will always be executed regardless of the circumstances.
defer func() {
if !l.isInFallbackState {
l.isInFallbackState = true
l.recoverIfFirstRpcHealth()
}
}()
l.currentIndex++
if l.currentIndex >= len(l.urlList) {
l.log.Error("the fallback client has tried all urls")
Expand All @@ -413,10 +420,6 @@ func (l *FallbackClient) switchCurrentClient() {
}
l.lastMinuteFail.Store(0)
l.log.Info("switched current rpc to new url", "url", url)
if !l.isInFallbackState {
l.isInFallbackState = true
l.recoverIfFirstRpcHealth()
}
}

func (l *FallbackClient) recoverIfFirstRpcHealth() {
Expand All @@ -441,7 +444,9 @@ func (l *FallbackClient) recoverIfFirstRpcHealth() {
}
lastClient := *l.currentClient.Load()
l.currentClient.Store(&l.firstClient)
lastClient.Close()
if lastClient != l.firstClient {
lastClient.Close()
}
l.lastMinuteFail.Store(0)
l.currentIndex = 0
l.isInFallbackState = false
Expand Down
15 changes: 10 additions & 5 deletions op-service/sources/fallback_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ func (l *FallbackClient) switchCurrentRpc() {
}

func (l *FallbackClient) switchCurrentRpcLogic() error {
//Use defer to ensure that recoverIfFirstRpcHealth will always be executed regardless of the circumstances.
defer func() {
if !l.isInFallbackState {
l.isInFallbackState = true
l.recoverIfFirstRpcHealth()
}
}()
url := l.urlList[l.currentIndex]
newRpc, err := l.rpcInitFunc(url)
if err != nil {
Expand All @@ -179,10 +186,6 @@ func (l *FallbackClient) switchCurrentRpcLogic() error {
}
}
l.log.Info("switched current rpc to new url", "url", url)
if !l.isInFallbackState {
l.isInFallbackState = true
l.recoverIfFirstRpcHealth()
}
return nil
}

Expand Down Expand Up @@ -221,7 +224,9 @@ func (l *FallbackClient) recoverIfFirstRpcHealth() {
}
lastRpc := *l.currentRpc.Load()
l.currentRpc.Store(&l.firstRpc)
lastRpc.Close()
if lastRpc != l.firstRpc {
lastRpc.Close()
}
l.lastMinuteFail.Store(0)
l.currentIndex = 0
l.isInFallbackState = false
Expand Down

0 comments on commit ba7fb82

Please sign in to comment.