github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/msg/consumer/types.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package consumer
    22  
    23  import (
    24  	"net"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/msg/protocol/proto"
    28  	"github.com/m3db/m3/src/x/instrument"
    29  )
    30  
    31  // Message carries the data that needs to be processed.
    32  type Message interface {
    33  	// Bytes returns the bytes.
    34  	Bytes() []byte
    35  
    36  	// Ack acks the message.
    37  	Ack()
    38  
    39  	// ShardID returns shard ID of the Message.
    40  	ShardID() uint64
    41  
    42  	// SentAtNanos returns when the producer sent the Message.
    43  	SentAtNanos() uint64
    44  }
    45  
    46  // Consumer receives messages from a connection.
    47  type Consumer interface {
    48  	// Message waits for and returns the next message received.
    49  	Message() (Message, error)
    50  
    51  	// Init initializes the consumer.
    52  	Init()
    53  
    54  	// Close closes the consumer.
    55  	Close()
    56  }
    57  
    58  // Listener is a consumer listener based on a network address.
    59  type Listener interface {
    60  	// Accept waits for and returns the next connection based consumer.
    61  	Accept() (Consumer, error)
    62  
    63  	// Close closes the listener.
    64  	// Any blocked Accept operations will be unblocked and return errors.
    65  	Close() error
    66  
    67  	// Addr returns the listener's network address.
    68  	Addr() net.Addr
    69  }
    70  
    71  // Options configs the consumer listener.
    72  type Options interface {
    73  	// EncoderOptions returns the options for Encoder.
    74  	EncoderOptions() proto.Options
    75  
    76  	// SetEncoderOptions sets the options for Encoder.
    77  	SetEncoderOptions(value proto.Options) Options
    78  
    79  	// DecoderOptions returns the options for Decoder.
    80  	DecoderOptions() proto.Options
    81  
    82  	// SetDecoderOptions sets the options for Decoder.
    83  	SetDecoderOptions(value proto.Options) Options
    84  
    85  	// MessagePoolOptions returns the options for message pool.
    86  	MessagePoolOptions() MessagePoolOptions
    87  
    88  	// SetMessagePoolOptions sets the options for message pool.
    89  	SetMessagePoolOptions(value MessagePoolOptions) Options
    90  
    91  	// AckFlushInterval returns the ack flush interval.
    92  	AckFlushInterval() time.Duration
    93  
    94  	// SetAckFlushInterval sets the ack flush interval.
    95  	SetAckFlushInterval(value time.Duration) Options
    96  
    97  	// AckBufferSize returns the ack buffer size.
    98  	AckBufferSize() int
    99  
   100  	// SetAckBufferSize sets the ack buffer size.
   101  	SetAckBufferSize(value int) Options
   102  
   103  	// ConnectionWriteBufferSize returns the size of buffer before a write or a read.
   104  	ConnectionWriteBufferSize() int
   105  
   106  	// SetConnectionWriteBufferSize sets the buffer size.
   107  	SetConnectionWriteBufferSize(value int) Options
   108  
   109  	// ConnectionReadBufferSize returns the size of buffer before a write or a read.
   110  	ConnectionReadBufferSize() int
   111  
   112  	// SetConnectionWriteBufferSize sets the buffer size.
   113  	SetConnectionReadBufferSize(value int) Options
   114  
   115  	// ConnectionWriteTimeout returns the timeout for writing to the connection.
   116  	ConnectionWriteTimeout() time.Duration
   117  
   118  	// SetConnectionWriteTimeout sets the write timeout for the connection.
   119  	SetConnectionWriteTimeout(value time.Duration) Options
   120  
   121  	// InstrumentOptions returns the instrument options.
   122  	InstrumentOptions() instrument.Options
   123  
   124  	// SetInstrumentOptions sets the instrument options.
   125  	SetInstrumentOptions(value instrument.Options) Options
   126  }
   127  
   128  // MessageProcessor processes the message. When a MessageProcessor was set in the
   129  // server, it will be called to process every message received.
   130  type MessageProcessor interface {
   131  	Process(m Message)
   132  	Close()
   133  }
   134  
   135  // MessageProcessorFactory creates MessageProcessors.
   136  type MessageProcessorFactory interface {
   137  	// Create returns a MessageProcessor.
   138  	Create() MessageProcessor
   139  	// Close the factory.
   140  	Close()
   141  }
   142  
   143  // SingletonMessageProcessor returns a MessageProcessorFactory that shares the same MessageProcessor for all users. The
   144  // MessageProcessor is closed when the factory is closed.
   145  func SingletonMessageProcessor(mp MessageProcessor) MessageProcessorFactory {
   146  	return &singletonMessageProcessorFactory{mp: mp, noClose: &noCloseMessageProcessor{mp: mp}}
   147  }
   148  
   149  type singletonMessageProcessorFactory struct {
   150  	mp      MessageProcessor
   151  	noClose MessageProcessor
   152  }
   153  
   154  func (s singletonMessageProcessorFactory) Create() MessageProcessor {
   155  	return s.noClose
   156  }
   157  
   158  func (s singletonMessageProcessorFactory) Close() {
   159  	s.mp.Close()
   160  }
   161  
   162  type noCloseMessageProcessor struct {
   163  	mp MessageProcessor
   164  }
   165  
   166  func (n noCloseMessageProcessor) Process(m Message) {
   167  	n.mp.Process(m)
   168  }
   169  
   170  func (n noCloseMessageProcessor) Close() {}
   171  
   172  // NewMessageProcessorFactory returns a MessageProcessorFactory that creates a new MessageProcessor for every call to
   173  // Create.
   174  func NewMessageProcessorFactory(fn func() MessageProcessor) MessageProcessorFactory {
   175  	return &messageProcessorFactory{fn: fn}
   176  }
   177  
   178  type messageProcessorFactory struct {
   179  	fn func() MessageProcessor
   180  }
   181  
   182  func (m messageProcessorFactory) Create() MessageProcessor {
   183  	return m.fn()
   184  }
   185  
   186  func (m messageProcessorFactory) Close() {}
   187  
   188  // NewNoOpMessageProcessor creates a new MessageProcessor that does nothing.
   189  func NewNoOpMessageProcessor() MessageProcessor {
   190  	return &noOpMessageProcessor{}
   191  }
   192  
   193  type noOpMessageProcessor struct{}
   194  
   195  func (n noOpMessageProcessor) Process(Message) {}
   196  
   197  func (n noOpMessageProcessor) Close() {}
   198  
   199  // ConsumeFn processes the consumer. This is useful when user want to reuse
   200  // resource across messages received on the same consumer or have finer level
   201  // control on how to read messages from consumer.
   202  type ConsumeFn func(c Consumer)