-
Notifications
You must be signed in to change notification settings - Fork 98
/
package.scala
299 lines (270 loc) · 10.3 KB
/
package.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright 2018-2020 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
package fs2
import cats.effect._
import scala.concurrent.duration.FiniteDuration
package object kafka {
type Id[+A] = A
/** Alias for Java Kafka `Consumer[Array[Byte], Array[Byte]]`. */
type KafkaByteConsumer =
org.apache.kafka.clients.consumer.Consumer[Array[Byte], Array[Byte]]
/** Alias for Java Kafka `Producer[Array[Byte], Array[Byte]]`. */
type KafkaByteProducer =
org.apache.kafka.clients.producer.Producer[Array[Byte], Array[Byte]]
/** Alias for Java Kafka `Deserializer[A]`. */
type KafkaDeserializer[A] =
org.apache.kafka.common.serialization.Deserializer[A]
/** Alias for Java Kafka `Serializer[A]`. */
type KafkaSerializer[A] =
org.apache.kafka.common.serialization.Serializer[A]
/** Alias for Java Kafka `Header`. */
type KafkaHeader =
org.apache.kafka.common.header.Header
/** Alias for Java Kafka `Headers`. */
type KafkaHeaders =
org.apache.kafka.common.header.Headers
/** Alias for Java Kafka `ConsumerRecords[Array[Byte], Array[Byte]]`. */
type KafkaByteConsumerRecords =
org.apache.kafka.clients.consumer.ConsumerRecords[Array[Byte], Array[Byte]]
/** Alias for Java Kafka `ConsumerRecord[Array[Byte], Array[Byte]]`. */
type KafkaByteConsumerRecord =
org.apache.kafka.clients.consumer.ConsumerRecord[Array[Byte], Array[Byte]]
/** Alias for Java Kafka `ProducerRecord[Array[Byte], Array[Byte]]`. */
type KafkaByteProducerRecord =
org.apache.kafka.clients.producer.ProducerRecord[Array[Byte], Array[Byte]]
/**
* Commits offsets in batches of every `n` offsets or time window
* of length `d`, whichever happens first. If there are no offsets
* to commit within a time window, no attempt will be made to commit
* offsets for that time window.
*/
def commitBatchWithin[F[_]](n: Int, d: FiniteDuration)(
implicit F: Concurrent[F],
timer: Timer[F]
): Pipe[F, CommittableOffset[F], Unit] =
_.groupWithin(n, d).evalMap(CommittableOffsetBatch.fromFoldable(_).commit)
/**
* Creates a [[KafkaProducer]] using the provided settings and
* produces record in batches, limiting the number of records
* in the same batch using [[ProducerSettings#parallelism]].
*/
def produce[F[_], K, V, P](
settings: ProducerSettings[F, K, V]
)(
implicit F: ConcurrentEffect[F],
context: ContextShift[F]
): Pipe[F, ProducerRecords[K, V, P], ProducerResult[K, V, P]] =
records => producerStream(settings).flatMap(produce(settings, _).apply(records))
/**
* Produces records in batches using the provided [[KafkaProducer]].
* The number of records in the same batch is limited using the
* [[ProducerSettings#parallelism]] setting.
*/
def produce[F[_], K, V, P](
settings: ProducerSettings[F, K, V],
producer: KafkaProducer[F, K, V]
)(
implicit F: ConcurrentEffect[F]
): Pipe[F, ProducerRecords[K, V, P], ProducerResult[K, V, P]] =
_.evalMap(producer.produce).mapAsync(settings.parallelism)(identity)
/**
* Creates a new [[KafkaAdminClient]] in the `Resource` context,
* using the specified [[AdminClientSettings]]. If working in a
* `Stream` context, you might prefer [[adminClientStream]].
*/
def adminClientResource[F[_]](settings: AdminClientSettings[F])(
implicit F: Concurrent[F],
context: ContextShift[F]
): Resource[F, KafkaAdminClient[F]] =
KafkaAdminClient.resource(settings)
/**
* Creates a new [[KafkaAdminClient]] in the `Stream` context,
* using the specified [[AdminClientSettings]]. If you're not
* working in a `Stream` context, you might instead prefer to
* use the [[adminClientResource]] function.
*/
def adminClientStream[F[_]](settings: AdminClientSettings[F])(
implicit F: Concurrent[F],
context: ContextShift[F]
): Stream[F, KafkaAdminClient[F]] =
Stream.resource(adminClientResource(settings))
/**
* Creates a new [[KafkaConsumer]] in the `Resource` context,
* using the specified [[ConsumerSettings]]. Note that there
* is another version where `F[_]` is specified explicitly and
* the key and value type can be inferred, which allows you
* to use the following syntax.
*
* {{{
* consumerResource[F].using(settings)
* }}}
*/
def consumerResource[F[_], K, V](settings: ConsumerSettings[F, K, V])(
implicit F: ConcurrentEffect[F],
context: ContextShift[F],
timer: Timer[F]
): Resource[F, KafkaConsumer[F, K, V]] =
KafkaConsumer.consumerResource(settings)
/**
* Alternative version of `consumerResource` where the `F[_]` is
* specified explicitly, and where the key and value type can
* be inferred from the [[ConsumerSettings]]. This allows you
* to use the following syntax.
*
* {{{
* consumerResource[F].using(settings)
* }}}
*/
def consumerResource[F[_]](implicit F: ConcurrentEffect[F]): ConsumerResource[F] =
new ConsumerResource[F](F)
/**
* Creates a new [[KafkaConsumer]] in the `Stream` context,
* using the specified [[ConsumerSettings]]. Note that there
* is another version where `F[_]` is specified explicitly and
* the key and value type can be inferred, which allows you
* to use the following syntax.
*
* {{{
* consumerStream[F].using(settings)
* }}}
*/
def consumerStream[F[_], K, V](settings: ConsumerSettings[F, K, V])(
implicit F: ConcurrentEffect[F],
context: ContextShift[F],
timer: Timer[F]
): Stream[F, KafkaConsumer[F, K, V]] =
Stream.resource(consumerResource(settings))
/**
* Alternative version of `consumerStream` where the `F[_]` is
* specified explicitly, and where the key and value type can
* be inferred from the [[ConsumerSettings]]. This allows you
* to use the following syntax.
*
* {{{
* consumerStream[F].using(settings)
* }}}
*/
def consumerStream[F[_]](implicit F: ConcurrentEffect[F]): ConsumerStream[F] =
new ConsumerStream[F](F)
/**
* Creates a new [[KafkaProducer]] in the `Resource` context,
* using the specified [[ProducerSettings]]. Note that there
* is another version where `F[_]` is specified explicitly and
* the key and value type can be inferred, which allows you
* to use the following syntax.
*
* {{{
* producerResource[F].using(settings)
* }}}
*/
def producerResource[F[_], K, V](settings: ProducerSettings[F, K, V])(
implicit F: ConcurrentEffect[F],
context: ContextShift[F]
): Resource[F, KafkaProducer[F, K, V]] =
KafkaProducer.resource(settings)
/**
* Alternative version of `producerResource` where the `F[_]` is
* specified explicitly, and where the key and value type can
* be inferred from the [[ProducerSettings]]. This allows you
* to use the following syntax.
*
* {{{
* producerResource[F].using(settings)
* }}}
*/
def producerResource[F[_]](implicit F: ConcurrentEffect[F]): ProducerResource[F] =
new ProducerResource(F)
/**
* Creates a new [[KafkaProducer]] in the `Stream` context,
* using the specified [[ProducerSettings]]. Note that there
* is another version where `F[_]` is specified explicitly and
* the key and value type can be inferred, which allows you
* to use the following syntax.
*
* {{{
* producerStream[F].using(settings)
* }}}
*/
def producerStream[F[_], K, V](settings: ProducerSettings[F, K, V])(
implicit F: ConcurrentEffect[F],
context: ContextShift[F]
): Stream[F, KafkaProducer[F, K, V]] =
Stream.resource(producerResource(settings))
/**
* Alternative version of `producerStream` where the `F[_]` is
* specified explicitly, and where the key and value type can
* be inferred from the [[ProducerSettings]]. This allows you
* to use the following syntax.
*
* {{{
* producerStream[F].using(settings)
* }}}
*/
def producerStream[F[_]](implicit F: ConcurrentEffect[F]): ProducerStream[F] =
new ProducerStream[F](F)
/**
* Creates a new [[TransactionalKafkaProducer]] in the `Resource` context,
* using the specified [[TransactionalProducerSettings]]. Note that there
* is another version where `F[_]` is specified explicitly and the key and
* value type can be inferred, which allows you to use the following syntax.
*
* {{{
* transactionalProducerResource[F].using(settings)
* }}}
*/
def transactionalProducerResource[F[_], K, V](
settings: TransactionalProducerSettings[F, K, V]
)(
implicit F: ConcurrentEffect[F],
context: ContextShift[F]
): Resource[F, TransactionalKafkaProducer[F, K, V]] =
TransactionalKafkaProducer.resource(settings)
/**
* Alternative version of `transactionalProducerResource` where the `F[_]`
* is specified explicitly, and where the key and value type can be
* inferred from the [[TransactionalProducerSettings]]. This allows
* you to use the following syntax.
*
* {{{
* transactionalProducerResource[F].using(settings)
* }}}
*/
def transactionalProducerResource[F[_]](
implicit F: ConcurrentEffect[F]
): TransactionalProducerResource[F] =
new TransactionalProducerResource(F)
/**
* Creates a new [[TransactionalKafkaProducer]] in the `Stream` context,
* using the specified [[TransactionalProducerSettings]]. Note that there
* is another version where `F[_]` is specified explicitly and the key and
* value type can be inferred, which allows you to use the following syntax.
*
* {{{
* transactionalProducerStream[F].using(settings)
* }}}
*/
def transactionalProducerStream[F[_], K, V](
settings: TransactionalProducerSettings[F, K, V]
)(
implicit F: ConcurrentEffect[F],
context: ContextShift[F]
): Stream[F, TransactionalKafkaProducer[F, K, V]] =
Stream.resource(transactionalProducerResource(settings))
/**
* Alternative version of `transactionalProducerStream` where the `F[_]`
* is specified explicitly, and where the key and value type can be
* inferred from the [[TransactionalProducerSettings]]. This allows
* you to use the following syntax.
*
* {{{
* transactionalProducerStream[F].using(settings)
* }}}
*/
def transactionalProducerStream[F[_]](
implicit F: ConcurrentEffect[F]
): TransactionalProducerStream[F] =
new TransactionalProducerStream(F)
}