Tristanable

Tag-based asynchronous messaging library



[Request (Tag: 69, Arrived: false, Used: false), Request (Tag: 70, Arrived: true, Used: false)]
[Request (Tag: 69, Arrived: false, Used: false), Request (Tag: 70, Arrived: true, Used: false)]
[Request (Tag: 69, Arrived: false, Used: false), Request (Tag: 70, Arrived: true, Used: false)]
[Request (Tag: 69, Arrived: false, Used: false), Request (Tag: 70, Arrived: true, Used: false)]
[Request (Tag: 69, Arrived: false, Used: false), Request (Tag: 70, Arrived: true, Used: false)]
[1]
[2]
[Request (Tag: 69, Arrived: true, Used: true), Request (Tag: 70, Arrived: true, Used: true), Request (Tag: 70, Arrived: false, Used: false)]
[Request (Tag: 69, Arrived: true, Used: true), Request (Tag: 70, Arrived: true, Used: true), Request (Tag: 70, Arrived: false, Used: false)]
[Request (Tag: 69, Arrived: true, Used: true), Request (Tag: 70, Arrived: true, Used: true), Request (Tag: 70, Arrived: false, Used: false)]
[Request (Tag: 69, Arrived: true, Used: true), Request (Tag: 70, Arrived: true, Used: true), Request (Tag: 70, Arrived: false, Used: false)]
[Request (Tag: 69, Arrived: true, Used: true), Request (Tag: 70, Arrived: true, Used: true), Request (Tag: 70, Arrived: false, Used: false)]
[2]
[Request (Tag: 69, Arrived: true, Used: true), Request (Tag: 70, Arrived: true, Used: true), Request (Tag: 70, Arrived: true, Used: true)]


What is it though?

Tristanable is a library I created, like bformat, to ease the development of a lot of projects I
am working on during this time that require similar baseline protocols to work. In the case
of the former, I wanted a library that would solve the problem as described below (this is by
far the best way I can describe the use of this project):

So you first have a client and a server. The client sends jobs to the server and the server then
executes them on separate threads, sending back a reply for the job completed. Now a job
entails the client sending something to the server, the server doing some work for that job on
a separate thread and then sending that reply back to the client. The jobs, in this example, are
independent of each other, so the order you send them in and get their results back doesn't matter
but you just have to get, in this case, the two of them back.

Here is the performance problem that occurs, say now performing job 1, the first send-work-reply
procedure that occurs, takes 1 hour and only after a blocking receive after that 1 hour can you
proceed to then do the next blocking receive (after a send of course) for job 2? You see, we could
have gotten job 2 back first whilst job 1 was working (the implication that job one takes significantly
less time than job 1). But this implies the server waiting for job 1 to complete and then only queuing it
to the network buffer for sending and then queuing the server-side finished job 2 to it as well.

The solution? Well the job context is derived from the state machine implied by the ordering of the
socket calls but if we go out of order we then need order again, we can simply tag the messages - this is
what tristanable does!

Tristanable watches the socket, and in this case the server (upon job completion of job 2 - as per our
example) would immediately send the job 2 result back, and when it receives the results for job 2 back
it then will let any blocking call to receive(tagID) through. So we would implement this with a thread
per job on the client side - making the separate blocking calls possible - concurrently but the one job
that takes an hour will not hold ours up for an hour and a little bit - job 2's thread handler can receive its
results and do what it needs to with them.

Using tristanable in your D project

You can easily add the library (source-based) to your project by running the following command in your
project's root:

dub add tristanable

Acknowledgments

I had to make amends with a friend for calling one of my projects by a certain name (of a certain someone)
over one of her suggestions so I decided to take her suggestion, "tristanable", and call this project after it!

Thanks Gabby Smuts (@kwaranpyn - check out her really cool art)