Skip to content

Commit

Permalink
debug: mw cost
Browse files Browse the repository at this point in the history
  • Loading branch information
ppzqh committed Aug 15, 2024
1 parent d4e163a commit cd42b4b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions client/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ func newProxyMW(prx proxy.ForwardProxy) endpoint.Middleware {
}
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request, response interface{}) error {
klog.CtxInfof(ctx, "[DEBUG] newProxyMW start, time=%s", time.Now())
err := prx.ResolveProxyInstance(ctx)
if err != nil {
klog.Info("[DEBUG] newProxyMW finish, time=%s", time.Now())
return err
}
klog.CtxInfof(ctx, "[DEBUG] newProxyMW finish, time=%s", time.Now())
err = next(ctx, request, response)
return err
}
Expand Down Expand Up @@ -85,6 +88,7 @@ func newResolveMWBuilder(lbf *lbcache.BalancerFactory) endpoint.MiddlewareBuilde
return func(ctx context.Context) endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request, response interface{}) error {
klog.CtxInfof(ctx, "[DEBUG] resolveMW start, time=%s", time.Now())
rpcInfo := rpcinfo.GetRPCInfo(ctx)

dest := rpcInfo.To()
Expand Down Expand Up @@ -120,6 +124,7 @@ func newResolveMWBuilder(lbf *lbcache.BalancerFactory) endpoint.MiddlewareBuilde
if ins == nil {
err = kerrors.ErrNoMoreInstance.WithCause(fmt.Errorf("last error: %w", lastErr))
} else {
klog.CtxInfof(ctx, "[DEBUG] resolveMW finish1, time=%s", time.Now())
remote.SetInstance(ins)
// TODO: generalize retry strategy
err = next(ctx, request, response)
Expand Down
2 changes: 2 additions & 0 deletions client/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func WithMiddleware(mw endpoint.Middleware) Option {
return mw
}
return Option{F: func(o *client.Options, di *utils.Slice) {
klog.Infof("[DEBUG] configure mw, name=%s", utils.GetFuncName(mw))
di.Push(fmt.Sprintf("WithMiddleware(%+v)", utils.GetFuncName(mw)))
o.MWBs = append(o.MWBs, mwb)
}}
Expand All @@ -100,6 +101,7 @@ func WithMiddleware(mw endpoint.Middleware) Option {
// WithMiddlewareBuilder adds middleware that depend on context for client to handle request
func WithMiddlewareBuilder(mwb endpoint.MiddlewareBuilder) Option {
return Option{F: func(o *client.Options, di *utils.Slice) {
klog.Infof("[DEBUG] configure mwbuilder, name=%s", utils.GetFuncName(mwb))
di.Push(fmt.Sprintf("WithMiddlewareBuilder(%+v)", utils.GetFuncName(mwb)))
o.MWBs = append(o.MWBs, mwb)
}}
Expand Down
1 change: 1 addition & 0 deletions client/rpctimeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func rpcTimeoutMW(mwCtx context.Context) endpoint.Middleware {

return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request, response interface{}) error {
klog.CtxInfof(ctx, "[DEBUG] rpcTimeoutMW start, time=%s", time.Now())
ri := rpcinfo.GetRPCInfo(ctx)
if ri.Config().InteractionMode() == rpcinfo.Streaming {
return next(ctx, request, response)
Expand Down
3 changes: 2 additions & 1 deletion internal/mocks/proxy/proxy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/circuitbreak/circuitbreak.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package circuitbreak
import (
"context"
"errors"
"github.com/cloudwego/kitex/pkg/klog"
"time"

"github.com/bytedance/gopkg/cloud/circuitbreaker"

Expand Down Expand Up @@ -78,15 +80,19 @@ type Control struct {
func NewCircuitBreakerMW(control Control, panel circuitbreaker.Panel) endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request, response interface{}) (err error) {
klog.CtxInfof(ctx, "[DEBUG] NewCircuitBreakerMW start, time=%s", time.Now())
key, enabled := control.GetKey(ctx, request)
if !enabled {
klog.CtxInfof(ctx, "[DEBUG] NewCircuitBreakerMW finish1, time=%s", time.Now())
return next(ctx, request, response)
}

if !panel.IsAllowed(key) {
klog.CtxInfof(ctx, "[DEBUG] NewCircuitBreakerMW finish2, time=%s", time.Now())
return control.DecorateError(ctx, request, kerrors.ErrCircuitBreak)
}

klog.CtxInfof(ctx, "[DEBUG] NewCircuitBreakerMW finish3, time=%s", time.Now())
err = next(ctx, request, response)
RecordStat(ctx, request, response, err, key, &control, panel)
return
Expand Down

0 comments on commit cd42b4b

Please sign in to comment.