text
stringlengths 0
14.1k
|
---|
// NewIndexSubscriber creates a new index subscriber. It also starts the |
// handler for incoming index update subscriptions. |
func NewIndexSubscriber(sCtx context.Context) *IndexSubscriber { |
ctx, cancel := context.WithCancel(sCtx) |
s := &IndexSubscriber{ |
c: make(chan IndexNtfn, bufferSize), |
subscriptions: make(map[string]*IndexSubscription), |
ctx: ctx, |
cancel: cancel, |
quit: make(chan struct{}), |
} |
return s |
} |
// Subscribe subscribes an index for updates. The returned index subscription |
// has functions to retrieve a channel that produces a stream of index updates |
// and to stop the stream when the caller no longer wishes to receive updates. |
func (s *IndexSubscriber) Subscribe(index Indexer, prerequisite string) (*IndexSubscription, error) { |
sub := newIndexSubscription(s, index, prerequisite) |
// If the subscription has a prequisite, find it and set the subscription |
// as a dependency. |
if prerequisite != noPrereqs { |
s.mtx.Lock() |
prereq, ok := s.subscriptions[prerequisite] |
s.mtx.Unlock() |
if !ok { |
return nil, fmt.Errorf(""no subscription found with id %s"", prerequisite) |
} |
prereq.mtx.Lock() |
defer prereq.mtx.Unlock() |
if prereq.dependent != nil { |
return nil, fmt.Errorf(""%s already has a dependent set: %s"", |
prereq.id, prereq.dependent.id) |
} |
prereq.dependent = sub |
atomic.AddUint32(&s.subscribers, 1) |
return sub, nil |
} |
// If the subscription does not have a prerequisite, add it to the index |
// subscriber's subscriptions. |
s.mtx.Lock() |
s.subscriptions[sub.id] = sub |
s.mtx.Unlock() |
atomic.AddUint32(&s.subscribers, 1) |
return sub, nil |
} |
// Notify relays an index notification to subscribed indexes for processing. |
func (s *IndexSubscriber) Notify(ntfn *IndexNtfn) { |
subscribers := atomic.LoadUint32(&s.subscribers) |
// Only relay notifications when there are subscribed indexes |
// to be notified. |
if subscribers > 0 { |
select { |
case <-s.quit: |
case s.c <- *ntfn: |
} |
} |
} |
// findLowestIndexTipHeight determines the lowest index tip height among |
// subscribed indexes and their dependencies. |
func (s *IndexSubscriber) findLowestIndexTipHeight(queryer ChainQueryer) (int64, int64, error) { |
// Find the lowest tip height to catch up among subscribed indexes. |
bestHeight, _ := queryer.Best() |
lowestHeight := bestHeight |
for _, sub := range s.subscriptions { |
tipHeight, tipHash, err := sub.idx.Tip() |
if err != nil { |
return 0, bestHeight, err |
} |
// Ensure the index tip is on the main chain. |
if !queryer.MainChainHasBlock(tipHash) { |
return 0, bestHeight, fmt.Errorf(""%s: index tip (%s) is not on the ""+ |
""main chain"", sub.idx.Name(), tipHash) |
} |
if tipHeight < lowestHeight { |
lowestHeight = tipHeight |
} |
// Update the lowest tip height if a dependent has a lower tip height. |
dependent := sub.dependent |
for dependent != nil { |
tipHeight, _, err := sub.dependent.idx.Tip() |
if err != nil { |
return 0, bestHeight, err |
} |
if tipHeight < lowestHeight { |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.