The message will go through several states to complete:
Queued - Packet serialized and placed in a local channel
Written - The packet was successfully placed on the wire
Finished - The packet was ACK'ed by the remote host
If the first step fails, it would happen immediately before publish returns (meaning the Future is complete upon return from the call). The next two steps might not finish for minutes, hours, or even days - if the client is disconnected and waiting to automatically reconnect later.
A user of the library is asking for a way to determine if the first step fails, without blocking on an await, which could take days to complete.
Some ideas:
await with a timeout?
Something akin to a try_await (non-blocking zero sec timeout)
Return the Future in a timeout, in a non-async function, like:
Oh, yeah. That last one is an interesting; hadn't thought of that.
It could be good because the actual, current, implementation does already return an object that implements Future, but could pretty easily implement the calls for intermediate stages while keeping to current behavior that the object itself implements the overall finish(), so doesn't break the existing API.
I had an API with a similar situation, but I just had the future immediately return the error if the preparation failed. It's possible to poll a future once to check if a result is immediately available.
I personally like this option the most... Especially since the underlying C lib already supports callbacks, so it should be reasonably simple to use those.
This option gives you both immediate feedback (is the initial queue successful) and will notify you with the end result (was the message eventually published successfully) without having to add and manage long running tasks yourself.
That's what I was thinking by the "try" suggestion, because that's the way that the Future object is implemented, internally, already. But I wasn't sure how to do that from the app. So, umm, how do you poll a future once?