IDHttp Post Timeout

Posted on 2008-11-06
Last Modified: 2013-11-23
I am working on a legacy app that uses D6 with Indy 8.

I am wondering if there is a way to time out a post that's taking too long.  I realize the latest version Indy supports this with the ReadTimeOut property.  But for legacy reasons I am stuck with Indy 8.


Question by:SheratonGroup
    LVL 28

    Expert Comment

    you can "hack" it like  this:
    - create one thread
    - add each connection you want to timeout to the threads list (list:TList ) in a record type = record connection:TIdTcpConnection; timeout:TDateTime; end
    - make sure access to the list is synchronized (use a cs:TCriticalSection from SyncObjs)
    - before post-ing, add the connection to the above list and set the timeout to now
    - after post-ing, remove it from the list
    - the thread will check every second (or as often as you want) the entire list and when one of the timeout values exceed with 5 seconds (or whatever time you want) it will forcibly disconnect the connection.

    one thing to keep in mind here: if the connection is slow or the post response is big, from this point of view the conneciton will timeout. so make sure you use the onwork event to update the timeout from the record so the conneciton will not be closed by the trhead when it is actually working.

    it's a little complicated and I don't have time tonight to make you a demo, but if you cannot do it by yourself, just let me know and I'll try to do it tomorrow. or during the weekend ;)

    Author Comment

    Thanks for that, I would really prefer not to add any threads to the application.  

    Though I did try something similar with the IDAntiFreeze component and a TTimer.  I didn't like this solution either.  

    I also tried putting a handler in the onWork event hoping that I could keep track of elapsed time there, and disconnect based on how much time elapsed, unfortunately the event doesn't fire often enough to use.

    Is there no other "out of the box" way to do this with Indy 8?

    Btw, not that it matters too much, but this is for an ISAPI application so there is no GUI.

    LVL 28

    Accepted Solution

    since it's an isapi applicaiton, it does not have a form. antifreeze only works if you place it on the same component with the rest of indy (you can make it work at runtime as well, but you need pay extra attention). so I hope you were either doing it right at runtime, or you were using a datamodule and dropping the components on it.

    >> Is there no other "out of the box" way to do this with Indy 8?
    if you code your own http post, then yes, otehrwise, no.

    >> I would really prefer not to add any threads to the application.
    you can add one global thread. that way you don't compromise the server resources (I suspect this is the main reason behind not wanting threads)

    basically, it's like this:
    - indy is blocking
    - indy 8 has no timeout for http operations (as far as I seen looking through the code)
    - because of the above, you need to somehow disconnect the socket if it stalled too much
    - tehre are 2 ways: antifreeze or another thread

    that's it. and I can tell you that both methods work. there might be issues with antifreeze running in a dll mode in case of indy 8 (I think I successfully used indy9 or 10 in a dll with antifreeze), but in worse case scenario, you still have the thread method which will work.

    I'll answer any other questions tomorrow. It's close to midnight here :)

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now