Skip to content
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions pkg/feeds/factory/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2026 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package factory

import "github.com/ethersphere/bee/v2/pkg/feeds"

func WrapLookup(f feeds.Factory, t feeds.Type, lookup feeds.Lookup) feeds.Lookup {
return f.(*factory).wrapLookup(t, lookup)
}
69 changes: 65 additions & 4 deletions pkg/feeds/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,88 @@
package factory

import (
"context"
"errors"
"time"

"github.com/ethersphere/bee/v2/pkg/feeds"
"github.com/ethersphere/bee/v2/pkg/feeds/epochs"
"github.com/ethersphere/bee/v2/pkg/feeds/sequence"
m "github.com/ethersphere/bee/v2/pkg/metrics"
storage "github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/swarm"
)

var _ m.Collector = (*factory)(nil)

type factory struct {
storage.Getter
metrics metrics
}

func New(getter storage.Getter) feeds.Factory {
return &factory{getter}
return &factory{
Getter: getter,
metrics: newMetrics(),
}
}

func (f *factory) NewLookup(t feeds.Type, feed *feeds.Feed) (feeds.Lookup, error) {
var lookup feeds.Lookup
switch t {
case feeds.Sequence:
return sequence.NewAsyncFinder(f.Getter, feed), nil
lookup = sequence.NewAsyncFinder(f.Getter, feed)
case feeds.Epoch:
return epochs.NewAsyncFinder(f.Getter, feed), nil
lookup = epochs.NewAsyncFinder(f.Getter, feed)
default:
return nil, feeds.ErrFeedTypeNotFound
}

return f.wrapLookup(t, lookup), nil
}

func (f *factory) wrapLookup(t feeds.Type, lookup feeds.Lookup) feeds.Lookup {
return &instrumentedLookup{
lookup: lookup,
typ: lookupType(t),
m: f.metrics,
}
}

type instrumentedLookup struct {
lookup feeds.Lookup
typ string
m metrics
}

return nil, feeds.ErrFeedTypeNotFound
func (l *instrumentedLookup) At(ctx context.Context, at int64, after uint64) (swarm.Chunk, feeds.Index, feeds.Index, error) {
l.m.LookupStarted.WithLabelValues(l.typ).Inc()
start := time.Now()
ch, cur, next, err := l.lookup.At(ctx, at, after)
l.m.LookupDuration.WithLabelValues(l.typ, lookupResult(ctx, ch, err)).Observe(time.Since(start).Seconds())
return ch, cur, next, err
}

func lookupType(t feeds.Type) string {
switch t {
case feeds.Sequence:
return "sequence"
case feeds.Epoch:
return "epoch"
default:
return "unknown"
}
}

func lookupResult(ctx context.Context, ch swarm.Chunk, err error) string {
switch {
case errors.Is(ctx.Err(), context.Canceled) || errors.Is(err, context.Canceled):
return "canceled"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the canceled label actually fire for sequence feeds?
If the client disconnects after the first chunk is fetched, sequence.asyncFinder turns the cancel into "not found" and returns the last chunk it found with err == nil, so the lookup is recorded as found.
Would checking ctx.Err() != nil first in lookupResult be more reliable?

case err != nil || ctx.Err() != nil:
return "error"
case ch == nil:
return "not_found"
default:
return "found"
}
}
Loading
Loading