-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Go Substrate Client - Connection Failover Implementation (#1022)
* feat: Maintains single connection with multiple backup URLs * fix: remove panic in Transfer * test: adding test suite covers failover senarios * chore: remove old workflows * Update CI and test image * fix: guard Substarte connection state in GetClient() and Close() * test: Added a sync.WaitGroup to properly track when goroutines complete in TestFailoverMechanism
- Loading branch information
1 parent
5e76664
commit d8a0468
Showing
9 changed files
with
194 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,21 +21,22 @@ jobs: | |
- name: Set up Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: "1.20" | ||
go-version: "1.21" | ||
cache: false | ||
# cache-dependency-path: clients/tfchain-client-go/go.sum | ||
id: go | ||
|
||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v3.7.0 | ||
uses: golangci/golangci-lint-action@v6 | ||
with: | ||
args: --timeout 3m --verbose | ||
working-directory: clients/tfchain-client-go | ||
|
||
- name: staticcheck | ||
uses: dominikh/staticcheck-action@v1.3.0 | ||
uses: dominikh/staticcheck-action@v1 | ||
with: | ||
version: "2022.1.3" | ||
version: "latest" | ||
install-go: false | ||
working-directory: clients/tfchain-client-go | ||
env: | ||
GO111MODULE: on | ||
|
@@ -44,4 +45,4 @@ jobs: | |
uses: Jerome1337/[email protected] | ||
with: | ||
gofmt-path: './clients/tfchain-client-go' | ||
gofmt-flags: "-l -d" | ||
gofmt-flags: "-l -d" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package substrate | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFailoverMechanism(t *testing.T) { | ||
t.Run("should failover to next URL when current node is unhealthy", func(t *testing.T) { | ||
// Create manager with multiple URLs | ||
urls := []string{"ws://fail1", getUrlBasedOnEnv()} | ||
mgr := NewManager(urls...) | ||
|
||
// Get initial substrate client | ||
sub, err := mgr.Substrate() | ||
require.NoError(t, err) | ||
defer sub.Close() | ||
|
||
// Store initial Client | ||
initialClient := sub.cl.Client | ||
|
||
// Force connection to become unhealthy by closing it | ||
sub.cl.Client.Close() | ||
|
||
// Try to use the connection - should trigger failover | ||
_, err = sub.Time() | ||
require.NoError(t, err) | ||
|
||
// Check that we're now using a different URL | ||
newClient := sub.cl.Client | ||
assert.NotEqual(t, initialClient, newClient) | ||
}) | ||
|
||
t.Run("should try all URLs in rotation", func(t *testing.T) { | ||
urls := []string{ | ||
"ws://fail1", | ||
"ws://fail2", | ||
getUrlBasedOnEnv(), | ||
} | ||
|
||
mgr := NewManager(urls...) | ||
sub, err := mgr.Substrate() | ||
require.NoError(t, err) | ||
defer sub.Close() | ||
|
||
// The final URL should be the working one | ||
assert.Equal(t, getUrlBasedOnEnv(), sub.cl.Client.URL()) | ||
}) | ||
|
||
t.Run("should reuse connection if healthy", func(t *testing.T) { | ||
sub := startLocalConnection(t) | ||
defer sub.Close() | ||
|
||
initialClient := sub.cl.Client | ||
|
||
// Use the connection multiple times | ||
for i := 0; i < 3; i++ { | ||
_, err := sub.Time() | ||
require.NoError(t, err) | ||
assert.Equal(t, initialClient, sub.cl.Client) | ||
} | ||
}) | ||
|
||
t.Run("should handle all nodes being down", func(t *testing.T) { | ||
urls := []string{"ws://fail1", "ws://fail2"} | ||
mgr := NewManager(urls...) | ||
_, err := mgr.Substrate() | ||
assert.Error(t, err) | ||
}) | ||
|
||
t.Run("should handle concurrent failover attempts", func(t *testing.T) { | ||
urls := []string{getUrlBasedOnEnv(), getUrlBasedOnEnv()} | ||
mgr := NewManager(urls...) | ||
sub1, err := mgr.Substrate() | ||
require.NoError(t, err) | ||
defer sub1.Close() | ||
|
||
sub2, err := mgr.Substrate() | ||
require.NoError(t, err) | ||
defer sub2.Close() | ||
|
||
// Force both connections to fail | ||
sub1.cl.Client.Close() | ||
sub2.cl.Client.Close() | ||
|
||
// Create WaitGroup to ensure all goroutines complete before test ends | ||
var wg sync.WaitGroup | ||
wg.Add(2) | ||
|
||
// Try to use both connections concurrently | ||
errs := make(chan error, 2) | ||
go func() { | ||
defer wg.Done() | ||
_, err := sub1.Time() | ||
errs <- err | ||
}() | ||
|
||
go func() { | ||
defer wg.Done() | ||
_, err := sub2.Time() | ||
errs <- err | ||
}() | ||
|
||
// Wait for both operations to complete | ||
go func() { | ||
wg.Wait() | ||
close(errs) | ||
}() | ||
|
||
// Check errors from both goroutines | ||
for err := range errs { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters