swarm/storage/feed: make handler read count atomic

This commit is contained in:
Javier Peletier 2019-04-04 16:19:01 +02:00
parent 4981a9a1ca
commit f87cc006bc

View file

@ -23,6 +23,7 @@ import (
"context" "context"
"fmt" "fmt"
"sync" "sync"
"sync/atomic"
"github.com/ethereum/go-ethereum/swarm/chunk" "github.com/ethereum/go-ethereum/swarm/chunk"
@ -178,12 +179,13 @@ func (h *Handler) Lookup(ctx context.Context, query *Query) (*cacheEntry, error)
return nil, NewError(ErrInit, "Call Handler.SetStore() before performing lookups") return nil, NewError(ErrInit, "Call Handler.SetStore() before performing lookups")
} }
var readCount int var readCount int32
// Invoke the lookup engine. // Invoke the lookup engine.
// The callback will be called every time the lookup algorithm needs to guess // The callback will be called every time the lookup algorithm needs to guess
requestPtr, err := lookup.Lookup(ctx, timeLimit, query.Hint, func(ctx context.Context, epoch lookup.Epoch, now uint64) (interface{}, error) { requestPtr, err := lookup.Lookup(ctx, timeLimit, query.Hint, func(ctx context.Context, epoch lookup.Epoch, now uint64) (interface{}, error) {
readCount++ fmt.Printf("Epoch query: %s\n", epoch.String())
atomic.AddInt32(&readCount, 1)
id := ID{ id := ID{
Feed: query.Feed, Feed: query.Feed,
Epoch: epoch, Epoch: epoch,
@ -228,17 +230,17 @@ func (h *Handler) updateCache(request *Request) (*cacheEntry, error) {
updateAddr := request.Addr() updateAddr := request.Addr()
log.Trace("feed cache update", "topic", request.Topic.Hex(), "updateaddr", updateAddr, "epoch time", request.Epoch.Time, "epoch level", request.Epoch.Level) log.Trace("feed cache update", "topic", request.Topic.Hex(), "updateaddr", updateAddr, "epoch time", request.Epoch.Time, "epoch level", request.Epoch.Level)
feedUpdate := h.get(&request.Feed) entry := h.get(&request.Feed)
if feedUpdate == nil { if entry == nil {
feedUpdate = &cacheEntry{} entry = &cacheEntry{}
h.set(&request.Feed, feedUpdate) h.set(&request.Feed, entry)
} }
// update our rsrcs entry map // update our rsrcs entry map
feedUpdate.lastKey = updateAddr entry.lastKey = updateAddr
feedUpdate.Update = request.Update entry.Update = request.Update
feedUpdate.Reader = bytes.NewReader(feedUpdate.data) entry.Reader = bytes.NewReader(entry.data)
return feedUpdate, nil return entry, nil
} }
// Update publishes a feed update // Update publishes a feed update