Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions addresspool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type HttpProbeOptions struct {

type Options struct {
HttpProbeOptions *HttpProbeOptions // used in available check if set, tcp will be used if not set
DiffAzEndpoints []string
}

// Pool cloud server address pool
Expand Down Expand Up @@ -77,17 +78,23 @@ func NewPool(addresses []string, opts ...Options) *Pool {
statusHistory: make([]map[string]string, 0, 4),
}

if len(opts) > 0 && opts[0].HttpProbeOptions != nil {
optCopy := *(opts[0].HttpProbeOptions)
p.httpProbeOptions = &optCopy
if len(p.httpProbeOptions.Protocol) == 0 {
p.httpProbeOptions.Protocol = "http"
if len(opts) > 0 {
if opts[0].HttpProbeOptions != nil {
optCopy := *(opts[0].HttpProbeOptions)
p.httpProbeOptions = &optCopy
if len(p.httpProbeOptions.Protocol) == 0 {
p.httpProbeOptions.Protocol = "http"
}
p.httpProbeClient, _ = httpclient.New(&httpclient.Options{
TLSConfig: &tls.Config{InsecureSkipVerify: true},
RequestTimeout: 5 * time.Second,
})
}
if len(opts[0].DiffAzEndpoints) != 0 {
p.diffAzAddress = opts[0].DiffAzEndpoints
}
p.httpProbeClient, _ = httpclient.New(&httpclient.Options{
TLSConfig: &tls.Config{InsecureSkipVerify: true},
RequestTimeout: 5 * time.Second,
})
}

p.monitor()
return p
}
Expand Down
75 changes: 75 additions & 0 deletions addresspool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,81 @@ func TestAddressPool_SetAddressByInstances(t *testing.T) {
assert.Equal(t, []string{"192.168.1.1:30100", "192.168.1.2:30100"}, p.diffAzAddress)
}

// pool 存在diffAzAddress时,通过SetAddressByInstances方法后,diffAzAddress不会为空
func TestAddressPool_SetAddressByInstancesForDiffAzAddress(t *testing.T) {
p := NewPool([]string{"192.168.3.1:30100"}, Options{
DiffAzEndpoints: []string{"192.168.3.5:30100"}})
err := p.SetAddressByInstances(nil)
assert.Error(t, err)
assert.Equal(t, []string{"192.168.3.5:30100"}, p.diffAzAddress)

err = p.SetAddressByInstances([]*discovery.MicroServiceInstance{})
assert.Error(t, err)
assert.Equal(t, []string{"192.168.3.5:30100"}, p.diffAzAddress)

// SetAddressByInstances仅传入本端地址时
err = p.SetAddressByInstances([]*discovery.MicroServiceInstance{
{
Endpoints: []string{"rest://192.168.3.1:30100"},
},
})
assert.NoError(t, err)
assert.Equal(t, []string{"192.168.3.5:30100"}, p.diffAzAddress)

// SetAddressByInstances仅传入对端地址时
err = p.SetAddressByInstances([]*discovery.MicroServiceInstance{
{
Endpoints: []string{"rest://192.168.3.5:30100"},
},
})
assert.NoError(t, err)
assert.Equal(t, []string{"192.168.3.5:30100"}, p.diffAzAddress)

// SetAddressByInstances同时传入本端和对端地址时
err = p.SetAddressByInstances([]*discovery.MicroServiceInstance{
{
Endpoints: []string{"rest://192.168.3.1:30100"},
DataCenterInfo: &discovery.DataCenterInfo{
Name: "engine2",
Region: "cn1",
AvailableZone: "cn1a",
},
},
{
Endpoints: []string{"rest://192.168.3.5:30100"},
DataCenterInfo: &discovery.DataCenterInfo{
Name: "engine2",
Region: "cn2",
AvailableZone: "cn2a",
},
},
})
assert.NoError(t, err)
assert.Equal(t, []string{"192.168.3.5:30100"}, p.diffAzAddress)

// SetAddressByInstances 两个地址都传入为同AZ地址时
err = p.SetAddressByInstances([]*discovery.MicroServiceInstance{
{
Endpoints: []string{"rest://192.168.3.1:30100"},
DataCenterInfo: &discovery.DataCenterInfo{
Name: "engine2",
Region: "cn3",
AvailableZone: "cn3a",
},
},
{
Endpoints: []string{"rest://192.168.3.5:30100"},
DataCenterInfo: &discovery.DataCenterInfo{
Name: "engine2",
Region: "cn3",
AvailableZone: "cn3a",
},
},
})
assert.NoError(t, err)
assert.Equal(t, []string{"192.168.3.5:30100"}, p.diffAzAddress)
}

func TestAddressPool_checkConnectivity(t *testing.T) {
server1 := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
return
Expand Down
Loading