@@ -155,7 +155,6 @@ func (m *Matcher) Start(ctx context.Context, begin, end uint64, results chan uin
155
155
session := & MatcherSession {
156
156
matcher : m ,
157
157
quit : make (chan struct {}),
158
- kill : make (chan struct {}),
159
158
ctx : ctx ,
160
159
}
161
160
for _ , scheduler := range m .schedulers {
@@ -386,10 +385,8 @@ func (m *Matcher) distributor(dist chan *request, session *MatcherSession) {
386
385
requests = make (map [uint ][]uint64 ) // Per-bit list of section requests, ordered by section number
387
386
unallocs = make (map [uint ]struct {}) // Bits with pending requests but not allocated to any retriever
388
387
retrievers chan chan uint // Waiting retrievers (toggled to nil if unallocs is empty)
389
- )
390
- var (
391
- allocs int // Number of active allocations to handle graceful shutdown requests
392
- shutdown = session .quit // Shutdown request channel, will gracefully wait for pending requests
388
+ allocs int // Number of active allocations to handle graceful shutdown requests
389
+ shutdown = session .quit // Shutdown request channel, will gracefully wait for pending requests
393
390
)
394
391
395
392
// assign is a helper method fo try to assign a pending bit an actively
@@ -409,15 +406,12 @@ func (m *Matcher) distributor(dist chan *request, session *MatcherSession) {
409
406
for {
410
407
select {
411
408
case <- shutdown :
412
- // Graceful shutdown requested, wait until all pending requests are honoured
409
+ // Shutdown requested. No more retrievers can be allocated,
410
+ // but we still need to wait until all pending requests have returned.
411
+ shutdown = nil
413
412
if allocs == 0 {
414
413
return
415
414
}
416
- shutdown = nil
417
-
418
- case <- session .kill :
419
- // Pending requests not honoured in time, hard terminate
420
- return
421
415
422
416
case req := <- dist :
423
417
// New retrieval request arrived to be distributed to some fetcher process
@@ -499,8 +493,9 @@ func (m *Matcher) distributor(dist chan *request, session *MatcherSession) {
499
493
assign (result .Bit )
500
494
}
501
495
}
502
- // If we're in the process of shutting down, terminate
503
- if allocs == 0 && shutdown == nil {
496
+
497
+ // End the session when all pending deliveries have arrived.
498
+ if shutdown == nil && allocs == 0 {
504
499
return
505
500
}
506
501
}
@@ -514,7 +509,6 @@ type MatcherSession struct {
514
509
515
510
closer sync.Once // Sync object to ensure we only ever close once
516
511
quit chan struct {} // Quit channel to request pipeline termination
517
- kill chan struct {} // Term channel to signal non-graceful forced shutdown
518
512
519
513
ctx context.Context // Context used by the light client to abort filtering
520
514
err atomic.Value // Global error to track retrieval failures deep in the chain
@@ -529,7 +523,6 @@ func (s *MatcherSession) Close() {
529
523
s .closer .Do (func () {
530
524
// Signal termination and wait for all goroutines to tear down
531
525
close (s .quit )
532
- time .AfterFunc (time .Second , func () { close (s .kill ) })
533
526
s .pend .Wait ()
534
527
})
535
528
}
@@ -542,10 +535,10 @@ func (s *MatcherSession) Error() error {
542
535
return nil
543
536
}
544
537
545
- // AllocateRetrieval assigns a bloom bit index to a client process that can either
538
+ // allocateRetrieval assigns a bloom bit index to a client process that can either
546
539
// immediately request and fetch the section contents assigned to this bit or wait
547
540
// a little while for more sections to be requested.
548
- func (s * MatcherSession ) AllocateRetrieval () (uint , bool ) {
541
+ func (s * MatcherSession ) allocateRetrieval () (uint , bool ) {
549
542
fetcher := make (chan uint )
550
543
551
544
select {
@@ -557,9 +550,9 @@ func (s *MatcherSession) AllocateRetrieval() (uint, bool) {
557
550
}
558
551
}
559
552
560
- // PendingSections returns the number of pending section retrievals belonging to
553
+ // pendingSections returns the number of pending section retrievals belonging to
561
554
// the given bloom bit index.
562
- func (s * MatcherSession ) PendingSections (bit uint ) int {
555
+ func (s * MatcherSession ) pendingSections (bit uint ) int {
563
556
fetcher := make (chan uint )
564
557
565
558
select {
@@ -571,9 +564,9 @@ func (s *MatcherSession) PendingSections(bit uint) int {
571
564
}
572
565
}
573
566
574
- // AllocateSections assigns all or part of an already allocated bit-task queue
567
+ // allocateSections assigns all or part of an already allocated bit-task queue
575
568
// to the requesting process.
576
- func (s * MatcherSession ) AllocateSections (bit uint , count int ) []uint64 {
569
+ func (s * MatcherSession ) allocateSections (bit uint , count int ) []uint64 {
577
570
fetcher := make (chan * Retrieval )
578
571
579
572
select {
@@ -589,14 +582,10 @@ func (s *MatcherSession) AllocateSections(bit uint, count int) []uint64 {
589
582
}
590
583
}
591
584
592
- // DeliverSections delivers a batch of section bit-vectors for a specific bloom
585
+ // deliverSections delivers a batch of section bit-vectors for a specific bloom
593
586
// bit index to be injected into the processing pipeline.
594
- func (s * MatcherSession ) DeliverSections (bit uint , sections []uint64 , bitsets [][]byte ) {
595
- select {
596
- case <- s .kill :
597
- return
598
- case s .matcher .deliveries <- & Retrieval {Bit : bit , Sections : sections , Bitsets : bitsets }:
599
- }
587
+ func (s * MatcherSession ) deliverSections (bit uint , sections []uint64 , bitsets [][]byte ) {
588
+ s .matcher .deliveries <- & Retrieval {Bit : bit , Sections : sections , Bitsets : bitsets }
600
589
}
601
590
602
591
// Multiplex polls the matcher session for retrieval tasks and multiplexes it into
@@ -608,31 +597,31 @@ func (s *MatcherSession) DeliverSections(bit uint, sections []uint64, bitsets []
608
597
func (s * MatcherSession ) Multiplex (batch int , wait time.Duration , mux chan chan * Retrieval ) {
609
598
for {
610
599
// Allocate a new bloom bit index to retrieve data for, stopping when done
611
- bit , ok := s .AllocateRetrieval ()
600
+ bit , ok := s .allocateRetrieval ()
612
601
if ! ok {
613
602
return
614
603
}
615
604
// Bit allocated, throttle a bit if we're below our batch limit
616
- if s .PendingSections (bit ) < batch {
605
+ if s .pendingSections (bit ) < batch {
617
606
select {
618
607
case <- s .quit :
619
608
// Session terminating, we can't meaningfully service, abort
620
- s .AllocateSections (bit , 0 )
621
- s .DeliverSections (bit , []uint64 {}, [][]byte {})
609
+ s .allocateSections (bit , 0 )
610
+ s .deliverSections (bit , []uint64 {}, [][]byte {})
622
611
return
623
612
624
613
case <- time .After (wait ):
625
614
// Throttling up, fetch whatever's available
626
615
}
627
616
}
628
617
// Allocate as much as we can handle and request servicing
629
- sections := s .AllocateSections (bit , batch )
618
+ sections := s .allocateSections (bit , batch )
630
619
request := make (chan * Retrieval )
631
620
632
621
select {
633
622
case <- s .quit :
634
623
// Session terminating, we can't meaningfully service, abort
635
- s .DeliverSections (bit , sections , make ([][]byte , len (sections )))
624
+ s .deliverSections (bit , sections , make ([][]byte , len (sections )))
636
625
return
637
626
638
627
case mux <- request :
@@ -644,7 +633,7 @@ func (s *MatcherSession) Multiplex(batch int, wait time.Duration, mux chan chan
644
633
s .err .Store (result .Error )
645
634
s .Close ()
646
635
}
647
- s .DeliverSections (result .Bit , result .Sections , result .Bitsets )
636
+ s .deliverSections (result .Bit , result .Sections , result .Bitsets )
648
637
}
649
638
}
650
639
}
0 commit comments