My intensive work on EasyNetQ (our super simple .NET API for RabbitMQ) continues. I’ve been taking lessons learned from nearly two years of production and the fantastic feedback from EasyNetQ’s users, mashing this together, and making lots changes to both the internals and the API. I know that API changes cause problems for users; they break your application and force you to revisit your code. But the longer term benefits should outweigh the immediate costs as EasyNetQ slowly morphs into a solid, reliable library that really does make working with RabbitMQ as easy as possible.
Changes in version 0.17 are all around the request-response pattern. The initial implementation was very rough with lots of nasty ways that resource use could run away when things went wrong. The lack of timeouts also meant that your application could wait forever when messages got lost. Lastly the API was quite clunky, with call-backs where Tasks are a far better choice. All these problems have been corrected in this version.
API changes
There is now a synchronous Request method. Of course messaging is by nature a asynchronous operation, but sometimes you just want the simplest possible thing and you don’t care about blocking your thread while you wait for a response. Here’s what it looks like:
var response = bus.Request<TestRequestMessage, TestResponseMessage>(request);
The old call-back Request method has been removed. There was no need for it when the RequestAsync that returned a Task<TResult> was always a better choice:
var task = bus.RequestAsync<TestRequestMessage, TestResponseMessage>(request)
task.ContinueWith(response =>
{
Console.WriteLine("Got response: '{0}'", response.Result.Text);
});
Timeouts
Timeouts are an essential ingredient of any distributed system. This probably deserves a blog post of its own, but no matter how resilient you make your architecture, if an important piece simply goes away (like the network for example), you need a circuit breaker. EasyNetQ now has a global timeout that you can configure via the connection string:
var bus = RabbitHutch.CreateBus("host=localhost;timeout=60");
Here we’ve configured the timeout as 60 seconds. The default is 10 seconds. If you make a request, but no response is received within the timeout period, a System.Timeout exception will be thrown.
If the connection goes away while a request is in-flight, EasyNetQ doesn’t wait for the timeout to fire, but immediately throws an EasyNetQException with a message saying that the connection has been lost. Your application should catch both Timeout and EasyNetQ exceptions and react appropriately.
Internal Changes
My last blog post was a discussion of the implementation options of request-response with RabbitMQ. As I said there, I now believe that a single exclusive queue for all responses to a client is the best option. Version 0.17 implements this. When you call bus.Request(…) you will see a queue created named easynetq.response.<some guid>. This will last for the lifetime of the current connection.
Happy requesting!
2 comments:
Any chance you could add a Timeout value to the Request/RequestAsync methods? It's great to have a global timeout value, but in some cases, I know a specific message is going to take a while.
Example:
TResponse IBus.Request(TRequest req, int? TimeoutMS=null)
with this, if the TimeoutMS value is null, you can use the global timeout, but if it's set, you can use the connection timeout.
I'm using the Request/Response pattern a lot, usually with a single global IBus instance. Some responses come quickly, as the information needed to fulfill them is in memory. Other times there's a costly DB operation.
I don't know if it's there and I'm missing it, or if there's a better way to do it. I definitely think being able to specify a per-Request timeout would be beneficial.
I'm reluctant to change the API at this stage. I'm trying to stabilise things for a 1.0 release. But thanks, it's an interesting idea. I'm afraid there isn't any alternative to default timeout presently.
Post a Comment