Monday, April 05, 2010

Timeouts by analogy

Most networking-enabled applications have to deal with timeouts. Read or write operations may continue indefinitely, and programs need a way to determine when to tear down connections, resend requests, or take whatever other measures are necessary.

Asio includes the deadline_timer class for managing timeouts. This class aims to provide a minimal interface for scheduling events. Of course, minimalism gives little in the way of design guidance, so some users struggle in finding an elegant way to incorporate timers and timeouts into their programs.

From the minimalist perspective of Asio, there's no one true right way to do it. (Perhaps there's no better proof of that than my design preferences having changed over the years.) Yet that answer doesn't get programs written, so in this post I will try to present a simple mental model for managing timers.

Parking meters

High-traffic, commercial areas near where I live have limited on-street parking. The street parking that is available is metered. It's the usual drill:


  • Park your vehicle.

  • Feed some coins into the parking meter (or, as is more likely these days, swipe your credit card or send an SMS).

  • Go do whatever you came to do.

  • Make sure you return to your vehicle before the meter expires.

If you don't get back in time, you'd better hope your vehicle hasn't had a visit from the parking inspector. A visit means a ticket under the wipers and a nasty fine due.

Parking meters are a good analogy for reasoning about timeouts because it's easy to identify the two actors:


  • The driver of the vehicle.

  • The parking inspector.

The driver performs the following steps:


  1. Feeds the meter.

  2. Leaves the vehicle to run some errands.

  3. Returns to the vehicle.

  4. If no ticket has been issued, repeats from step 1.

  5. If a fine has been issued, goes home.

The parking inspector's job is simple:


  1. Checks whether the meter has expired.

  2. If the meter has expired, writes up a ticket.

  3. If the meter has not expired, notes how much time is remaining.

  4. Goes off for a walk until the remaining time has elapsed.

Using the analogy to inform program design

Hopefully you've already guessed how these actors map to networked applications:


  • The driver represents your protocol handling code.

  • The parking inspector corresponds to your timeout management logic.

Let's take a look at how this works in a very simple use case.

// The "driver" actor.
void session::handle_read(error_code ec, size_t length)
{
// On entering this function we have returned to the vehicle.

if (!ec)
{
// Phew, no ticket. Feed the meter.
my_timer.expires_from_now(seconds(5));

// Process incoming data.
// ...

// Run some more errands.
my_socket.async_read_some(buffer(my_buffer),
bind(&session::handle_read, this, _1, _2));
}
else
{
// We got a ticket. Go home.
}
}

// The "parking inspector" actor.
void session::handle_timeout(error_code ec)
{
// On entering this function we are checking the meter.

// Has the meter expired?
if (my_timer.expires_from_now() < seconds(0))
{
// Write up a ticket.
my_socket.close();
}
else
{
// Note remaining time and go for a walk.
my_timer.async_wait(
bind(&session::handle_timeout, this, _1));
}
}

It's important to remember that the driver may need to run multiple errands each time they leave the vehicle. In protocol terms, you might have a fixed-length header followed by a variable-length body. You only want to "feed the meter" once you have received a complete message:

// First part of the "driver" actor.
void session::handle_read_header(error_code ec)
{
// We're not back at the vehicle yet.

if (!ec)
{
// Process header.
// ...

// Run some more errands.
async_read(my_socket, buffer(my_body),
bind(&session::handle_read_body, this, _1));
}
}

// Second part of the "driver" actor.
void session::handle_read_body(error_code ec)
{
// On entering this function we have returned to the vehicle.

if (!ec)
{
// Phew, no ticket. Feed the meter.
my_timer.expires_from_now(seconds(5));

// Process complete message.
// ...

// Run some more errands.
async_read(my_socket, buffer(my_header),
bind(&session::handle_read_header, this, _1));
}
else
{
// We got a ticket. Go home.
}
}

There are many variations on this theme. For example, you may feed the meter between consecutive errands, varying the amount of money inserted (i.e. setting different length timeouts) depending on which errand comes next. In protocol terms, that might mean allowing up to 30 seconds between messages, but only a further 5 seconds is permitted once the message header has been received.

As I indicated earlier, there's no single right way to manage timeouts. In fact, there are many different facets to this problem that are probably worth exploring in their own right. However, I think that the approach shown here is probably suited to most applications and I would recommend it as a starting point when designing your timeout handling.

46 comments:

Nikhil Bansal said...

Really nice and definitely it will be useful for many people. Kindly keep update like this. Nidhi (owner and founder) software development company in delhi ncr, india.

Nikhil Bansal said...

