diff --git a/sysmodule/HttpClientPoolModule.go b/sysmodule/HttpClientPoolModule.go index cda6bfe..ddd2e37 100644 --- a/sysmodule/HttpClientPoolModule.go +++ b/sysmodule/HttpClientPoolModule.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net" "net/http" + "net/url" "time" "github.com/duanhf2012/origin/service" @@ -41,7 +42,15 @@ func (slf *SyncHttpRespone) Get(timeoutMs int) HttpRespone { } } -func (slf *HttpClientPoolModule) Init(maxpool int) { +func (slf *HttpClientPoolModule) Init(maxpool int, proxyUrl string) { + type ProxyFun func(_ *http.Request) (*url.URL, error) + var proxyfun ProxyFun + if proxyUrl != "" { + proxyfun = func(_ *http.Request) (*url.URL, error) { + return url.Parse(proxyUrl) + } + } + slf.client = &http.Client{ Transport: &http.Transport{ DialContext: (&net.Dialer{ @@ -51,22 +60,23 @@ func (slf *HttpClientPoolModule) Init(maxpool int) { MaxIdleConns: maxpool, MaxIdleConnsPerHost: maxpool, IdleConnTimeout: 60 * time.Second, + Proxy: proxyfun, }, } } -func (slf *HttpClientPoolModule) SyncRequest(method string, url string, body []byte) SyncHttpRespone { +func (slf *HttpClientPoolModule) SyncRequest(method string, url string, body []byte, header http.Header) SyncHttpRespone { ret := SyncHttpRespone{ resp: make(chan HttpRespone, 1), } go func() { - rsp := slf.Request(method, url, body) + rsp := slf.Request(method, url, body, header) ret.resp <- rsp }() return ret } -func (slf *HttpClientPoolModule) Request(method string, url string, body []byte) HttpRespone { +func (slf *HttpClientPoolModule) Request(method string, url string, body []byte, header http.Header) HttpRespone { if slf.client == nil { panic("Call the init function first") } @@ -76,6 +86,9 @@ func (slf *HttpClientPoolModule) Request(method string, url string, body []byte) ret.Err = err return ret } + if header != nil { + req.Header = header + } rsp, err := slf.client.Do(req) if err != nil { ret.Err = err diff --git a/sysmodule/HttpClientPoolModule_test.go b/sysmodule/HttpClientPoolModule_test.go index 61360f0..7089a04 100644 --- a/sysmodule/HttpClientPoolModule_test.go +++ b/sysmodule/HttpClientPoolModule_test.go @@ -12,14 +12,14 @@ func TestHttpClientPoolModule(t *testing.T) { c := sysmodule.HttpClientPoolModule{} c.Init(10) - rsp := c.Request(http.MethodGet, "https://www.baidu.com/", nil) + rsp := c.Request(http.MethodGet, "https://www.baidu.com/", nil, nil) fmt.Println(rsp.Err) fmt.Println(rsp.Header) fmt.Println(rsp.StatusCode) fmt.Println(rsp.Status) fmt.Println(string(rsp.Body)) - srsp := c.SyncRequest(http.MethodGet, "https://www.baidu.com/", nil) + srsp := c.SyncRequest(http.MethodGet, "https://www.baidu.com/", nil, nil) rsp1 := srsp.Get(1) fmt.Println(rsp1.Err) fmt.Println(rsp1.Header)