none
I need to synchronize access to a process for multiple HTTP requests RRS feed

  • Question

  • I have a command line process which gets invoked via the Process class which can only service one HTTP request at a time. I need to synchronize access to the process, perhaps using a named mutex, but I also want to service HTTP requests in the order they come using a timeout so that the request is serviced in a reasonable amount of time.  I have successfully implemented a timeout on the process with the appropriate process termination and my next step is to attempt to implement the mutex, but I don't know how to service the oldest pending HTTP request.

    Tim

    Sunday, June 8, 2014 3:22 AM

Answers

  • I think you have failed to understand the problem. The command line application is supposed to get invoked each time there is an HTTP request. A List<> cannot be shared for each request - that's why the Mutex and Semaphore classes were created.

    I ended up using a Mutex in a web app controller to ensure that no more than one HTTP request is invoking the process that runs the command line app at a time.  However, it does not guaranty an order in which pending requests will get serviced.  This is okay for the development and testing phase of a UI application during which we don't expect to see a lot of requests queued up.

    The problem is queuing the multiple HTTP requests on the server so that they are handled in list order.  The web app only has access to each HTTP request. Perhaps what I need is some kind of proxy in the server that will queue up the HTTP requests and access the resource as it becomes available.


    Tim

    • Marked as answer by aaomtim Monday, June 16, 2014 2:21 PM
    Monday, June 16, 2014 1:55 PM

All replies

  • You have a master/slave (Client/server) application.  You command line application is the master sending HTTP requests to a remote server.  The timeout should be implemented in the server application and the server should send a response when the timeout occurs so you remain synchronized.  The command line process will send one command and will not send another command until the server sends a response.

    The command line input will not accept a 2nd command until the first command is completed which occurs when the server send a response.  If you want to accept multiple command lines before the response occurs then you have to buffer the input command lines with a collection object like a List<string>().


    jdweng

    Sunday, June 8, 2014 9:00 AM
  • It is true that I have a client server application, but the command line process is not the client.  It is the primary server application that is being invoked each time the HTTP client posts and XML file for processing and it is guarded by a mutex. Basically each http client will send a post request to the web (server) app submitting XML via a URL like www.hostname.com:port/api/xmlparser.

    If the command line process is running as indicated by the mutex, then the client will wait for the process to complete until it's request is serviced. If there are multiple HTTP requests awaiting the process completion, I don't know how to guaranty that the oldest pending request gets serviced next.

    Tim

    Sunday, June 8, 2014 4:43 PM
  • Hi Tim,

    From your description, I know you want to build a high performance client-server system and the server app can receive many requests at the same time. The following example introduces the idea of asynchronous sockets, which were added to the Winsock framework to promote scalability in large applications.  Async sockets use multiple application threads to simultaneously read and write to many connection sockets. 

    Please refer to the following link for details. http://code.msdn.microsoft.com/windowsdesktop/High-Performance-NET-69c2df2f.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 9, 2014 6:38 AM
    Moderator
  • By creating a List<>.  You add new requests to the end of the List<>, and process/remove items from index zero.  The List will act as a FIFO.


    jdweng

    Monday, June 9, 2014 6:42 AM
  • I think you have failed to understand the problem. The command line application is supposed to get invoked each time there is an HTTP request. A List<> cannot be shared for each request - that's why the Mutex and Semaphore classes were created.

    I ended up using a Mutex in a web app controller to ensure that no more than one HTTP request is invoking the process that runs the command line app at a time.  However, it does not guaranty an order in which pending requests will get serviced.  This is okay for the development and testing phase of a UI application during which we don't expect to see a lot of requests queued up.

    The problem is queuing the multiple HTTP requests on the server so that they are handled in list order.  The web app only has access to each HTTP request. Perhaps what I need is some kind of proxy in the server that will queue up the HTTP requests and access the resource as it becomes available.


    Tim

    • Marked as answer by aaomtim Monday, June 16, 2014 2:21 PM
    Monday, June 16, 2014 1:55 PM
  • I don't think you want to prevent multiple clients from connecting simultaneously.  If you do then the second connection will not complete.  That is why I think you need a List<Client>.  The clients will put each request into a queue.


    jdweng

    Monday, June 16, 2014 2:36 PM
  • I do not want to prevent them from connecting simultaneously.  I merely want each to block and not respond to each HTTP request until the protected command line process completes for the previous request.  The command line app is not queuing requests - ideally it would be invoked in parallel as each request comes, but the mutex disallows that.  The List<> would be useful if the command line app was the client, but it's not. 

    Now if you can tell me how to pool the HTTP requests coming into the server into a List<> and to provide a response to each request as each is serviced by the command line process, that would be great. Meanwhile all I need is the mutex to prevent the command line process from being executed in parallel.


    Tim

    Monday, June 16, 2014 3:03 PM
  • You need a HttpListener.  See webpage below.  There are a ffew ways of managing the List<> object.

    http://msdn.microsoft.com/en-us/library/system.net.httplistenerrequest(v=vs.110).aspx

    List<HttpListener> listeners = new List<HttpListener>();
    HttpListener listener = new HttpListener();
    listeners.Add(listener);

    The final code will look very similar to an Asynchronous Socket Server using the HttpListener instead of the Socket Listener.

    http://msdn.microsoft.com/en-us/library/w89fhyex(v=vs.110).aspx


    jdweng

    Monday, June 16, 2014 4:13 PM