Skip to content

Commit

Permalink
Merge pull request #69 from humingcheng/http-probe
Browse files Browse the repository at this point in the history
使用http做地址探测
  • Loading branch information
humingcheng authored Jan 18, 2025
2 parents 52023d6 + fb48f22 commit b711ed9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 808 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/static_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ jobs:
name: Merge check
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.13
- name: Set up Go 1.20
uses: actions/setup-go@v1
with:
go-version: 1.13
go-version: 1.20
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
*.out
glide.lock
.idea/
vendor/
9 changes: 8 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
DependencyPath = "/dependencies"
PropertiesPath = "/properties"
TokenPath = "/v4/token"
ReadinessPath = "/health/readiness"
HeaderContentType = "Content-Type"
HeaderUserAgent = "User-Agent"
HeaderAuth = "Authorization"
Expand Down Expand Up @@ -147,7 +148,13 @@ func NewClient(opt Options) (*Client, error) {
}
// Update the API Base Path based on the project
c.updateAPIPath()
c.pool = addresspool.NewPool(opt.Endpoints)
c.pool = addresspool.NewPool(opt.Endpoints, addresspool.Options{
HttpProbeOptions: &addresspool.HttpProbeOptions{
Client: c.client,
Protocol: c.protocol,
Path: MSAPIPath + ReadinessPath,
},
})
return c, nil
}

Expand Down
23 changes: 17 additions & 6 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,18 @@ func TestClient_DataRace(t *testing.T) {
}

func TestClient_SyncEndpoints(t *testing.T) {
mockHealthApiServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
os.Setenv("CHASSIS_SC_HEALTH_CHECK_INTERVAL", "1")

anotherScServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusOK)
return
}))

scServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
resp := &discovery.GetInstancesResponse{
Instances: []*discovery.MicroServiceInstance{
{
Endpoints: []string{"rest://127.0.0.1:3000"},
Endpoints: []string{"rest://" + anotherScServer.Listener.Addr().String()},
HostName: "test",
Status: sc.MSInstanceUP,
DataCenterInfo: &discovery.DataCenterInfo{
Expand All @@ -362,12 +369,16 @@ func TestClient_SyncEndpoints(t *testing.T) {

c, err := sc.NewClient(
sc.Options{
Endpoints: []string{mockHealthApiServer.Listener.Addr().String()},
Endpoints: []string{scServer.Listener.Addr().String()},
})
assert.NoError(t, err)
assert.Equal(t, scServer.Listener.Addr().String(), c.GetAddress()) // default

// should use the synced address
err = c.SyncEndpoints()
assert.NoError(t, err)
assert.Equal(t, "127.0.0.1:3000", c.GetAddress())
assert.Equal(t, scServer.Listener.Addr().String(), c.GetAddress())

scServer.Close()
time.Sleep(3*time.Second + 100*time.Millisecond)
// sc stopped, should use the synced address
assert.Equal(t, anotherScServer.Listener.Addr().String(), c.GetAddress())
}
16 changes: 14 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
module github.com/go-chassis/sc-client

go 1.20

require (
github.com/cenkalti/backoff/v4 v4.1.1
github.com/go-chassis/cari v0.9.1-0.20241220092204-f30abdb125d9
github.com/go-chassis/cari v0.9.1-0.20250118080951-ce3e155fb54e
github.com/go-chassis/foundation v0.4.0
github.com/go-chassis/openlog v1.1.3
github.com/gorilla/websocket v1.4.3-0.20210424162022-e8629af678b7
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/stretchr/testify v1.7.2
)

go 1.13
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.7.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/karlseguin/ccache/v2 v2.0.8 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit b711ed9

Please sign in to comment.