Skip to content

Commit

Permalink
optimize: trace doFinish multiple times (#989)
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF authored Nov 7, 2023
1 parent f176f53 commit 66f5338
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
9 changes: 4 additions & 5 deletions pkg/protocol/http1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,6 @@ func (s Server) Serve(c context.Context, conn network.Conn) (err error) {
return
}

// general case
if s.EnableTrace {
traceCtl.DoFinish(cc, ctx, err)
}

if connectionClose {
return errShortConnection
}
Expand All @@ -382,6 +377,10 @@ func (s Server) Serve(c context.Context, conn network.Conn) (err error) {
if s.IdleTimeout == 0 {
return
}
// general case
if s.EnableTrace {
traceCtl.DoFinish(cc, ctx, err)
}

ctx.ResetWithoutConn()
}
Expand Down
46 changes: 46 additions & 0 deletions pkg/protocol/http1/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,52 @@ func TestExpect100ContinueHandler(t *testing.T) {
assert.DeepEqual(t, "", string(response.Body()))
}

type mockController struct {
FinishTimes int
}

func (m *mockController) Append(col tracer.Tracer) {}

func (m *mockController) DoStart(ctx context.Context, c *app.RequestContext) context.Context {
return ctx
}

func (m *mockController) DoFinish(ctx context.Context, c *app.RequestContext, err error) {
m.FinishTimes++
}

func (m *mockController) HasTracer() bool { return true }

func (m *mockController) reset() { m.FinishTimes = 0 }

func TestTraceDoFinishTimes(t *testing.T) {
server := &Server{}
server.eventStackPool = pool
server.EnableTrace = true
reqCtx := &app.RequestContext{}
controller := &mockController{}
server.Core = &mockCore{
ctxPool: &sync.Pool{New: func() interface{} {
ti := traceinfo.NewTraceInfo()
ti.Stats().SetLevel(2)
reqCtx.SetTraceInfo(&mockTraceInfo{ti})
return reqCtx
}},
controller: controller,
}
// for disableKeepAlive case
server.DisableKeepalive = true
err := server.Serve(context.TODO(), mock.NewConn("GET /aaa HTTP/1.1\nHost: foobar.com\n\n"))
assert.True(t, errors.Is(err, errs.ErrShortConnection))
assert.DeepEqual(t, 1, controller.FinishTimes)
// for IdleTimeout==0 case
server.IdleTimeout = 0
controller.reset()
err = server.Serve(context.TODO(), mock.NewConn("GET /aaa HTTP/1.1\nHost: foobar.com\n\n"))
assert.True(t, errors.Is(err, errs.ErrShortConnection))
assert.DeepEqual(t, 1, controller.FinishTimes)
}

type mockCore struct {
ctxPool *sync.Pool
controller tracer.Controller
Expand Down

0 comments on commit 66f5338

Please sign in to comment.