go-micro.dev/v5@v5.12.0/events/options.go (about)

     1  package events
     2  
     3  import "time"
     4  
     5  type Options struct{}
     6  
     7  type Option func(o *Options)
     8  
     9  type StoreOptions struct {
    10  	TTL    time.Duration
    11  	Backup Backup
    12  }
    13  
    14  type StoreOption func(o *StoreOptions)
    15  
    16  // PublishOptions contains all the options which can be provided when publishing an event
    17  type PublishOptions struct {
    18  	// Metadata contains any keys which can be used to query the data, for example a customer id
    19  	Metadata map[string]string
    20  	// Timestamp to set for the event, if the timestamp is a zero value, the current time will be used
    21  	Timestamp time.Time
    22  }
    23  
    24  // PublishOption sets attributes on PublishOptions
    25  type PublishOption func(o *PublishOptions)
    26  
    27  // WithMetadata sets the Metadata field on PublishOptions
    28  func WithMetadata(md map[string]string) PublishOption {
    29  	return func(o *PublishOptions) {
    30  		o.Metadata = md
    31  	}
    32  }
    33  
    34  // WithTimestamp sets the timestamp field on PublishOptions
    35  func WithTimestamp(t time.Time) PublishOption {
    36  	return func(o *PublishOptions) {
    37  		o.Timestamp = t
    38  	}
    39  }
    40  
    41  // ConsumeOptions contains all the options which can be provided when subscribing to a topic
    42  type ConsumeOptions struct {
    43  	// Group is the name of the consumer group, if two consumers have the same group the events
    44  	// are distributed between them
    45  	Group string
    46  	// Offset is the time from which the messages should be consumed from. If not provided then
    47  	// the messages will be consumed starting from the moment the Subscription starts.
    48  	Offset time.Time
    49  	// AutoAck if true (default true), automatically acknowledges every message so it will not be redelivered.
    50  	// If false specifies that each message need ts to be manually acknowledged by the subscriber.
    51  	// If processing is successful the message should be ack'ed to remove the message from the stream.
    52  	// If processing is unsuccessful the message should be nack'ed (negative acknowledgement) which will mean it will
    53  	// remain on the stream to be processed again.
    54  	AutoAck bool
    55  	AckWait time.Duration
    56  	// RetryLimit indicates number of times a message is retried
    57  	RetryLimit int
    58  	// CustomRetries indicates whether to use RetryLimit
    59  	CustomRetries bool
    60  }
    61  
    62  // ConsumeOption sets attributes on ConsumeOptions
    63  type ConsumeOption func(o *ConsumeOptions)
    64  
    65  // WithGroup sets the consumer group to be part of when consuming events
    66  func WithGroup(q string) ConsumeOption {
    67  	return func(o *ConsumeOptions) {
    68  		o.Group = q
    69  	}
    70  }
    71  
    72  // WithOffset sets the offset time at which to start consuming events
    73  func WithOffset(t time.Time) ConsumeOption {
    74  	return func(o *ConsumeOptions) {
    75  		o.Offset = t
    76  	}
    77  }
    78  
    79  // WithAutoAck sets the AutoAck field on ConsumeOptions and an ackWait duration after which if no ack is received
    80  // the message is requeued in case auto ack is turned off
    81  func WithAutoAck(ack bool, ackWait time.Duration) ConsumeOption {
    82  	return func(o *ConsumeOptions) {
    83  		o.AutoAck = ack
    84  		o.AckWait = ackWait
    85  	}
    86  }
    87  
    88  // WithRetryLimit sets the RetryLimit field on ConsumeOptions.
    89  // Set to -1 for infinite retries (default)
    90  func WithRetryLimit(retries int) ConsumeOption {
    91  	return func(o *ConsumeOptions) {
    92  		o.RetryLimit = retries
    93  		o.CustomRetries = true
    94  	}
    95  }
    96  
    97  func (s ConsumeOptions) GetRetryLimit() int {
    98  	if !s.CustomRetries {
    99  		return -1
   100  	}
   101  	return s.RetryLimit
   102  }
   103  
   104  // WriteOptions contains all the options which can be provided when writing an event to a store
   105  type WriteOptions struct {
   106  	// TTL is the duration the event should be recorded for, a zero value TTL indicates the event should
   107  	// be stored indefinately
   108  	TTL time.Duration
   109  }
   110  
   111  // WriteOption sets attributes on WriteOptions
   112  type WriteOption func(o *WriteOptions)
   113  
   114  // WithTTL sets the TTL attribute on WriteOptions
   115  func WithTTL(d time.Duration) WriteOption {
   116  	return func(o *WriteOptions) {
   117  		o.TTL = d
   118  	}
   119  }
   120  
   121  // ReadOptions contains all the options which can be provided when reading events from a store
   122  type ReadOptions struct {
   123  	// Limit the number of results to return
   124  	Limit uint
   125  	// Offset the results by this number, useful for paginated queries
   126  	Offset uint
   127  }
   128  
   129  // ReadOption sets attributes on ReadOptions
   130  type ReadOption func(o *ReadOptions)
   131  
   132  // ReadLimit sets the limit attribute on ReadOptions
   133  func ReadLimit(l uint) ReadOption {
   134  	return func(o *ReadOptions) {
   135  		o.Limit = 1
   136  	}
   137  }
   138  
   139  // ReadOffset sets the offset attribute on ReadOptions
   140  func ReadOffset(l uint) ReadOption {
   141  	return func(o *ReadOptions) {
   142  		o.Offset = 1
   143  	}
   144  }