Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add debug log for context stack #29

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 65 additions & 34 deletions util/ctxutil/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,94 @@ import (
"encoding/json"
"runtime/debug"
"sync"
"time"

"github.com/eluv-io/common-go/util/goutil"
"github.com/eluv-io/common-go/util/traceutil/trace"
)

// NewStack creates a new ContextStack.
func NewStack() ContextStack {
return &contextStack{stacks: map[int64]*entry{}}
c := &contextStack{stacks: map[int64]*entry{}}
go func() {
for {
// Log stack info every 5 min for debugging
time.Sleep(time.Minute * 5)
c.mutex.Lock()
size := len(c.stacks)
depths := map[int64]int{}
maxDepth := 0
for gid, entry := range c.stacks {
depth := stackDepth(entry.stack)
depths[gid] = depth
if depth > maxDepth {
maxDepth = depth
}
}
c.mutex.Unlock()
log.Warn("ContextStack", "size", size, "max_depth", maxDepth, "depths", depths)
}
}()
return c
}

func stackDepth(s *stack) int {
depth := 1
for s.parent != nil {
s = s.parent
depth++
}
return depth
}

// ContextStack provides access to a stack of context.Context values individually managed per goroutine. It is
// essentially a thead-local implementation that offers an alternative to passing context object in each call along a
// call chain.
//
// The "standard" way of using contexts in go is passing it to each function in a call chain:
// type anObject struct {}
//
// func (r *anObject) A(ctx context.Context) {
// ctx = context.WithValue(context.Background(), "key", "val")
// r.B(ctx)
// ...
// }
// type anObject struct {}
//
// func (r *anObject) A(ctx context.Context) {
// ctx = context.WithValue(context.Background(), "key", "val")
// r.B(ctx)
// ...
// }
//
// func (r *anObject) B(ctx context.Context) {
// ...
// r.C(ctx)
// ...
// }
// func (r *anObject) B(ctx context.Context) {
// ...
// r.C(ctx)
// ...
// }
//
// func (r *anObject) C(ctx context.Context) string {
// val := ctx.Value("key")
// ...
// }
// func (r *anObject) C(ctx context.Context) string {
// val := ctx.Value("key")
// ...
// }
//
// ContextStack achieves the same without adding a context to each method call:
//
// type anObject struct {
// cs *ctxutil.ContextStack
// }
// type anObject struct {
// cs *ctxutil.ContextStack
// }
//
// func (r *anObject) A() string {
// release := r.cs.WithValue("key", "val")
// defer release()
// r.B()
// ...
// }
// func (r *anObject) A() string {
// release := r.cs.WithValue("key", "val")
// defer release()
// r.B()
// ...
// }
//
// func (r *anObject) B() string {
// ...
// r.C()
// ...
// }
// func (r *anObject) B() string {
// ...
// r.C()
// ...
// }
//
// func (r *anObject) C() string {
// val := r.Ctx().Value("key")
// ...
// }
// func (r *anObject) C() string {
// val := r.Ctx().Value("key")
// ...
// }
type ContextStack interface {
// Ctx retrieves the current context for the current goroutine.
Ctx() context.Context
Expand Down
Loading