-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial functional streams support (#284)
* feat: add fs2 support [reader, subscription, producer, committable producer] (cherry picked from commit 45a0ddc) * fix: avoid unsubscription by default after end of stream. Co-authored-by: Sam Sam <[email protected]>
- Loading branch information
1 parent
03c1540
commit db85f88
Showing
4 changed files
with
242 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
pulsar4s-fs2/src/main/scala/com/sksamuel/pulsar4s/fs2/Streams.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.sksamuel.pulsar4s.fs2 | ||
|
||
import cats.Applicative | ||
import cats.effect.{Bracket, BracketThrow, ExitCase, Resource} | ||
import cats.implicits._ | ||
import com.sksamuel.pulsar4s._ | ||
|
||
trait CommittableMessage[F[_], X] { | ||
def ack: F[Unit] | ||
def nack: F[Unit] | ||
def data: X | ||
|
||
def map[Y](f: X => Y): CommittableMessage[F, Y] | ||
} | ||
|
||
object Streams { | ||
import _root_.fs2.{Pipe, Stream} | ||
|
||
private final class DelegateCommittableMessage[F[_] : AsyncHandler, T]( | ||
message: ConsumerCommittableMessage[F, _], | ||
payload: T | ||
) extends CommittableMessage[F, T] { | ||
override def ack: F[Unit] = message.ack | ||
override def nack: F[Unit] = message.nack | ||
override def data: T = payload | ||
override def map[Y](f: T => Y): CommittableMessage[F, Y] = new DelegateCommittableMessage(message, f(payload)) | ||
} | ||
|
||
private final case class ConsumerCommittableMessage[F[_] : AsyncHandler, T]( | ||
message: ConsumerMessage[T], | ||
consumer: Consumer[T] | ||
) extends CommittableMessage[F, ConsumerMessage[T]] { | ||
override def ack: F[Unit] = consumer.acknowledgeAsync(message.messageId) | ||
override def nack: F[Unit] = consumer.negativeAcknowledgeAsync(message.messageId) | ||
override def data: ConsumerMessage[T] = message | ||
|
||
override def map[Y](f: ConsumerMessage[T] => Y): CommittableMessage[F, Y] = | ||
new DelegateCommittableMessage(this, f(message)) | ||
} | ||
|
||
def batch[F[_] : Applicative : AsyncHandler, T]( | ||
consumer: F[Consumer[T]] | ||
): Stream[F, CommittableMessage[F, ConsumerMessage[T]]] = | ||
Stream.resource(Resource.make(consumer)(_.closeAsync)) | ||
.flatMap { consumer => | ||
Stream | ||
.repeatEval(consumer.receiveBatchAsync[F]) | ||
.flatMap(Stream.emits(_)) | ||
.mapChunks(_.map(message => ConsumerCommittableMessage(message, consumer))) | ||
} | ||
|
||
def single[F[_] : Applicative : AsyncHandler, T]( | ||
consumer: F[Consumer[T]] | ||
): Stream[F, CommittableMessage[F, ConsumerMessage[T]]] = | ||
Stream.resource(Resource.make(consumer)(_.closeAsync)) | ||
.flatMap { consumer => | ||
Stream | ||
.repeatEval(consumer.receiveAsync[F]) | ||
.mapChunks(_.map(message => ConsumerCommittableMessage(message, consumer))) | ||
} | ||
|
||
def reader[F[_] : Applicative : AsyncHandler, T]( | ||
reader: F[Reader[T]] | ||
): Stream[F, ConsumerMessage[T]] = | ||
Stream.resource(Resource.make(reader)(_.closeAsync)) | ||
.flatMap { reader => | ||
Stream | ||
.repeatEval(reader.nextAsync[F]) | ||
} | ||
|
||
def sink[F[_] : Applicative : AsyncHandler, T]( | ||
producer: F[Producer[T]] | ||
): Pipe[F, ProducerMessage[T], MessageId] = messages => | ||
Stream.resource(Resource.make(producer)(_.closeAsync)) | ||
.flatMap { producer => | ||
messages.evalMap(producer.sendAsync(_)) | ||
} | ||
|
||
def committableSink[F[_] : Applicative : BracketThrow : AsyncHandler , T]( | ||
producer: F[Producer[T]] | ||
): Pipe[F, CommittableMessage[F, ProducerMessage[T]], MessageId] = messages => | ||
Stream.resource(Resource.make(producer)(_.closeAsync)) | ||
.flatMap { producer => | ||
messages.evalMap { message => | ||
Bracket[F, Throwable].guaranteeCase(producer.sendAsync(message.data)) { | ||
case ExitCase.Completed => message.ack | ||
case _ => message.nack | ||
} | ||
} | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
pulsar4s-fs2/src/test/scala/com/sksamuel/pulsar4s/fs2/StreamsTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.sksamuel.pulsar4s.fs2 | ||
|
||
import cats.effect.{IO, Sync} | ||
import cats.implicits._ | ||
import com.sksamuel.pulsar4s.{AsyncHandler, ConsumerConfig, Message, MessageId, ProducerConfig, ProducerMessage, PulsarAsyncClient, PulsarClient, ReaderConfig, Subscription, Topic} | ||
import org.apache.pulsar.client.api.{Schema, SubscriptionInitialPosition} | ||
import org.scalatest.BeforeAndAfterAll | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import java.util.UUID | ||
|
||
class StreamsTest extends AnyFunSuite with Matchers with BeforeAndAfterAll { | ||
|
||
implicit val schema: Schema[String] = Schema.STRING | ||
|
||
private val client = PulsarClient("pulsar://localhost:6650") | ||
|
||
override def afterAll(): Unit = { | ||
client.close() | ||
} | ||
|
||
private def publishMessages[F[_]: Sync: AsyncHandler](client: PulsarAsyncClient, t: Topic, messages: List[String]): F[Unit] = | ||
for { | ||
producer <- client.producerAsync[String, F](ProducerConfig( | ||
topic = t | ||
)) | ||
_ <- messages | ||
.map(producer.sendAsync(_)) | ||
.sequence | ||
} yield () | ||
|
||
test("able to read from topic") { | ||
import com.sksamuel.pulsar4s.cats.CatsAsyncHandler._ | ||
val topic = Topic("persistent://sample/standalone/ns1/" + UUID.randomUUID().toString) | ||
val messages = (0 to 10).map(p => s"TestMessage_$p").toList | ||
|
||
(for { | ||
_ <- publishMessages[IO](client, topic, messages) | ||
read <- Streams.reader[IO, String](client.readerAsync[String, IO](ReaderConfig( | ||
topic = topic, | ||
startMessage = Message(MessageId.earliest) | ||
))).take(messages.size).map(_.value).compile.toList | ||
} yield read shouldBe messages).unsafeRunSync() | ||
} | ||
|
||
test("able to read from subscription [batch]") { | ||
import com.sksamuel.pulsar4s.cats.CatsAsyncHandler._ | ||
val topic = Topic("persistent://sample/standalone/ns1/" + UUID.randomUUID().toString) | ||
val messages = (0 to 10).map(p => s"TestMessage_$p").toList | ||
|
||
(for { | ||
_ <- publishMessages[IO](client, topic, messages) | ||
batch <- Streams.batch[IO, String](client.consumerAsync[String, IO](ConsumerConfig( | ||
subscriptionName = Subscription("fs2_subscription_batch"), | ||
topics = Seq(topic), | ||
subscriptionInitialPosition = Some(SubscriptionInitialPosition.Earliest) | ||
))).take(messages.size).map(_.data.value).compile.toList | ||
single <- Streams.single[IO, String](client.consumerAsync[String, IO](ConsumerConfig( | ||
subscriptionName = Subscription("fs2_subscription_single"), | ||
topics = Seq(topic), | ||
subscriptionInitialPosition = Some(SubscriptionInitialPosition.Earliest) | ||
))).take(messages.size).map(_.data.value).compile.toList | ||
} yield { | ||
batch shouldBe messages | ||
single shouldBe messages | ||
}).unsafeRunSync() | ||
} | ||
|
||
test("able to connect with sink") { | ||
import com.sksamuel.pulsar4s.cats.CatsAsyncHandler._ | ||
|
||
val topic = Topic("persistent://sample/standalone/ns1/" + UUID.randomUUID().toString) | ||
val topic2 = Topic("persistent://sample/standalone/ns1/" + UUID.randomUUID().toString) | ||
val messages = (0 to 10).map(p => s"TestMessage_$p").toList | ||
|
||
(for { | ||
_ <- publishMessages[IO](client, topic, messages) | ||
|
||
_ <- Streams.batch[IO, String](client.consumerAsync[String, IO](ConsumerConfig( | ||
subscriptionName = Subscription("fs2_subscription_batch"), | ||
topics = Seq(topic), | ||
subscriptionInitialPosition = Some(SubscriptionInitialPosition.Earliest) | ||
))) | ||
.take(messages.size) | ||
.map(_.map { message => | ||
ProducerMessage(message.value) | ||
}) | ||
.through(Streams.committableSink(client.producerAsync[String, IO](ProducerConfig(topic2)))) | ||
.compile | ||
.drain | ||
|
||
batch <- Streams.batch[IO, String](client.consumerAsync[String, IO](ConsumerConfig( | ||
subscriptionName = Subscription("fs2_subscription_batch"), | ||
topics = Seq(topic2), | ||
subscriptionInitialPosition = Some(SubscriptionInitialPosition.Earliest) | ||
))).take(messages.size).map(_.data.value).compile.toList | ||
} yield { | ||
batch shouldBe messages | ||
}).unsafeRunSync() | ||
} | ||
} |