github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/cluster/pubsub_producer_opts.go (about) 1 package cluster 2 3 import ( 4 "time" 5 6 "github.com/asynkron/protoactor-go/actor" 7 ) 8 9 type BatchingProducerConfigOption func(config *BatchingProducerConfig) 10 11 // WithBatchingProducerBatchSize sets maximum size of the published batch. Default: 2000. 12 func WithBatchingProducerBatchSize(batchSize int) BatchingProducerConfigOption { 13 return func(config *BatchingProducerConfig) { 14 config.BatchSize = batchSize 15 } 16 } 17 18 // WithBatchingProducerMaxQueueSize set max size of the requests waiting in queue. If value is provided, the producer will throw 19 // ProducerQueueFullException when queue size is exceeded. If 0 or unset, the queue is unbounded 20 // Note that bounded queue has better performance than unbounded queue. 21 // Default: 0 (unbounded) 22 func WithBatchingProducerMaxQueueSize(maxQueueSize int) BatchingProducerConfigOption { 23 return func(config *BatchingProducerConfig) { 24 config.MaxQueueSize = maxQueueSize 25 } 26 } 27 28 // WithBatchingProducerPublishTimeout sets how long to wait for the publishing to complete. 29 // Default: 5s 30 func WithBatchingProducerPublishTimeout(publishTimeout time.Duration) BatchingProducerConfigOption { 31 return func(config *BatchingProducerConfig) { 32 config.PublishTimeout = publishTimeout 33 } 34 } 35 36 // WithBatchingProducerOnPublishingError sets error handler that can decide what to do with an error when publishing a batch. 37 // Default: Fail and stop the BatchingProducer 38 func WithBatchingProducerOnPublishingError(onPublishingError PublishingErrorHandler) BatchingProducerConfigOption { 39 return func(config *BatchingProducerConfig) { 40 config.OnPublishingError = onPublishingError 41 } 42 } 43 44 // WithBatchingProducerLogThrottle sets a throttle for logging from this producer. By default, a throttle shared between all instances of 45 // BatchingProducer is used, that allows for 10 events in 10 seconds. 46 func WithBatchingProducerLogThrottle(logThrottle actor.ShouldThrottle) BatchingProducerConfigOption { 47 return func(config *BatchingProducerConfig) { 48 config.LogThrottle = logThrottle 49 } 50 } 51 52 // WithBatchingProducerPublisherIdleTimeout sets an optional idle timeout which will specify to the `IPublisher` how long it should wait before invoking clean 53 // up code to recover resources. 54 func WithBatchingProducerPublisherIdleTimeout(publisherIdleTimeout time.Duration) BatchingProducerConfigOption { 55 return func(config *BatchingProducerConfig) { 56 config.PublisherIdleTimeout = publisherIdleTimeout 57 } 58 } 59 60 type PublishingErrorDecision struct { 61 Delay time.Duration 62 } 63 64 // NewPublishingErrorDecision creates a new PublishingErrorDecision 65 func NewPublishingErrorDecision(delay time.Duration) *PublishingErrorDecision { 66 return &PublishingErrorDecision{Delay: delay} 67 } 68 69 // RetryBatchAfter returns a new PublishingErrorDecision with the Delay set to the given duration 70 func RetryBatchAfter(delay time.Duration) *PublishingErrorDecision { 71 return NewPublishingErrorDecision(delay) 72 } 73 74 // FailBatchAndStop causes the BatchingProducer to stop and fail the pending messages 75 var FailBatchAndStop = NewPublishingErrorDecision(0) 76 77 // FailBatchAndContinue skips the current batch and proceeds to the next one. The delivery reports (tasks) related to that batch are still 78 // failed with the exception that triggered the error handling. 79 var FailBatchAndContinue = NewPublishingErrorDecision(0) 80 81 // RetryBatchImmediately retries the current batch immediately 82 var RetryBatchImmediately = NewPublishingErrorDecision(0)