diff --git a/define.go b/define.go index 466ef40..1d8407e 100644 --- a/define.go +++ b/define.go @@ -38,7 +38,6 @@ type Api struct { SuccessCodeList []string `json:"success_code_list" yaml:"success_code_list"` // 成功的值(优先级高, 会覆盖) SuccessHttpCodeList []int `json:"success_http_code_list" yaml:"success_http_code_list"` // 任务成功的http状态码, 不配置默认只有200(优先级高, 会覆盖) Header map[string]string `json:"header" yaml:"header"` // 传入的header - Cookie map[string]string `json:"cookie" yaml:"cookie"` // 传入的cookie BindURIParamList []string `json:"bind_uri_param_list" yaml:"bind_uri_param_list"` // 绑定到URI的参数列表 Timeout ApiTimeout `json:"timeout" yaml:"timeout"` Retry ApiRetry `json:"retry" yaml:"retry"` // 重试策略 @@ -67,3 +66,10 @@ type ApiRetry struct { Count int `json:"count" yaml:"count"` // 重试次数 Frequency int `json:"frequency" yaml:"frequency"` // 重试频率, 单位 : ms } + +const ( + // DefaultConnectTimeout 默认连接超时 + DefaultConnectTimeout = 100 + // DefaultReadTimeout 默认读取超时 + DefaultReadTimeout = 300 +) diff --git a/go.mod b/go.mod index 4ea92fb..d25608b 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module git.zhangdeman.cn/zhangdeman/rpc go 1.17 -require go.uber.org/zap v1.21.0 +require ( + github.com/ddliu/go-httpclient v0.6.9 + go.uber.org/zap v1.21.0 +) require ( go.uber.org/atomic v1.9.0 // indirect diff --git a/go.sum b/go.sum index e1e1582..f5f230d 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/ddliu/go-httpclient v0.6.9 h1:/3hsBVpcgCJwqm1dkVlnAJ9NWuYInbRc+i9FyUXX/LE= +github.com/ddliu/go-httpclient v0.6.9/go.mod h1:zM9P0OxV4OGGz1pt/ibuj0ooX2SWH9a6MvXZLbT0JMc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= diff --git a/rpc.go b/rpc.go index 4251ff4..66899c5 100644 --- a/rpc.go +++ b/rpc.go @@ -10,7 +10,9 @@ package rpc import ( "errors" "sync" + "time" + httpclient "github.com/ddliu/go-httpclient" "go.uber.org/zap" ) @@ -249,3 +251,23 @@ func (r *request) Connect() error { func (r *request) Send() error { return nil } + +// GetHttpClient 获取client实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:00 2022/6/30 +func (r *request) GetHttpClient(header map[string]string, timeout ApiTimeout) *httpclient.HttpClient { + client := httpclient.NewHttpClient() + if timeout.Connect <= 0 { + timeout.Connect = DefaultConnectTimeout + } + + if timeout.Read <= 0 { + timeout.Read = DefaultReadTimeout + } + client.WithHeaders(header) + client.WithOption(httpclient.OPT_CONNECTTIMEOUT_MS, time.Duration(timeout.Connect)*time.Millisecond) + client.WithOption(httpclient.OPT_TIMEOUT_MS, time.Duration(timeout.Read)*time.Millisecond) + return client +}