Skip to content

Commit 905ee9a

Browse files
fix: global callbacks repeated in AppendHandlers (#26)
1 parent 5c77ce1 commit 905ee9a

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

callbacks/aspect_inject_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/stretchr/testify/assert"
2727

28+
"github.com/cloudwego/eino/internal/callbacks"
2829
"github.com/cloudwego/eino/schema"
2930
)
3031

@@ -181,3 +182,19 @@ func TestAspectInject(t *testing.T) {
181182
assert.Equal(t, 186, cnt)
182183
})
183184
}
185+
186+
func TestGlobalCallbacksRepeated(t *testing.T) {
187+
times := 0
188+
testHandler := NewHandlerBuilder().OnStartFn(func(ctx context.Context, info *callbacks.RunInfo, input callbacks.CallbackInput) context.Context {
189+
times++
190+
return ctx
191+
}).Build()
192+
callbacks.GlobalHandlers = append(callbacks.GlobalHandlers, testHandler)
193+
194+
ctx := context.Background()
195+
ctx = callbacks.AppendHandlers(ctx, &RunInfo{})
196+
ctx = callbacks.AppendHandlers(ctx, &RunInfo{})
197+
198+
callbacks.On(ctx, "test", callbacks.OnStartHandle[string], TimingOnStart)
199+
assert.Equal(t, times, 1)
200+
}

internal/callbacks/inject.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func On[T any](ctx context.Context, inOut T, handle Handle[T], timing CallbackTi
5959
return ctx, inOut
6060
}
6161

62-
hs := make([]Handler, 0, len(mgr.handlers))
63-
for _, handler := range mgr.handlers {
62+
hs := make([]Handler, 0, len(mgr.handlers)+len(mgr.globalHandlers))
63+
for _, handler := range append(mgr.handlers, mgr.globalHandlers...) {
6464
timingChecker, ok_ := handler.(TimingChecker)
6565
if !ok_ || timingChecker.Needed(ctx, mgr.runInfo, timing) {
6666
hs = append(hs, handler)

internal/callbacks/manager.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,25 @@ package callbacks
1919
import "context"
2020

2121
type manager struct {
22-
handlers []Handler
23-
runInfo *RunInfo
22+
globalHandlers []Handler
23+
handlers []Handler
24+
runInfo *RunInfo
2425
}
2526

2627
var GlobalHandlers []Handler
2728

2829
func newManager(runInfo *RunInfo, handlers ...Handler) (*manager, bool) {
29-
l := len(handlers) + len(GlobalHandlers)
30-
if l == 0 {
30+
if len(handlers)+len(GlobalHandlers) == 0 {
3131
return nil, false
3232
}
33-
hs := make([]Handler, 0, l)
34-
hs = append(hs, GlobalHandlers...)
35-
hs = append(hs, handlers...)
33+
34+
hs := make([]Handler, len(GlobalHandlers))
35+
copy(hs, GlobalHandlers)
3636

3737
return &manager{
38-
handlers: hs,
39-
runInfo: runInfo,
38+
globalHandlers: hs,
39+
handlers: handlers,
40+
runInfo: runInfo,
4041
}, true
4142
}
4243

@@ -50,8 +51,9 @@ func (m *manager) withRunInfo(runInfo *RunInfo) *manager {
5051
}
5152

5253
return &manager{
53-
handlers: m.handlers,
54-
runInfo: runInfo,
54+
globalHandlers: m.globalHandlers,
55+
handlers: m.handlers,
56+
runInfo: runInfo,
5557
}
5658
}
5759

@@ -60,8 +62,9 @@ func managerFromCtx(ctx context.Context) (*manager, bool) {
6062
m, ok := v.(*manager)
6163
if ok && m != nil {
6264
return &manager{
63-
handlers: m.handlers,
64-
runInfo: m.runInfo,
65+
globalHandlers: m.globalHandlers,
66+
handlers: m.handlers,
67+
runInfo: m.runInfo,
6568
}, true
6669
}
6770

0 commit comments

Comments
 (0)