@@ -53,7 +53,7 @@ type dataLoader[K comparable, V any] struct {
53
53
config config
54
54
mu sync.Mutex
55
55
batch []K
56
- batchCtx [] context.Context
56
+ batchCtx map [ context.Context ] struct {}
57
57
chs map [K ][]chan Result [V ]
58
58
stopSchedule chan struct {}
59
59
}
@@ -167,7 +167,9 @@ func (d *dataLoader[K, V]) goLoad(ctx context.Context, key K) <-chan Result[V] {
167
167
// Lock the DataLoader
168
168
d .mu .Lock ()
169
169
if d .config .TracerProvider != nil {
170
- d .batchCtx = append (d .batchCtx , ctx )
170
+ if _ , ok := d .batchCtx [ctx ]; ! ok {
171
+ d .batchCtx [ctx ] = struct {}{}
172
+ }
171
173
}
172
174
173
175
if len (d .batch ) == 0 {
@@ -216,7 +218,7 @@ func (d *dataLoader[K, V]) scheduleBatch(ctx context.Context, stopSchedule <-cha
216
218
}
217
219
218
220
// processBatch processes a batch of keys
219
- func (d * dataLoader [K , V ]) processBatch (ctx context.Context , keys []K , batchCtx [] context.Context , chs map [K ][]chan Result [V ]) {
221
+ func (d * dataLoader [K , V ]) processBatch (ctx context.Context , keys []K , batchCtx map [ context.Context ] struct {} , chs map [K ][]chan Result [V ]) {
220
222
defer func () {
221
223
if r := recover (); r != nil {
222
224
buf := make ([]byte , 64 << 10 )
@@ -234,13 +236,8 @@ func (d *dataLoader[K, V]) processBatch(ctx context.Context, keys []K, batchCtx
234
236
// Create a span with links to the batch contexts, which enables trace propagation
235
237
// We should deduplicate identical batch contexts to avoid creating duplicate links.
236
238
links := make ([]trace.Link , 0 , len (keys ))
237
- seen := make (map [context.Context ]struct {}, len (batchCtx ))
238
- for _ , bCtx := range batchCtx {
239
- if _ , ok := seen [bCtx ]; ok {
240
- continue
241
- }
239
+ for bCtx := range batchCtx {
242
240
links = append (links , trace.Link {SpanContext : trace .SpanContextFromContext (bCtx )})
243
- seen [bCtx ] = struct {}{}
244
241
}
245
242
var span trace.Span
246
243
ctx , span = d .startTrace (ctx , "dataLoader.Batch" , trace .WithLinks (links ... ))
@@ -259,7 +256,7 @@ func (d *dataLoader[K, V]) processBatch(ctx context.Context, keys []K, batchCtx
259
256
// reset resets the DataLoader state
260
257
func (d * dataLoader [K , V ]) reset () {
261
258
d .batch = make ([]K , 0 , d .config .BatchSize )
262
- d .batchCtx = make ([] context.Context , 0 , d .config .BatchSize )
259
+ d .batchCtx = make (map [ context.Context ] struct {} , d .config .BatchSize )
263
260
d .chs = make (map [K ][]chan Result [V ], d .config .BatchSize )
264
261
}
265
262
0 commit comments