-
Notifications
You must be signed in to change notification settings - Fork 18
Don't unsafely run effect from Producer's send callback #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
20a85f9
337006e
19f316c
2f5a4eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,12 @@ package com.evolutiongaming.skafka | |
| package producer | ||
|
|
||
| import cats.data.{NonEmptyMap => Nem} | ||
| import cats.effect.{Resource, Sync, Async, Deferred} | ||
| import cats.effect.implicits._ | ||
| import cats.effect.{Async, Resource, Sync} | ||
| import cats.implicits._ | ||
| import cats.{Applicative, Functor, MonadError, ~>} | ||
| import com.evolutiongaming.catshelper.CatsHelper._ | ||
| import com.evolutiongaming.catshelper.{Blocking, Log, MonadThrowable, ToTry} | ||
| import com.evolutiongaming.catshelper.{Blocking, Log, MonadThrowable} | ||
| import com.evolutiongaming.skafka.Converters._ | ||
| import com.evolutiongaming.skafka.producer.ProducerConverters._ | ||
| import com.evolutiongaming.smetrics.MeasureDuration | ||
|
|
@@ -18,7 +18,7 @@ import org.apache.kafka.clients.producer.{ | |
| RecordMetadata => RecordMetadataJ | ||
| } | ||
|
|
||
| import scala.concurrent.{ExecutionContext, ExecutionException} | ||
| import scala.concurrent.{ExecutionContext, ExecutionException, Promise} | ||
| import scala.jdk.CollectionConverters._ | ||
|
|
||
| /** | ||
|
|
@@ -91,14 +91,14 @@ object Producer { | |
| } | ||
|
|
||
| @deprecated("Use of(ProducerConfig)", since = "12.0.1") | ||
| def of[F[_]: ToTry: Async]( | ||
| def of[F[_]: Async]( | ||
| config: ProducerConfig, | ||
| executorBlocking: ExecutionContext | ||
| ): Resource[F, Producer[F]] = { | ||
| of(config) | ||
| } | ||
|
|
||
| def of[F[_]: ToTry: Async]( | ||
| def of[F[_]: Async]( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. breaks backward compatibility
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I though it might break it, but MiMa checks passing kinda convinced me otherwise.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or do you mean source compatibility? |
||
| config: ProducerConfig | ||
| ): Resource[F, Producer[F]] = { | ||
| val producer = CreateProducerJ(config) | ||
|
|
@@ -108,11 +108,11 @@ object Producer { | |
| private sealed abstract class Main | ||
|
|
||
| @deprecated("Use fromProducerJ2", since = "12.0.1") | ||
| def fromProducerJ1[F[_]: Blocking: ToTry: Async](producer: F[ProducerJ[Bytes, Bytes]]): Resource[F, Producer[F]] = { | ||
| def fromProducerJ1[F[_]: Blocking: Async](producer: F[ProducerJ[Bytes, Bytes]]): Resource[F, Producer[F]] = { | ||
| fromProducerJ2(producer) | ||
| } | ||
|
|
||
| def fromProducerJ2[F[_]: ToTry: Async](producer: F[ProducerJ[Bytes, Bytes]]): Resource[F, Producer[F]] = { | ||
| def fromProducerJ2[F[_]: Async](producer: F[ProducerJ[Bytes, Bytes]]): Resource[F, Producer[F]] = { | ||
|
|
||
| def blocking[A](f: => A) = Sync[F].blocking(f) | ||
|
|
||
|
|
@@ -150,7 +150,7 @@ object Producer { | |
|
|
||
| def block(record: ProducerRecordJ[Bytes, Bytes]) = { | ||
|
|
||
| def callbackOf(deferred: Deferred[F, Either[Throwable, RecordMetadataJ]]): Callback = { | ||
| def callbackOf(promise: Promise[RecordMetadataJ]): Callback = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you think that this is anyhow faster?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't measured it of course, it just intuitively feels more lightweight, but I don't have any strong arguments in this regard.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd recommend keep using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But how is
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if I change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Just ignore it, as executed code is private and you exactly know what is being executed
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Because that combination of Promise/Future adds more overhead than you might think of comparing to native, it is also not a cancellable thing.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But this particular use case is not cancellable either way, is it? Even if you cancel the resulting effect the producer's callback will still be executed
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean cancelling the "inner" effect resulting from |
||
| (metadata: RecordMetadataJ, exception: Exception) => | ||
| val result = if (exception != null) { | ||
| exception.asLeft[RecordMetadataJ] | ||
|
|
@@ -159,22 +159,16 @@ object Producer { | |
| } else { | ||
| SkafkaError("both metadata & exception are nulls").asLeft[RecordMetadataJ] | ||
| } | ||
| deferred | ||
| .complete(result) | ||
| .toTry | ||
| .get | ||
| () | ||
| promise.complete(result.toTry) | ||
| } | ||
|
|
||
| val result = for { | ||
| deferred <- Async[F].deferred[Either[Throwable, RecordMetadataJ]] | ||
| callback = callbackOf(deferred) | ||
| _ <- blocking { producer.send(record, callback) } | ||
| promise <- Sync[F].delay(Promise[RecordMetadataJ]()) | ||
| callback = callbackOf(promise) | ||
| _ <- blocking { producer.send(record, callback) } | ||
| res = Async[F].fromFuture(Sync[F].delay(promise.future)) | ||
| } yield { | ||
| deferred | ||
| .get | ||
| .flatMap { _.liftTo[F] } | ||
| .recoverWith(executionException) | ||
| res.recoverWith(executionException) | ||
| } | ||
| result.recoverWith(executionException) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
breaks backward compatibility
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But how does MiMa check pass then?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good question, should not pass