diff --git a/sysmodule/HttpClientPoolModule.go b/sysmodule/HttpClientPoolModule.go index a31f0d3..656c9b9 100644 --- a/sysmodule/HttpClientPoolModule.go +++ b/sysmodule/HttpClientPoolModule.go @@ -1,19 +1,96 @@ package sysmodule -import "github.com/duanhf2012/origin/service" +import ( + "bytes" + "fmt" + "io/ioutil" + "net" + "net/http" + "time" + + "github.com/duanhf2012/origin/service" +) type HttpClientPoolModule struct { service.BaseModule + client *http.Client +} + +type HttpRespone struct { + Err error + Header http.Header + StatusCode int + Status string + Body []byte +} + +type SyncHttpRespone struct { + resp chan *HttpRespone +} + +func (slf *SyncHttpRespone) Get(timeoutMs int) *HttpRespone { + timerC := time.NewTicker(time.Millisecond * time.Duration(timeoutMs)).C + select { + case <-timerC: + break + case rsp := <-slf.resp: + return rsp + } + return &HttpRespone{ + Err: fmt.Errorf("Getting the return result timeout [%d]ms", timeoutMs), + } } func (slf *HttpClientPoolModule) Init(maxpool int) { - + slf.client = &http.Client{ + Transport: &http.Transport{ + DialContext: (&net.Dialer{ + Timeout: 5 * time.Second, + KeepAlive: 30 * time.Second, + }).DialContext, + MaxIdleConns: maxpool, + MaxIdleConnsPerHost: maxpool, + IdleConnTimeout: 60 * time.Second, + }, + } } -func (slf *HttpClientPoolModule) SyncRequest() { - +func (slf *HttpClientPoolModule) SyncRequest(method string, url string, body []byte) SyncHttpRespone { + ret := SyncHttpRespone{ + resp: make(chan *HttpRespone, 1), + } + go func() { + rsp := slf.Request(method, url, body) + ret.resp <- rsp + }() + return ret } -func (slf *HttpClientPoolModule) Request() { +func (slf *HttpClientPoolModule) Request(method string, url string, body []byte) *HttpRespone { + if slf.client == nil { + panic("Call the init function first") + } + ret := &HttpRespone{} + req, err := http.NewRequest(method, url, bytes.NewReader(body)) + if err != nil { + ret.Err = err + return ret + } + rsp, err := slf.client.Do(req) + if err != nil { + ret.Err = err + return ret + } + defer rsp.Body.Close() + ret.Body, err = ioutil.ReadAll(rsp.Body) + if err != nil { + ret.Err = err + return ret + } + ret.StatusCode = rsp.StatusCode + ret.Status = rsp.Status + ret.Header = rsp.Header + + return ret } diff --git a/sysmodule/HttpClientPoolModule_test.go b/sysmodule/HttpClientPoolModule_test.go new file mode 100644 index 0000000..61360f0 --- /dev/null +++ b/sysmodule/HttpClientPoolModule_test.go @@ -0,0 +1,29 @@ +package sysmodule_test + +import ( + "fmt" + "net/http" + "testing" + + "github.com/duanhf2012/origin/sysmodule" +) + +func TestHttpClientPoolModule(t *testing.T) { + c := sysmodule.HttpClientPoolModule{} + c.Init(10) + + rsp := c.Request(http.MethodGet, "https://www.baidu.com/", 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) + rsp1 := srsp.Get(1) + fmt.Println(rsp1.Err) + fmt.Println(rsp1.Header) + fmt.Println(rsp1.StatusCode) + fmt.Println(rsp1.Status) + fmt.Println(string(rsp1.Body)) +}