Ayende @ Rahien

Refunds available at head office

Lying as a valid coding pattern

Davy Brion has an interesting post about Protecting Your Application From Remote Problems, which contains a really interesting implementation of CircuitBreaker.

One thing, though, bothered me about it. Let us look at the code:

private class OpenState : CircuitBreakerState
{
	private readonly Timer timer;

	public OpenState(CircuitBreaker circuitBreaker)
		: base(circuitBreaker)
	{
		timer = new Timer(circuitBreaker.timeout.TotalMilliseconds);
		timer.Elapsed += TimeoutHasBeenReached;
		timer.AutoReset = false;
		timer.Start();
	}

	private void TimeoutHasBeenReached(object sender, ElapsedEventArgs e)
	{
		circuitBreaker.MoveToHalfOpenState();
	}

	public override void ProtectedCodeIsAboutToBeCalled()
	{
		throw new OpenCircuitException();
	}
}

The code works correctly, but I… don’t like the implementation. Please note that this is based on more gut feeling than anything else.

The part that I don’t like is that it is doing too much work. The use of timer for the timeout seems like a waste to me. What I would rather do is something like this:

private class OpenState : CircuitBreakerState
{
	private readonly DateTime expiry;

	public OpenState(CircuitBreaker circuitBreaker)
		: base(circuitBreaker)
	{
		expiry = DateTime.UtcNow + circuitBreaker.timeout;
	}

	public override void ProtectedCodeIsAboutToBeCalled()
	{
  	     if(DateTime.UtcNow < expiry)
			throw new OpenCircuitException();
             circuitBreaker.MoveToHalfOpenState();
	}
}

As seen from outside, this behave in exactly the same fashion, while not actually requiring anything from the system when we are actually not touching the code that the circuit breaker protects. It also force this to be a lazy evaluation, so if we are actually doing some reporting on the state of the circuit breaker, we won’t see the status change until something actually make a call to it.

That may or may not be a consideration. But even if it is, I would rather do the state change in the reporting function than in a dedicated timer.

Asynchronous order processing

One of the more common challenges that I run into when discussing the notion of async as the main communication mechanism is that there seems to be an entrenched belief that things should be synchronous. It appear to make things simpler, from a conceptual level, while making them significantly more difficult to actually implement them in a production worthy way.

Arguably the most common issue that I hear about is with downloadable materials, and it can be summed up as some variation of:

What do you mean we aren’t going to just start downloading stuff immediately?

That was why I was heartened when I tried to get a few Kindle books (ebooks) and saw that:

image

After about 5 minutes, it changed to this and allowed downloading this:

image

It is sad to say it, but applying to authority may suck as a argument tactic in a moral sense, but it is a damn effective one. Being able to say, here are all the reasons that you want to do that, and this is how Amazon is doing things, make it so much easier to sell this approach.

Another round of NHibernate courses

Well, it is time to start thinking about the next round of NHibernate courses.

The next one is on August 12th, in London, UK. The previous two courses that I taught there went very well, and the response from the students was quite positive.

There are still reservations available, so you might want to hurry up and book it now.

So, where was I?

Well, this has been quite a period of silence for me. The last post on the blog was made two weeks ago, and that is about as long as I have been silent in five years or so.

I took an unscheduled, urgently required, vacation. For the last three weeks or so, I barely even touched a computer, couldn’t stomach the thought.

It shows in my inbox, which I usually try to keep empty:

image

The problem with this type of vacation is that the longer you are gone, the more you don’t want to get back. I can only imagine the amount of stuff that has been piling in.

The reason for the vacation is actually quite simple, I think that I have been overdoing things, and after I got back from the last trip, I realized that 18 hours of sleep a day for a week are not just my reaction to jetlag.

It has been my experience that there are people who try to do too much end up getting burned.

"burning the candle at both ends"

There are some forms of escape from that, probably the most healthy one is to avoid doing what you have been doing. That is rarely the response though. About 7 years ago, I was working in Prison 6, as a prison guard. It was… hard. Probably one of the hardest things that I ever had to do. I distinctly remember being burned there, totally and completely. I was just a short stop away from going AWOL in an attempt to make it stop. I didn’t, and looking back, I am pretty happy about the way it turned out, but the memory of that time is still one of the worst times in my life.

I would really like to avoid having the same thing happen to me in my own field of expertise. One of the reasons that I like software development so much is that it has been my hobby as well as my profession. The last thing that I want to happen is that it would be a cause for distaste for me.

Hence, the reason that I called that vacation required, not merely needed. I also made a few other decisions, regarding the amount of travel and parallel work that I am going to take on from now on.

Hopefully, I can now return to my regularly scheduled blogging/working.