From ff5cb0cbae2746749efc111fd9476b489de073c9 Mon Sep 17 00:00:00 2001 From: Abid Omar Date: Wed, 23 Oct 2024 12:48:39 +0800 Subject: [PATCH] imp: add support for custom executors --- Cargo.toml | 2 ++ src/auth.rs | 2 +- src/lib.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3583fe17..0c63c55d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,9 @@ bytes = "1.0.1" chrono = { version = "0.4.19", default-features = false, features = [ "serde", "clock", + "wasmbind" ] } +web-time = { version = "1.1.0", features = ["serde"] } cfg-if = "1.0.0" either = "1.8.0" futures = { version = "0.3.15" } diff --git a/src/auth.rs b/src/auth.rs index 89521eb7..f92c10ce 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -7,7 +7,7 @@ use jsonwebtoken::{Algorithm, EncodingKey, Header}; use secrecy::{ExposeSecret, SecretString}; use serde::{Deserialize, Serialize}; use std::fmt; -use std::time::{Duration, SystemTime}; +use web_time::{Duration, SystemTime}; use snafu::*; diff --git a/src/lib.rs b/src/lib.rs index f1aae9ac..720222ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -203,11 +203,13 @@ use http_body_util::BodyExt; use service::middleware::auth_header::AuthHeaderLayer; use std::convert::{Infallible, TryInto}; use std::fmt; +use std::future::Future; use std::io::Write; use std::marker::PhantomData; +use std::pin::Pin; use std::str::FromStr; use std::sync::{Arc, RwLock}; -use std::time::Duration; +use web_time::Duration; use http::{header::HeaderName, StatusCode}; use hyper::{Request, Response}; @@ -385,6 +387,7 @@ pub struct OctocrabBuilder { auth: Auth, config: Config, _layer_ready: PhantomData, + executor: Option>>) -> ()>>, } //Indicates weather the builder supports config @@ -407,6 +410,7 @@ impl OctocrabBuilder { auth: NoAuth {}, config: NoConfig {}, _layer_ready: PhantomData, + executor: None, } } } @@ -424,6 +428,29 @@ impl OctocrabBuilder { auth: self.auth, config: self.config, _layer_ready: PhantomData, + executor: None, + } + } +} + +impl OctocrabBuilder +where + Svc: Service, Response = Response> + Send + 'static, + Svc::Future: Send + 'static, + Svc::Error: Into, + B: http_body::Body + Send + 'static, + B::Error: Into, +{ + pub fn with_executor( + self, + executor: Box>>) -> ()>, + ) -> OctocrabBuilder { + OctocrabBuilder { + service: self.service, + auth: self.auth, + config: self.config, + _layer_ready: PhantomData, + executor: Some(executor), } } } @@ -445,12 +472,14 @@ where service: stack, auth, config, + executor, .. } = self; OctocrabBuilder { service: layer.layer(stack), auth, config, + executor, _layer_ready: PhantomData, } } @@ -467,6 +496,7 @@ impl OctocrabBuilder { OctocrabBuilder { service: self.service, auth: self.auth, + executor: self.executor, config, _layer_ready: PhantomData, } @@ -490,6 +520,10 @@ where .layer(self.service) .map_err(|e| e.into()); + if let Some(executor) = self.executor { + return Ok(Octocrab::new_with_executor(service, self.auth, executor)); + } + Ok(Octocrab::new(service, self.auth)) } } @@ -500,6 +534,7 @@ impl OctocrabBuilder { service: self.service, auth, config: self.config, + executor: self.executor, _layer_ready: PhantomData, } } @@ -796,6 +831,10 @@ impl OctocrabBuilder let client = AuthHeaderLayer::new(auth_header, base_uri, upload_uri).layer(client); + if let Some(executor) = self.executor { + return Ok(Octocrab::new_with_executor(client, auth_state, executor)); + } + Ok(Octocrab::new(client, auth_state)) } } @@ -1003,6 +1042,31 @@ impl Octocrab { } } + /// Creates a new `Octocrab` with a custom executor + fn new_with_executor( + service: S, + auth_state: AuthState, + executor: Box>>) -> ()>, + ) -> Self + where + S: Service, Response = Response>> + + Send + + 'static, + S::Future: Send + 'static, + S::Error: Into, + { + // Use Buffer pair to return the background worker + let (service, worker) = Buffer::pair(BoxService::new(service.map_err(Into::into)), 1024); + + // Execute the background worker with the custom executor + executor(Box::pin(worker)); + + Self { + client: service, + auth_state, + } + } + /// Returns a new `Octocrab` based on the current builder but /// authorizing via a specific installation ID. /// Typically you will first construct an `Octocrab` using