pub struct LookupHandler<K, V, E> {
pub map: Arc<Mutex<HashMap<K, SenderList<V, E>>>>,
/* private fields */
}
Expand description
LookupHandler
provides ability to queue multiple async requests for the same key
into a group of futures that resolve upon request completion.
To use LookupHandler
, you need to create a custom lookup function. The example below
declares a function lookup()
that uses LookupHandler
to queue requests
and if there are no pending requests (request is new) performs the actual
request by calling lookup_impl()
. The LookupHandler::complete()
will
resolve all pending futures for the specific key.
Example:
...
pub lookup_handler : LookupHandler<Pubkey,Arc<Data>,Error>
...
async fn lookup(&self, pubkey:&Pubkey) -> Result<Option<Arc<Data>>> {
let request_type = self.lookup_handler.queue(pubkey).await;
let result = match request_type {
RequestType::New(receiver) => {
// execute the actual lookup
let response = self.lookup_impl(pubkey).await;
// signal completion for all awaiting futures
lookup_handler.complete(pubkey, response).await;
// this request is queued like all the others
// so wait for your own notification as well
receiver.recv().await?
},
RequestType::Pending(receiver) => {
receiver.recv().await?
}
}
};
Fields§
§map: Arc<Mutex<HashMap<K, SenderList<V, E>>>>
Implementations§
Source§impl<K, V, E> LookupHandler<K, V, E>
impl<K, V, E> LookupHandler<K, V, E>
Sourcepub async fn queue(&self, key: &K) -> RequestType<V, E>
pub async fn queue(&self, key: &K) -> RequestType<V, E>
Queue the request for key K
. Returns RequestType::New
if
no other requests for the same key are pending and RequestType::Pending
if there are pending requests. Both RequestType
values contain a async_std::channel::Receiver
that can be listened to for lookup completion. Lookup completion
can be signaled by LookupHandler::complete()
Sourcepub async fn complete(&self, key: &K, result: LookupResult<V, E>)
pub async fn complete(&self, key: &K, result: LookupResult<V, E>)
Signal the lookup completion for key K
by supplying a LookupResult
with a resulting value V
or an error E
.