trying to prevent leaking subscriptions.

This commit is contained in:
fiatjaf
2026-02-26 23:01:18 -03:00
parent 00ffe16cb7
commit c31b92707b
5 changed files with 25 additions and 12 deletions
+9 -5
View File
@@ -68,7 +68,7 @@ func NewBatchedLoader[K comparable, V any](batchFn BatchFunc[K, V], opts Options
// The first context passed to this function within a given batch window will be provided to
// the registered BatchFunc.
func (l *Loader[K, V]) Load(ctx context.Context, key K) (value V, err error) {
c := make(chan Result[V])
c := make(chan Result[V], 1)
// this is sent to batch fn. It contains the key and the channel to return
// the result on
@@ -117,11 +117,15 @@ func (l *Loader[K, V]) Load(ctx context.Context, key K) (value V, err error) {
l.batchLock.Unlock()
if v, ok := <-c; ok {
return v.Data, v.Error
select {
case v, ok := <-c:
if ok {
return v.Data, v.Error
}
return value, NoValueError
case <-ctx.Done():
return value, ctx.Err()
}
return value, NoValueError
}
type batcher[K comparable, V any] struct {