Skip to content

Commit

Permalink
Merge pull request #255 from sevennt/feature/intcp-fix
Browse files Browse the repository at this point in the history
fix ehttp interceptor bug in multi ehttp instance
  • Loading branch information
sevennt authored Mar 7, 2022
2 parents 3041392 + a29c5d8 commit e3a0677
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
4 changes: 1 addition & 3 deletions client/ehttp/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ type Component struct {
func newComponent(name string, config *Config, logger *elog.Component) *Component {
// resty的默认方法,无法设置长连接个数,和是否开启长连接,这里重新构造http client。
cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
var interceptors []func(name string, cfg *Config, logger *elog.Component) (resty.RequestMiddleware, resty.ResponseMiddleware, resty.ErrorHook)
interceptors = append(interceptors, fixedInterceptor, logInterceptor, metricInterceptor, traceInterceptor)

interceptors := []interceptor{fixedInterceptor, logInterceptor, metricInterceptor}
cli := resty.NewWithClient(&http.Client{Transport: createTransport(config), Jar: cookieJar}).
SetDebug(config.RawDebug).
SetTimeout(config.ReadTimeout).
Expand Down
7 changes: 3 additions & 4 deletions client/ehttp/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import (
"github.com/gotomicro/ego/core/util/xdebug"
)

type interceptor func(name string, cfg *Config, logger *elog.Component) (resty.RequestMiddleware, resty.ResponseMiddleware, resty.ErrorHook)

func logAccess(name string, config *Config, logger *elog.Component, req *resty.Request, res *resty.Response, err error) {
rr := req.RawRequest
var (
url string
host string
)
var url, host string
// 修复err 不是 *resty.ResponseError错误的时候,可能为nil
if rr != nil {
url = rr.URL.RequestURI()
Expand Down
34 changes: 24 additions & 10 deletions client/ehttp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,10 @@ func WithSlowLogThreshold(slowLogThreshold time.Duration) Option {
}
}

// WithEnableAccessInterceptor 设置开启请求日志
func WithEnableAccessInterceptor(enableAccessInterceptor bool) Option {
return func(c *Container) {
c.config.EnableAccessInterceptor = enableAccessInterceptor
}
}

// WithEnableAccessInterceptorRes 设置开启请求日志响应参数
func WithEnableAccessInterceptorRes(enableAccessInterceptorRes bool) Option {
// WithIdleConnTimeout 设置空闲连接时间
func WithIdleConnTimeout(idleConnTimeout time.Duration) Option {
return func(c *Container) {
c.config.EnableAccessInterceptorRes = enableAccessInterceptorRes
c.config.IdleConnTimeout = idleConnTimeout
}
}

Expand All @@ -65,9 +58,30 @@ func WithMaxIdleConnsPerHost(maxIdleConnsPerHost int) Option {
}
}

// WithEnableTraceInterceptor 设置开启Trace拦截器
func WithEnableTraceInterceptor(enableTraceInterceptor bool) Option {
return func(c *Container) {
c.config.EnableTraceInterceptor = enableTraceInterceptor
}
}

// WithEnableKeepAlives 设置是否开启长连接,默认打开
func WithEnableKeepAlives(enableKeepAlives bool) Option {
return func(c *Container) {
c.config.EnableKeepAlives = enableKeepAlives
}
}

// WithEnableAccessInterceptor 设置开启请求日志
func WithEnableAccessInterceptor(enableAccessInterceptor bool) Option {
return func(c *Container) {
c.config.EnableAccessInterceptor = enableAccessInterceptor
}
}

// WithEnableAccessInterceptorRes 设置开启请求日志响应参数
func WithEnableAccessInterceptorRes(enableAccessInterceptorRes bool) Option {
return func(c *Container) {
c.config.EnableAccessInterceptorRes = enableAccessInterceptorRes
}
}
4 changes: 2 additions & 2 deletions core/util/xdebug/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

// MakeReqResInfo ...
// Deprecated: MakeReqResInfo ...
// Deprecated: MakeReqResInfo will be removed in v0.10
func MakeReqResInfo(compName string, addr string, cost time.Duration, req interface{}, reply interface{}) string {
return fmt.Sprintf("%s %s %s %s => %s\n", xcolor.Green(compName), xcolor.Green(addr), xcolor.Yellow(fmt.Sprintf("[%vms]", float64(cost.Microseconds())/1000)), xcolor.Blue(fmt.Sprintf("%v", req)), xcolor.Blue(fmt.Sprintf("%v", reply)))
}

// MakeReqResError ...
// Deprecated: MakeReqResError 以error级别打印配置名、目标地址、耗时、请求数据、响应数据
// Deprecated: MakeReqResError will be removed in v0.10
func MakeReqResError(compName string, addr string, cost time.Duration, req string, err string) string {
return fmt.Sprintf("%s %s %s %s => %s\n", xcolor.Red(compName), xcolor.Red(addr), xcolor.Yellow(fmt.Sprintf("[%vms]", float64(cost.Microseconds())/1000)), xcolor.Blue(fmt.Sprintf("%v", req)), xcolor.Red(err))
}
Expand Down

0 comments on commit e3a0677

Please sign in to comment.