Skip to main content

Crate firq_tower

Crate firq_tower 

Source
Expand description

Tower integration for Firq scheduling.

firq-tower exposes a configurable layer that:

  • extracts a tenant key from requests
  • enqueues requests through Firq
  • supports cancellation before execution turn
  • enforces in-flight limits with a semaphore
  • maps scheduling rejections to HTTP-friendly errors

§Example (header-style tenant extraction)

use firq_tower::{Firq, TenantKey};
use std::collections::HashMap;
use std::convert::Infallible;
use std::future::Ready;
use std::task::{Context, Poll};
use tower::{Service, ServiceBuilder};

#[derive(Clone)]
struct Request {
    headers: HashMap<&'static str, &'static str>,
    body: &'static str,
}

#[derive(Clone)]
struct EchoService;

impl Service<Request> for EchoService {
    type Response = &'static str;
    type Error = Infallible;
    type Future = Ready<Result<Self::Response, Self::Error>>;
    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }
    fn call(&mut self, req: Request) -> Self::Future {
        std::future::ready(Ok(req.body))
    }
}

let layer = Firq::new().build(|req: &Request| {
    req.headers
        .get("x-tenant-id")
        .and_then(|raw| raw.parse::<u64>().ok())
        .map(TenantKey::from)
        .unwrap_or(TenantKey::from(0))
});
let _svc = ServiceBuilder::new().layer(layer).service(EchoService);

Re-exports§

pub use firq_async;

Structs§

AsyncScheduler
Async wrapper around Scheduler.
Firq
Builder for configuring and constructing FirqLayer.
FirqHttpRejection
Structured HTTP rejection payload produced by the layer.
FirqLayer
Tower Layer implementation configured with a Firq scheduler.
FirqPermit
Internal permit payload sent to the background dequeue worker.
FirqService
Tower Service produced by FirqLayer.
QueueTimeBucket
Single histogram bucket for queue time metrics.
Scheduler
Multi-tenant in-process scheduler with DRR fairness and explicit backpressure.
SchedulerConfig
Scheduler runtime configuration.
SchedulerStats
Snapshot of scheduler metrics.
Task
Enqueued unit of work.
TaskHandle
Opaque identifier for a pending task.
TenantCount
Tenant counter entry used in top-talker snapshots.
TenantKey
Stable fairness key used to assign work to shards and DRR queues.

Enums§

BackpressurePolicy
Behavior when enqueue capacity limits are hit.
CancelResult
Result of task cancellation.
CloseMode
Scheduler shutdown mode.
DequeueResult
Result of a dequeue attempt.
EnqueueRejectReason
Reason for enqueue rejection.
EnqueueResult
Result of an enqueue operation.
EnqueueWithHandleResult
Result of enqueue_with_handle.
FirqError
Layer/service error type.
Priority
Scheduling priority for a task.

Traits§

KeyExtractor

Functions§

default_rejection_mapper
Default mapping from scheduler rejection reasons to HTTP responses.

Type Aliases§

RejectionMapper
Function used to map enqueue rejections into HTTP-facing errors.