Change to support effectful deserializers #120
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Similarly to #118, this pull request adds support for effectul deserializers.
Previously,
Deserializer[A]
extended the Java KafkaDeserializer
, providing the following function.While
A
can be anything, including e.g.IO[B]
for some typeB
, we have to returnConsumerRecord[K, V]
, which in case of usingIO
would beConsumerRecord[IO[K], IO[V]]
. This means users will have to deal with deserialization somewhere else, which is far from ideal. It would be better ifConsumerRecord[K, V]
could be returned even though deserializers are effectful.Like for serializers, only the most basic deserializers do not make use of effects. For example, Confluent provides a popular Avro deserializer for use with their schema registry. The deserializer can automatically fetch schemas and perform certain compatibility checks as part of
deserialize
, but these side effects are generally not captured properly.To support effectful deserializers,
Deserializer[A]
becomesDeserializer[F[_], A]
withdeserialize
:essentially only changing the return type to
F[A]
.Deserializer
s are not restricted to effect types only forF[_]
, but can work with e.g.Id
. However, when used with a consumer, we requireF[_]
to be an effect type (requiringSync[F]
).The implication of this change is that
ConsumerSettings
is now also parameterized onF[_]
, and consumers now deserialize toF[A]
, only retrieving theArray[Byte]
from the Java Kafka consumer, whereas before the Java Kafka consumer would perform the deserialization toA
.Since deserialization used to happen on the Java Kafka consumer thread, and since many deserializers are in fact blocking (including the Confluent Avro deserializer), we default to keep deserialization on the dedicated
ExecutionContext
and providewithShiftDeserialization
onConsumerSettings
for changing behaviour.Deserializer
no longer extends the Java KafkaDeserializer
andConsumerSettings
cannot be created using KafkaDeserializer
s directly. Instead, useDeserializer.delegate
andDeserializer#suspend
to create aDeserializer
which delegates to a Java instance, wrapping inpure
andsuspend
.Detailed changes as follows.
Deserializer#suspend
to capture side effects from an impureDeserializer
.Deserializer
to be parameterized onF[_]
.Deserializer
s can now include effects (e.g.IO
).F[_]
.ConsumerSettings
to be parameterized onF[_]
.Sync[F]
).ConsumerSettings#apply
with implicit deserializers now requiresF[_]
to be specified explicitly, unless it can be inferred from the context.ConsumerFactory
, andConsumerSettings#consumerFactory
andwithConsumerFactory
.ConsumerSettings#createConsumer
andwithCreateConsumer
as replacements.ConsumerSettings#shiftDeserialization
andwithShiftDeserialization
.ExecutionContext
or not.true
to retain current behaviour and to keep supporting blocking deserializers.Deserializer#delay
and addDeserializer#defer
as a generic replacement.Deserializer.Attempt
andDeserializer.attempt
.F[_]
requiringApplicativeError[F, Throwable]
.ConsumerSettings#apply
accepting Java KafkaDeserializer
s.Deserializer
withDeserializer.delegate
and useDeserializer#suspend
to capture any non-pure behaviours.apply
functions acceptingExecutionContext
when creatingConsumerSettings
andProducerSettings
.ExecutionContext
, usewithExecutionContext
.