github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/access/subscription/subscribe_handler.go (about) 1 package subscription 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/rs/zerolog" 8 9 "github.com/onflow/flow-go/engine" 10 ) 11 12 // SubscriptionHandler represents common streaming data configuration for creating streaming subscription. 13 type SubscriptionHandler struct { 14 log zerolog.Logger 15 16 broadcaster *engine.Broadcaster 17 18 sendTimeout time.Duration 19 responseLimit float64 20 sendBufferSize int 21 } 22 23 // NewSubscriptionHandler creates a new SubscriptionHandler instance. 24 // 25 // Parameters: 26 // - log: The logger to use for logging. 27 // - broadcaster: The engine broadcaster for publishing notifications. 28 // - sendTimeout: The duration after which a send operation will timeout. 29 // - responseLimit: The maximum allowed response time for a single stream. 30 // - sendBufferSize: The size of the response buffer for sending messages to the client. 31 // 32 // Returns a new SubscriptionHandler instance. 33 func NewSubscriptionHandler( 34 log zerolog.Logger, 35 broadcaster *engine.Broadcaster, 36 sendTimeout time.Duration, 37 responseLimit float64, 38 sendBufferSize uint, 39 ) *SubscriptionHandler { 40 return &SubscriptionHandler{ 41 log: log, 42 broadcaster: broadcaster, 43 sendTimeout: sendTimeout, 44 responseLimit: responseLimit, 45 sendBufferSize: int(sendBufferSize), 46 } 47 } 48 49 // Subscribe creates and starts a new subscription. 50 // 51 // Parameters: 52 // - ctx: The context for the operation. 53 // - startHeight: The height to start subscription from. 54 // - getData: The function to retrieve data by height. 55 func (h *SubscriptionHandler) Subscribe( 56 ctx context.Context, 57 startHeight uint64, 58 getData GetDataByHeightFunc, 59 ) Subscription { 60 sub := NewHeightBasedSubscription(h.sendBufferSize, startHeight, getData) 61 go NewStreamer(h.log, h.broadcaster, h.sendTimeout, h.responseLimit, sub).Stream(ctx) 62 63 return sub 64 }