Tritan is one of the leading software development company in delhi ncr, india. we have Over 18+ years of rich experience, we ensure that you always get the best guidance and support. We serve clients to our best possible efforts to make sure thier life is easier, whether as a trusted advisor to top management or hands-on coach for front line employees - "We assemble team with the most appropriate experience and expertise". Our goal is too optimise our customers business and make sure that they get the right level of expertise. We believe customer is first and 100% attention is given to build strong business relationships. We have offices at different locations Delhi NCR, Banglore, Gujarat etc. To know more about us Visit our website:- https://www.tritansolutions.com/about-us.aspx

suman said...

I found useful article in your blog. thank you for sharing good info
visit
web programming tutorial
welookups.com

Raj Sharma said...

The Suggession is really good. Thank you very much.
C++ course in Noida

Dev Arya said...

Great post. Thanks for sharing information.
C++ course in Delhi

Shailendra said...

Good Post. I like it. Thanks
C++ course in Gurgaon

Anonymous said...

Acknowledge the full Digital Marketing course within your budget with some special discounted offers ! Hurry Up
digital marketing course with placement

Acil said...

Thanks for sharing this post.
Interested in Graphic Designing Course!!!
Contact ACIL for the best Graphic designing Institute in Gurgaon.
GRAPHIC DESIGNING INSTITUTE IN GURGAON



Thanks for sharing this post.
Interested in Python training!!!
Contact ACIL for the best Python Training Institute in Gurgaon.
PYTHON TRAINING INSTITUTE IN GURGAON

ptiacademy said...

I love your blog post, you are a great blogger. I want to introduce you about our institute PTI Academy offer Summer Internship in Jaipur at affordable price.

educational blogs said...

I am so happy after reading your blog. It’s a very useful blog for us.

Angular 7 corporate training in Nigeria

educational blogs said...

I am so happy after reading your blog. It’s very useful blog for us.

Corporate training in Machine learning

Nandita said...

Great post. I am experiencing many of these issues as well..

selenium training in Bangalore

Selenium Courses in Bangalore

best selenium training institute in Bangalore



Angular expert said...

Good web site you have here.. It’s difficult to find excellent writing like yours these days. I honestly appreciate people like you!

Java Training In Bangalore

java training center Bangalore

Best core java training in Bangalore

Java courses in Bangalore

Java Training in Marathahalli

Institute in Gurgaon said...

SYSAPPPRO Gurgaon is rated as one of the Best SAP HANA Training Institute in Gurgaon based on Google and other outsider reviews. We have completed over 5000 Training classes in SAP HANA and provided 100% placement assistance to students. Over the previous 5 years, Team at SYSAPPPRO Gurgaon is dedicated to provide High-Quality Training in SAP HANA.

For More Info: SAP HANA Course in Gurgaon

Attitute Tally Academy said...

thank you for sharing nice content.
PHP Web Development Training in Uttam Nagar

Attitute Tally Academy said...

thank you for sharing nice content.
PHP Web Development Training in Uttam Nagar

surbhi nahta said...

Thank you for the information and updates. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. I am searching for this topic and I get exact post. I am also searching about Tally Course In Pune,try to write something about this topic.

Shruti said...

I was able to find good info from your blog posts.


Selenium Courses in Marathahalli

selenium institutes in Marathahalli

selenium training in Bangalore

Selenium Courses in Bangalore

best selenium training institute in Bangalore

selenium training institute in Bangalore

Nida said...

CONTRACT ADVANTAGE is Contract Management System offered as SaaS designed to help you do more with your contracts using fewer people, less time, and a smaller budget.

high technologies solutions said...

very useful content shared by you.Thanks for the posting
Python training course in Delhi
python training institute in noida

high technologies solutions said...

Great information and very useful content
Python training institute in Delhi
python training institute in noida

Arajgure said...

HI,

Great Post for Beginner to understand. I finally found great post here. Thanks for information.keep sharing more articles. For More details Visit:

Advanced Excel Training In Pune

googleads123 said...

I really happy found this website eventually. Really informative and inoperative, Thanks for the post and effort! Please keep sharing more such blog.
Visit website: Ad Position Lowest CPC

high technologies solutions said...

Thanks for posting. Very useful content shared by you
java training course in Delhi
java training course in Noida

high technologies solutions said...

very informative post shared by you. Thanks for sharing
Digital Marketing course in Delhi
Digital Marketing course in Noida

SAP Training Delhi said...

Thanks for Sharing a very Nice Post & It’s really helpful for everyone. Keep on updating these kinds of
Informative things Otherwise If anyone Want to Learn SAP Training Course Basic to Adv. Level So Contact THERE- 9599118710
Some Best SAP Training Center in Delhi, India
sap training institute in delhi
sap training center in delhi
sap training in delhi
sap course in delhi

