github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/topic/topicoptions/topicoptions_reader.go (about)

     1  package topicoptions
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/config"
     7  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawtopic/rawtopiccommon"
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/topic/topicreaderinternal"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/topic/topictypes"
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
    11  )
    12  
    13  // ReadSelector set rules for reader: set of topic, partitions, start time filted, etc.
    14  type ReadSelector = topicreaderinternal.PublicReadSelector
    15  
    16  // ReadSelectors slice of rules for topic reader
    17  type ReadSelectors []ReadSelector
    18  
    19  // ReadTopic create simple selector for read topics, if no need special settings.
    20  func ReadTopic(path string) ReadSelectors {
    21  	return ReadSelectors{{Path: path}}
    22  }
    23  
    24  // ReaderOption options for topic reader
    25  type ReaderOption = topicreaderinternal.PublicReaderOption
    26  
    27  // WithReaderOperationTimeout
    28  //
    29  // # Experimental
    30  //
    31  // Notice: This API is EXPERIMENTAL and may be changed or removed in a later release.
    32  func WithReaderOperationTimeout(timeout time.Duration) ReaderOption {
    33  	return func(cfg *topicreaderinternal.ReaderConfig) {
    34  		config.SetOperationTimeout(&cfg.Common, timeout)
    35  	}
    36  }
    37  
    38  // WithReaderStartTimeout mean timeout for connect to reader stream and work some time without errors
    39  //
    40  // # Experimental
    41  //
    42  // Notice: This API is EXPERIMENTAL and may be changed or removed in a later release.
    43  func WithReaderStartTimeout(timeout time.Duration) ReaderOption {
    44  	return func(cfg *topicreaderinternal.ReaderConfig) {
    45  		cfg.RetrySettings.StartTimeout = timeout
    46  	}
    47  }
    48  
    49  // WithReaderCheckRetryErrorFunction can override default error retry policy
    50  // use CheckErrorRetryDecisionDefault for use default behavior for the error
    51  // callback func must be fast and deterministic: always result same result for same error - it can be called
    52  // few times for every error
    53  func WithReaderCheckRetryErrorFunction(callback CheckErrorRetryFunction) ReaderOption {
    54  	return func(cfg *topicreaderinternal.ReaderConfig) {
    55  		cfg.RetrySettings.CheckError = callback
    56  	}
    57  }
    58  
    59  // WithReaderOperationCancelAfter
    60  //
    61  // # Experimental
    62  //
    63  // Notice: This API is EXPERIMENTAL and may be changed or removed in a later release.
    64  func WithReaderOperationCancelAfter(cancelAfter time.Duration) ReaderOption {
    65  	return func(cfg *topicreaderinternal.ReaderConfig) {
    66  		config.SetOperationCancelAfter(&cfg.Common, cancelAfter)
    67  	}
    68  }
    69  
    70  // WithCommonConfig
    71  //
    72  // # Experimental
    73  //
    74  // Notice: This API is EXPERIMENTAL and may be changed or removed in a later release.
    75  func WithCommonConfig(common config.Common) ReaderOption {
    76  	return func(cfg *topicreaderinternal.ReaderConfig) {
    77  		cfg.Common = common
    78  	}
    79  }
    80  
    81  // WithCommitTimeLagTrigger
    82  // Deprecated: (was experimental) will be removed soon.
    83  // Use WithReaderCommitTimeLagTrigger instead
    84  func WithCommitTimeLagTrigger(lag time.Duration) ReaderOption {
    85  	return WithReaderCommitTimeLagTrigger(lag)
    86  }
    87  
    88  // WithReaderCommitTimeLagTrigger set time lag from first commit message before send commit to server
    89  // for accumulate many similar-time commits to one server request
    90  // 0 mean no additional lag and send commit soon as possible
    91  // Default value: 1 second
    92  func WithReaderCommitTimeLagTrigger(lag time.Duration) ReaderOption {
    93  	return func(cfg *topicreaderinternal.ReaderConfig) {
    94  		cfg.CommitterBatchTimeLag = lag
    95  	}
    96  }
    97  
    98  // WithCommitCountTrigger
    99  // Deprecated: (was experimental) will be removed soon.
   100  // Use WithReaderCommitCountTrigger instead
   101  func WithCommitCountTrigger(count int) ReaderOption {
   102  	return WithReaderCommitCountTrigger(count)
   103  }
   104  
   105  // WithReaderCommitCountTrigger set count trigger for send batch to server
   106  // if count > 0 and sdk count of buffered commits >= count - send commit request to server
   107  // 0 mean no count limit and use timer lag trigger only
   108  func WithReaderCommitCountTrigger(count int) ReaderOption {
   109  	return func(cfg *topicreaderinternal.ReaderConfig) {
   110  		cfg.CommitterBatchCounterTrigger = count
   111  	}
   112  }
   113  
   114  // WithBatchReadMinCount
   115  // prefer min count messages in batch
   116  // sometimes batch can contain fewer messages, for example if local buffer is full and SDK can't receive more messages
   117  //
   118  // Deprecated: (was experimental) the method will be removed soon.
   119  //
   120  // The option will be removed for simplify code internals
   121  func WithBatchReadMinCount(count int) ReaderOption {
   122  	return func(cfg *topicreaderinternal.ReaderConfig) {
   123  		cfg.DefaultBatchConfig.MinCount = count
   124  	}
   125  }
   126  
   127  // WithBatchReadMaxCount
   128  // Deprecated: (was experimental) will be removed soon.
   129  // Use WithReaderBatchMaxCount instead.
   130  func WithBatchReadMaxCount(count int) ReaderOption {
   131  	return func(cfg *topicreaderinternal.ReaderConfig) {
   132  		cfg.DefaultBatchConfig.MaxCount = count
   133  	}
   134  }
   135  
   136  // WithReaderBatchMaxCount set max messages count, returned by topic.TopicReader.ReadBatch method
   137  func WithReaderBatchMaxCount(count int) ReaderOption {
   138  	return func(cfg *topicreaderinternal.ReaderConfig) {
   139  		cfg.DefaultBatchConfig.MaxCount = count
   140  	}
   141  }
   142  
   143  // WithMessagesBufferSize
   144  // Deprecated: (was experimental) will be removed soon
   145  // Use WithReaderBufferSizeBytes instead.
   146  func WithMessagesBufferSize(size int) ReaderOption {
   147  	return WithReaderBufferSizeBytes(size)
   148  }
   149  
   150  // WithReaderBufferSizeBytes set size of internal buffer for read ahead messages.
   151  func WithReaderBufferSizeBytes(size int) ReaderOption {
   152  	return func(cfg *topicreaderinternal.ReaderConfig) {
   153  		cfg.BufferSizeProtoBytes = size
   154  	}
   155  }
   156  
   157  // CreateDecoderFunc interface for fabric of message decoders
   158  type CreateDecoderFunc = topicreaderinternal.PublicCreateDecoderFunc
   159  
   160  // WithAddDecoder add decoder for a codec.
   161  // It allows to set decoders fabric for custom codec and replace internal decoders.
   162  func WithAddDecoder(codec topictypes.Codec, decoderCreate CreateDecoderFunc) ReaderOption {
   163  	return func(cfg *topicreaderinternal.ReaderConfig) {
   164  		cfg.Decoders.AddDecoder(rawtopiccommon.Codec(codec), decoderCreate)
   165  	}
   166  }
   167  
   168  // CommitMode variants of commit mode of the reader
   169  type CommitMode = topicreaderinternal.PublicCommitMode
   170  
   171  const (
   172  	// CommitModeAsync - commit return true if commit success add to internal send buffer (but not sent to server)
   173  	// now it is grpc buffer, in feature it may be internal sdk buffer
   174  	CommitModeAsync = topicreaderinternal.CommitModeAsync // default
   175  
   176  	// CommitModeNone - reader will not be commit operation
   177  	CommitModeNone = topicreaderinternal.CommitModeNone
   178  
   179  	// CommitModeSync - commit return true when sdk receive ack of commit from server
   180  	// The mode needs strong ordering client code for prevent deadlock.
   181  	// Example:
   182  	// Good:
   183  	// CommitOffset(1)
   184  	// CommitOffset(2)
   185  	//
   186  	// Error:
   187  	// CommitOffset(2) - server will wait commit offset 1 before send ack about offset 1 and 2 committed.
   188  	// CommitOffset(1)
   189  	// SDK will detect the problem and return error instead of deadlock.
   190  	CommitModeSync = topicreaderinternal.CommitModeSync
   191  )
   192  
   193  // WithCommitMode
   194  // Deprecated: (was experimental) will be removed soon.
   195  // Use WithReaderCommitMode instead.
   196  func WithCommitMode(mode CommitMode) ReaderOption {
   197  	return WithReaderCommitMode(mode)
   198  }
   199  
   200  // WithReaderCommitMode set commit mode to the reader
   201  func WithReaderCommitMode(mode CommitMode) ReaderOption {
   202  	return func(cfg *topicreaderinternal.ReaderConfig) {
   203  		cfg.CommitMode = mode
   204  	}
   205  }
   206  
   207  type (
   208  	// GetPartitionStartOffsetFunc callback function for optional handle start partition event and manage read progress
   209  	// at own side. It can call multiply times in parallel.
   210  	GetPartitionStartOffsetFunc = topicreaderinternal.PublicGetPartitionStartOffsetFunc
   211  
   212  	// GetPartitionStartOffsetRequest info about the partition
   213  	GetPartitionStartOffsetRequest = topicreaderinternal.PublicGetPartitionStartOffsetRequest
   214  
   215  	// GetPartitionStartOffsetResponse optional set offset for start reade messages for the partition
   216  	GetPartitionStartOffsetResponse = topicreaderinternal.PublicGetPartitionStartOffsetResponse
   217  )
   218  
   219  // WithGetPartitionStartOffset
   220  // Deprecated: (was experimental) will be removed soon.
   221  // Use WithReaderGetPartitionStartOffset instead
   222  func WithGetPartitionStartOffset(f GetPartitionStartOffsetFunc) ReaderOption {
   223  	return WithReaderGetPartitionStartOffset(f)
   224  }
   225  
   226  // WithReaderGetPartitionStartOffset set optional handler for own manage progress of read partitions
   227  // instead of/additional to commit messages
   228  func WithReaderGetPartitionStartOffset(f GetPartitionStartOffsetFunc) ReaderOption {
   229  	return func(cfg *topicreaderinternal.ReaderConfig) {
   230  		cfg.GetPartitionStartOffsetCallback = f
   231  	}
   232  }
   233  
   234  // WithReaderTrace set tracer for the topic reader
   235  //
   236  // # Experimental
   237  //
   238  // Notice: This API is EXPERIMENTAL and may be changed or removed in a later release.
   239  func WithReaderTrace(t trace.Topic) ReaderOption { //nolint:gocritic
   240  	return func(cfg *topicreaderinternal.ReaderConfig) {
   241  		cfg.Trace = cfg.Trace.Compose(&t)
   242  	}
   243  }
   244  
   245  // WithReaderUpdateTokenInterval set custom interval for send update token message to the server
   246  func WithReaderUpdateTokenInterval(interval time.Duration) ReaderOption {
   247  	return func(cfg *topicreaderinternal.ReaderConfig) {
   248  		cfg.CredUpdateInterval = interval
   249  	}
   250  }