Stateless locking server design

Please help a little.

I am creating a stateless server that will have the following functionality:

  • The client submits the job to the server.
  • The client is blocked while the server tries to complete the job.
  • The server will create one or more threads to complete the task.
  • The job ends, time or failure.
  • An appropriate response is generated (based on the result), the client is unblocked, and the response is sent to the client.

Here's what I thought so far.

  • The client submits the job to the server.
  • The server assigns an ID to the job, puts the job in the queue, and then puts the client in the next queue (where it will be blocked).
  • Have a thread pool that will execute the job, retrieve the result, and build the response accordingly.
  • Based on the ID, take the client out of the queue (thereby unblocking it), give it a response and send it.

Steps 1,3,4 seem pretty straight forward, however any ideas on how to queue a client and then block it. Any pointers to help me build this puppy would also be appreciated.

Greetings

+2


source to share


3 answers


Why do you need to block a client? It looks like it would be easier to go back (almost) immediately (after doing the initial check, if any) and provide the client with a unique ID for the jobs. The client will then be able to either poll using the specified identifier, or perhaps provide a callback.

Blocking means you are holding onto a socket, which obviously limits the top number of clients you can serve concurrently. Unless this is a problem for your scenario and you absolutely need to block (perhaps you have no control over the client code and cannot interrogate them?), There is little point in having threads to do the job unless you can actually split it into parallel tasks. The only "queue" in this case will be what is maintained by the shared thread pool. The workflow will be basically:

  • Create a thread pool (e.g. ThreadPoolExecutor )
  • For every customer request:
    • If you have parts of the job that you can run in parallel, delegate them to the pool.
    • And / or do them in the current thread.
    • Wait until the completed portions of the assignment are completed (if applicable).
    • Returns the results to the client.
  • Turn off the thread pool.


No identifiers are needed by themselves; although you may need to use latch for version 2.1 / 2.3 above.

Timeouts can be quite tricky. If you need them to be more or less accurate, you need to keep your main thread (the one that received the client request) free of work and force it to send the submitted parts of the job (by flipping the flag) when the timeout is reached and immediately will return, you will need to periodically check the specified flag and stop execution as soon as it turns over; Then the pool will return the stream.

+2


source


How do you communicate with the client?



I recommend that you create an object to represent each job that contains job parameters and a socket (or other communication mechanism) to access the client. The thread pool will then send a response to unblock the client at the end of the job processing.

0


source


Timeouts will be somewhat tricky and hidden, but the basic design will seem simple, write a class that accepts a Socket in the constructor. on socket.accept we just create a new socket handling instance, with more prediction and planning for scalability, or if this is an experimental test test then the socket handling class just goes to the data handling stuff and when it comes back you have some boolean type or numeric for state or whatever, a convenient place for a zero bit, and ether writes the success to the output stream from the socket, or informs the client about a timeout or whatever your business needs.

If you must have a scalable, efficient design for durable trucks, go straight to nio ... the hand-coded one-off solutions as I describe probably won't scale well, but will provide a fundamental conceptualization base for nio design code work.

(sorry, I think directly in the code - design patterns are then applied to the code after it's running. What's not delayed is reworked then, not before)

0


source







All Articles