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