pub struct IncomingRequests<I> { /* private fields */ }Expand description
Tracks requests received from clients (incoming to the server).
When the server receives a request, it registers the request ID along with any metadata needed to process the response. When the server is ready to send a response, it completes the request to retrieve the metadata.
Each request is also associated with a CancellationToken that can be
used to signal cancellation (e.g., when receiving $/cancelRequest).
The generic parameter I represents user-defined metadata associated with
each incoming request (e.g., handler context, timing info, request origin).
§Example
use lsp_server_tokio::{IncomingRequests, RequestId};
use tokio_util::sync::CancellationToken;
let mut incoming: IncomingRequests<String> = IncomingRequests::new();
// Register a request with metadata and cancellation token
let token1 = CancellationToken::new();
let token2 = CancellationToken::new();
incoming.register(1.into(), "textDocument/hover".to_string(), token1);
incoming.register(2.into(), "textDocument/completion".to_string(), token2);
assert_eq!(incoming.pending_count(), 2);
assert!(incoming.is_pending(&1.into()));
// Cancel a request
incoming.cancel(&2.into());
// Complete request and get metadata back
let method = incoming.complete(&1.into());
assert_eq!(method, Some("textDocument/hover".to_string()));
assert_eq!(incoming.pending_count(), 1);Implementations§
Source§impl<I> IncomingRequests<I>
impl<I> IncomingRequests<I>
Sourcepub fn register(&mut self, id: RequestId, data: I, token: CancellationToken)
pub fn register(&mut self, id: RequestId, data: I, token: CancellationToken)
Registers an incoming request with associated metadata and cancellation token.
The metadata can be any user-defined type that you want to associate with this request until it’s completed. The cancellation token can be used to signal request cancellation to async handlers.
Sourcepub fn complete(&mut self, id: &RequestId) -> Option<I>
pub fn complete(&mut self, id: &RequestId) -> Option<I>
Completes an incoming request, removing it from tracking and returning the metadata.
Returns Some(metadata) if the request was pending, None otherwise.
The cancellation token is dropped when the request is completed.
Sourcepub fn is_pending(&self, id: &RequestId) -> bool
pub fn is_pending(&self, id: &RequestId) -> bool
Returns true if the request is currently pending.
Sourcepub fn cancel(&self, id: &RequestId) -> bool
pub fn cancel(&self, id: &RequestId) -> bool
Cancels a pending request by triggering its cancellation token.
Returns true if the request was found and cancelled, false if the
request ID was not pending. Note that cancelling an already-cancelled
token is a no-op.
Sourcepub fn get_token(&self, id: &RequestId) -> Option<CancellationToken>
pub fn get_token(&self, id: &RequestId) -> Option<CancellationToken>
Returns a clone of the cancellation token for a pending request.
Returns None if the request is not pending. The returned token
can be passed to async handlers for cooperative cancellation.
Sourcepub fn pending_count(&self) -> usize
pub fn pending_count(&self) -> usize
Returns the number of currently pending requests.