Skip to content

Commit 41f1988

Browse files
authored
Remove input client in http probe, use internal instead. (#76)
Remove input client in http probe, use internal instead. (#76)
1 parent c24f08a commit 41f1988

File tree

2 files changed

+83
-10
lines changed

2 files changed

+83
-10
lines changed

Diff for: addresspool/pool.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package addresspool
22

33
import (
44
"context"
5+
"crypto/tls"
56
"fmt"
67
"io"
78
"net"
@@ -35,7 +36,6 @@ const (
3536
)
3637

3738
type HttpProbeOptions struct {
38-
Client *httpclient.Requests
3939
Protocol string
4040
Path string
4141
}
@@ -53,11 +53,13 @@ type Pool struct {
5353
sameAzAddress []string
5454
diffAzAddress []string
5555

56-
status map[string]string
57-
onceMonitor sync.Once
58-
quit chan struct{}
59-
onceQuit sync.Once
56+
status map[string]string
57+
onceMonitor sync.Once
58+
quit chan struct{}
59+
onceQuit sync.Once
60+
6061
httpProbeOptions *HttpProbeOptions
62+
httpProbeClient *httpclient.Requests
6163
statusHistory []map[string]string
6264
}
6365

@@ -76,11 +78,15 @@ func NewPool(addresses []string, opts ...Options) *Pool {
7678
}
7779

7880
if len(opts) > 0 && opts[0].HttpProbeOptions != nil {
79-
p.httpProbeOptions = opts[0].HttpProbeOptions
80-
if p.httpProbeOptions.Client == nil {
81-
openlog.Info(fmt.Sprintf("http client nil, make one with default options"))
82-
p.httpProbeOptions.Client, _ = httpclient.New(nil)
81+
optCopy := *(opts[0].HttpProbeOptions)
82+
p.httpProbeOptions = &optCopy
83+
if len(p.httpProbeOptions.Protocol) == 0 {
84+
p.httpProbeOptions.Protocol = "http"
8385
}
86+
p.httpProbeClient, _ = httpclient.New(&httpclient.Options{
87+
TLSConfig: &tls.Config{InsecureSkipVerify: true},
88+
RequestTimeout: 5 * time.Second,
89+
})
8490
}
8591
p.monitor()
8692
return p
@@ -268,7 +274,7 @@ func (p *Pool) doCheckConnectivityWithTcp(endpoint string) error {
268274

269275
func (p *Pool) doCheckConnectivityWithHttp(endpoint string) error {
270276
u := p.httpProbeOptions.Protocol + "://" + endpoint + p.httpProbeOptions.Path
271-
resp, err := p.httpProbeOptions.Client.Get(context.Background(), u, nil)
277+
resp, err := p.httpProbeClient.Get(context.Background(), u, nil)
272278
if err != nil {
273279
return err
274280
}

Diff for: addresspool/pool_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/http"
55
"net/http/httptest"
66
"os"
7+
"reflect"
78
"sync"
89
"testing"
910
"time"
@@ -33,6 +34,13 @@ func TestNewPool(t *testing.T) {
3334
mockHttpServer.Close()
3435
time.Sleep(2*time.Second + 100*time.Millisecond)
3536
assert.NotEqual(t, statusAvailable, pool.status[defaultAddr]) // the status should be unavailable again
37+
38+
httpProbeOpt := &HttpProbeOptions{
39+
Protocol: "http",
40+
}
41+
pool = NewPool([]string{defaultAddr}, Options{HttpProbeOptions: httpProbeOpt})
42+
assert.False(t, httpProbeOpt == pool.httpProbeOptions) // not equal but deep equal, as copied
43+
assert.True(t, reflect.DeepEqual(httpProbeOpt, pool.httpProbeOptions))
3644
}
3745

3846
func TestAddressPool_GetAvailableAddress_priority(t *testing.T) {
@@ -328,3 +336,62 @@ func TestPool_CheckReadiness(t *testing.T) {
328336
})
329337
}
330338
}
339+
340+
func TestPool_doCheckConnectivity(t *testing.T) {
341+
server1HttpCalled := false
342+
server1 := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
343+
server1HttpCalled = true
344+
return
345+
}))
346+
server1Addr := server1.Listener.Addr().String()
347+
348+
// http probe is empty,use tcp probe
349+
p := NewPool([]string{server1Addr})
350+
assert.NoError(t, p.doCheckConnectivity(server1Addr))
351+
assert.False(t, server1HttpCalled)
352+
353+
// http probe is not empty
354+
p = NewPool([]string{server1Addr}, Options{HttpProbeOptions: &HttpProbeOptions{
355+
Protocol: "http",
356+
Path: "/",
357+
}})
358+
assert.NoError(t, p.doCheckConnectivity(server1Addr))
359+
assert.True(t, server1HttpCalled)
360+
server1.Close()
361+
assert.Error(t, p.doCheckConnectivity(server1Addr))
362+
363+
// http probe got 404,tcp probe again
364+
server1HttpCalled = false
365+
mux := http.NewServeMux()
366+
mux.HandleFunc("/test", func(writer http.ResponseWriter, request *http.Request) {
367+
server1HttpCalled = true
368+
return
369+
})
370+
server1 = httptest.NewServer(mux)
371+
server1Addr = server1.Listener.Addr().String()
372+
p = NewPool([]string{server1Addr}, Options{HttpProbeOptions: &HttpProbeOptions{
373+
Protocol: "http",
374+
Path: "/", // wrong path, got 404
375+
}})
376+
assert.NoError(t, p.doCheckConnectivity(server1Addr))
377+
assert.False(t, server1HttpCalled)
378+
p.httpProbeOptions.Path = "/test" // right path
379+
assert.NoError(t, p.doCheckConnectivity(server1Addr))
380+
assert.True(t, server1HttpCalled)
381+
server1.Close()
382+
383+
// https probe
384+
server1HttpCalled = false
385+
server1 = httptest.NewTLSServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
386+
server1HttpCalled = true
387+
return
388+
}))
389+
server1Addr = server1.Listener.Addr().String()
390+
p = NewPool([]string{server1Addr}, Options{HttpProbeOptions: &HttpProbeOptions{
391+
Protocol: "https",
392+
Path: "/", // wrong path, got 404
393+
}})
394+
assert.NoError(t, p.doCheckConnectivity(server1Addr))
395+
assert.True(t, server1HttpCalled)
396+
server1.Close()
397+
}

0 commit comments

Comments
 (0)