github.com/sunriselayer/sunrise-da@v0.13.1-sr3/das/subscriber.go (about)

     1  package das
     2  
     3  import (
     4  	"context"
     5  
     6  	libhead "github.com/celestiaorg/go-header"
     7  
     8  	"github.com/sunriselayer/sunrise-da/header"
     9  )
    10  
    11  // subscriber subscribes to notifications about new headers in the network to keep
    12  // sampling process up-to-date with current network state.
    13  type subscriber struct {
    14  	done
    15  }
    16  
    17  func newSubscriber() subscriber {
    18  	return subscriber{newDone("subscriber")}
    19  }
    20  
    21  func (s *subscriber) run(ctx context.Context, sub libhead.Subscription[*header.ExtendedHeader], emit listenFn) {
    22  	defer s.indicateDone()
    23  	defer sub.Cancel()
    24  
    25  	for {
    26  		h, err := sub.NextHeader(ctx)
    27  		if err != nil {
    28  			if err == context.Canceled {
    29  				return
    30  			}
    31  
    32  			log.Errorw("failed to get next header", "err", err)
    33  			continue
    34  		}
    35  		log.Debugw("new header received via subscription", "height", h.Height())
    36  
    37  		emit(ctx, h)
    38  	}
    39  }