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

feat: optimize HandlerName && add setIndex #1031

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions pkg/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,18 +328,28 @@ func (ctx *RequestContext) GetIndex() int8 {
return ctx.index
}

func (ctx *RequestContext) SetIndex(index int8) {
li-jin-gou marked this conversation as resolved.
Show resolved Hide resolved
ctx.index = index
}

type HandlerFunc func(c context.Context, ctx *RequestContext)

// HandlersChain defines a HandlerFunc array.
type HandlersChain []HandlerFunc

var handlerNames = make(map[uintptr]string)

var handlerRWLock sync.RWMutex

func SetHandlerName(handler HandlerFunc, name string) {
FGYFFFF marked this conversation as resolved.
Show resolved Hide resolved
handlerRWLock.Lock()
li-jin-gou marked this conversation as resolved.
Show resolved Hide resolved
defer handlerRWLock.Unlock()
handlerNames[getFuncAddr(handler)] = name
}

func GetHandlerName(handler HandlerFunc) string {
handlerRWLock.RLock()
defer handlerRWLock.RUnlock()
return handlerNames[getFuncAddr(handler)]
}

Expand Down
25 changes: 22 additions & 3 deletions pkg/app/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1344,16 +1344,35 @@ func TestGetWriter(t *testing.T) {
func TestIndex(t *testing.T) {
ctx := NewContext(0)
ctx.ResetWithoutConn()
res := ctx.GetIndex()
exc := int8(-1)
res := ctx.GetIndex()
assert.DeepEqual(t, exc, res)
ctx.SetIndex(int8(1))
res = ctx.GetIndex()
exc = int8(1)
assert.DeepEqual(t, exc, res)
}

func TestHandlerName(t *testing.T) {
h := func(c context.Context, ctx *RequestContext) {}
SetHandlerName(h, "test")
SetHandlerName(h, "test1")
for i := 0; i < 50; i++ {
go func() {
name := GetHandlerName(h)
assert.DeepEqual(t, "test1", name)
}()
}

time.Sleep(time.Second)

go func() {
SetHandlerName(h, "test2")
}()

time.Sleep(time.Second)

name := GetHandlerName(h)
assert.DeepEqual(t, "test", name)
assert.DeepEqual(t, "test2", name)
}

func TestHijack(t *testing.T) {
Expand Down