SEO Service said...

I read a lot of stuff and i found that the way of writing to clearifing that exactly want to say was very good so i am impressed and ilike to come again in future..satta king

satta king said...

This is also a very good post which I really enjoyed reading. It is not everyday that I have the possibility to see something like this. Satta king

hireseo123 said...

This is extremely helpful info!! Very good work. Everything is very interesting to learn and easy to understand. Thank you for giving information.
Visit website: digital marketing consultant

Trimmers123 said...

it is really a great and helpful piece of info. I am glad that you shared this helpful information with us. Please keep us informed like this. Thank you for sharing.
Website:- best cheap smartphone

high technologies solutions said...

High Technologies Solutions offers an inclusive Autocad training in Delhi and Noida.We offer Live projects, Assignments and free demo class. We provide study material. Well experienced trainers.For free demo class call at +919311002620 or visit our website.
autocad training institute in Delhi
autocad training institute in Noida


australiaeta123 said...

it is really a great and helpful piece of info. I am glad that you shared this helpful information with us. Please keep us informed like this. Thank you for sharing.
Website:- Australian high Commission Singapore

prerna mehta said...

Thanks for sharing such helpful information with us I appreciate your effort of writing a value able piece.
i also write on Spanish Language Course in Delhi. Please share your review on that.


realestate20 said...

thanks for share I am glad that you shared this helpful information with us. Please keep us informed like this
visit here- top real estate lawyers

jewelleryad123 said...

I always like to read a quality content having accurate information regarding the subject and the same thing I found in this post. Nice work
Fashionjewellery
Artificial jewellery online
western jewellery

godquotes1 said...

Words are god is the website with a global cause of igniting conscious living in mankind Being with us can be meeting yourself in person Awareness of our existence and improving it is a process which we must seek in life. We are the people with different Conscience but the same Consciousness and when we become identified with it, we find the connecting chords of the cosmos and get to know that we are not separate from anything in existence. Wisdom is power (subjectively), and power is best shared among us.spiritual quotes
wisdom quotes
love quotes

Specialist123 said...

powerful vashikaran specialist for husband|love vashikaran specilaist astrologer in india|husband and wife problem solution

matka bank said...

I really like your post, I always like to read a quality content having accurate information regarding the subject. Thanks for sharing. It is such a very amazing post...
Online matka play | Matka play | Play matka

bestwnh79 said...

I really like your post, I always like to read a quality content having accurate information regarding the subject. Thanks for sharing. It is such a very amazing post...
hotel in wayanad | wayanad hotel booking | wayanad best resorts

Shivani said...

I used to be able to find good advice from your blog articles.



Best Advanced Java Training In Bangalore Marathahalli

Advanced Java Courses In Bangalore Marathahalli

Advanced Java Training Center In Bangalore

Advanced Java Training Center In Bangalore

Selenium Training in Bangalore

Selenium Training in Marathahalli

Selenium Courses in Bangalore

best selenium training institute in Bangalore


Couple123 said...

This is a to a great extent signal post. Thanks quest of posting this
body shaper for women|matching outfits for family|personalized lockets

Sakshi said...

i also would like to share my content topic on French language course and German language course . please review and share your feedback.

Shruti said...

Pretty! This has been a really wonderful post. Many thanks for providing this information.

Best Advanced Java Training In Bangalore Marathahalli

Advanced Java Courses In Bangalore Marathahalli

Advanced Java Training Center In Bangalore

Advanced Java Training Center In Bangalore

Selenium Training in Bangalore

Selenium Training in Marathahalli

Selenium Courses in Bangalore

best selenium training institute in Bangalore



Sakshi said...

It was very insightful . thanks for sharing. keep sharing such good content. I also would like to share my content on IELTS Coaching and Digital Marketing Course. please review and share your feedback

Realestatelawyer97 said...

It's really amazing blog postt. i never expect that today i read like this thanks fro sharing. such a nice post. thanks a lot..
purchase Agreements | real estate sales agreement | commericial Property

lots2fun123 said...

A lot of thanks for every one of your efforts on this blog.I came across this board and I find It really useful & it helped me out a lot. Anyway keep up the excellent quality writing, it is rare to see a nice blog like this one nowadays. Thanks
Visit website: clash of clans|best attacks in coc builder base|clash of clans best attack army|best attack strategy coc builder base|best attack strategy coc|best attack in clash of clans|best attack army coc|>|clash of clans 13 town hall attack on titan