mirror of
https://github.com/duanhf2012/origin.git
synced 2026-03-05 21:57:33 +08:00
http模块
This commit is contained in:
@@ -1,19 +1,96 @@
|
|||||||
package sysmodule
|
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 {
|
type HttpClientPoolModule struct {
|
||||||
service.BaseModule
|
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) {
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
29
sysmodule/HttpClientPoolModule_test.go
Normal file
29
sysmodule/HttpClientPoolModule_test.go
Normal file
@@ -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))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user