github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/events/options.go (about)

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