Skip to content
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

Terminate the stream when the server closes the connection #737

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import cats.effect.std.Dispatcher
import cats.syntax.flatMap._
import cats.syntax.functor._
import cats.{Applicative, Functor}
import com.rabbitmq.client.{AMQP, Consumer, DefaultConsumer, Envelope}
import com.rabbitmq.client.{AMQP, Consumer, DefaultConsumer, Envelope, ShutdownSignalException}
import dev.profunktor.fs2rabbit.arguments.{Arguments, _}
import dev.profunktor.fs2rabbit.model._

Expand Down Expand Up @@ -106,6 +106,11 @@ object Consume {
}
}
}

override def handleShutdownSignal(consumerTag: String, sig: ShutdownSignalException): Unit =
if (!sig.isInitiatedByApplication) {
internals.queue.foreach(q => dispatcher.unsafeRunAndForget(q.offer(Left(sig))))
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ RabbitMQ:
- "5672:5672"
environment:
- DEBUG=false
volumes:
- ./rabbit-test-config/:/etc/rabbitmq/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tweak rabbit configuration so it detects missing ack and terminates the channel faster in tests.
With default settings it takes 30 minutes to reproduce the issue

6 changes: 6 additions & 0 deletions rabbit-test-config/advanced.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{rabbit, [
{channel_tick_interval, 500},
{loopback_users, []}
]}
].
1 change: 1 addition & 0 deletions rabbit-test-config/rabbitmq.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
consumer_timeout = 1000
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,28 @@ trait Fs2RabbitSpec { self: BaseSpec =>
}
}

it should "shutdown the stream when the server closes the channel" in withRabbit { interpreter =>
import interpreter._
val msg = "will-not-be-acked"
createConnectionChannel.use { implicit channel =>
for {
qxrk <- randomQueueData
(q, x, rk) = qxrk
_ <- declareExchange(x, ExchangeType.Topic)
_ <- declareQueue(DeclarationQueueConfig.default(q))
_ <- bindQueue(q, x, rk, QueueBindingArgs(Map.empty))
publisher <- createPublisher[String](x, rk)
_ <- publisher(msg)
stream <- createAckerConsumer(q).map(_._2)
results <- stream.attempt.compile.toList.timeoutAndForget(Duration(10, "s"))
} yield {
results.size shouldEqual 2
results.head.map(_.payload) shouldEqual Right(msg)
results.last.isLeft shouldEqual true
}
}
}

it should "preserve order of published messages" in withRabbit { interpreter =>
import dev.profunktor.fs2rabbit.effects.{EnvelopeDecoder, MessageEncoder}
import interpreter._
Expand Down