Skip to content

Commit

Permalink
- use AnyResponse instead ByteResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
laststylebender14 committed Oct 21, 2024
1 parent 82c476e commit 336a2a1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/core/app_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use async_graphql::dynamic::{self, DynamicRequest};
use async_graphql_value::ConstValue;
use dashmap::DashMap;

use super::jit::ByteResponse;
use super::jit::AnyResponse;
use crate::core::async_graphql_hyper::OperationId;
use crate::core::auth::context::GlobalAuthContext;
use crate::core::blueprint::{Blueprint, Definition, SchemaModifiers};
Expand All @@ -29,7 +29,7 @@ pub struct AppContext {
pub endpoints: EndpointSet<Checked>,
pub auth_ctx: Arc<GlobalAuthContext>,
pub dedupe_handler: Arc<DedupeResult<IoId, ConstValue, Error>>,
pub dedupe_operation_handler: DedupeResult<OperationId, ByteResponse, Error>,
pub dedupe_operation_handler: DedupeResult<OperationId, AnyResponse<Vec<u8>>, Error>,
pub operation_plans: DashMap<OPHash, OperationPlan<async_graphql_value::Value>>,
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/async_graphql_hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,12 @@ impl CacheControl {
}

pub struct GraphQLArcResponse {
response: JITBatchResponse,
response: JITBatchResponse<Vec<u8>>,
cache_control: Option<CacheControl>,
}

impl GraphQLArcResponse {
pub fn new(response: JITBatchResponse) -> Self {
pub fn new(response: JITBatchResponse<Vec<u8>>) -> Self {
Self { response, cache_control: None }
}

Expand Down
10 changes: 5 additions & 5 deletions src/core/jit/graphql_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use futures_util::stream::FuturesOrdered;
use futures_util::StreamExt;
use tailcall_hasher::TailcallHasher;

use super::{BatchResponse, ByteResponse};
use super::{AnyResponse, BatchResponse};
use crate::core::app_context::AppContext;
use crate::core::async_graphql_hyper::OperationId;
use crate::core::http::RequestContext;
Expand Down Expand Up @@ -37,7 +37,7 @@ impl JITExecutor {
&self,
exec: ConstValueExecutor,
jit_request: jit::Request<ConstValue>,
) -> ByteResponse {
) -> AnyResponse<Vec<u8>> {
let is_introspection_query = self.app_ctx.blueprint.server.get_enable_introspection()
&& exec.plan.is_introspection_query;
let jit_resp = exec
Expand All @@ -60,7 +60,7 @@ impl JITExecutor {
&self,
exec: ConstValueExecutor,
jit_request: jit::Request<ConstValue>,
) -> ByteResponse {
) -> AnyResponse<Vec<u8>> {
let out = self
.app_ctx
.dedupe_operation_handler
Expand Down Expand Up @@ -88,7 +88,7 @@ impl JITExecutor {
pub fn execute(
&self,
request: async_graphql::Request,
) -> impl Future<Output = ByteResponse> + Send + '_ {
) -> impl Future<Output = AnyResponse<Vec<u8>>> + Send + '_ {
let hash = Self::req_hash(&request);

async move {
Expand All @@ -115,7 +115,7 @@ impl JITExecutor {
}

/// Execute a GraphQL batch query.
pub async fn execute_batch(&self, batch_request: BatchRequest) -> BatchResponse {
pub async fn execute_batch(&self, batch_request: BatchRequest) -> BatchResponse<Vec<u8>> {
match batch_request {
BatchRequest::Single(request) => BatchResponse::Single(self.execute(request).await),
BatchRequest::Batch(requests) => {
Expand Down
25 changes: 15 additions & 10 deletions src/core/jit/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ impl Response<async_graphql::Value, jit::Error> {
}
}

pub type ByteResponse = AnyResponse<Vec<u8>>;

/// Represents a GraphQL response in a serialized byte format.
#[derive(Clone)]
pub struct AnyResponse<Body> {
Expand All @@ -81,15 +79,22 @@ pub struct AnyResponse<Body> {
pub is_ok: bool,
}

impl Default for ByteResponse {
impl<Body> Default for AnyResponse<Body>
where
Body: Default,
{
fn default() -> Self {
async_graphql::Response::default().into()
Self {
body: Default::default(),
cache_control: Default::default(),
is_ok: true,
}
}

Check warning on line 92 in src/core/jit/response.rs

View check run for this annotation

Codecov / codecov/patch

src/core/jit/response.rs#L86-L92

Added lines #L86 - L92 were not covered by tests
}

impl From<async_graphql::Response> for ByteResponse {
impl From<async_graphql::Response> for AnyResponse<Vec<u8>> {
fn from(response: async_graphql::Response) -> Self {
ByteResponse {
Self {
cache_control: CacheControl {
max_age: response.cache_control.max_age,
public: response.cache_control.public,
Expand All @@ -104,12 +109,12 @@ impl From<async_graphql::Response> for ByteResponse {
}
}

pub enum BatchResponse {
Single(ByteResponse),
Batch(Vec<ByteResponse>),
pub enum BatchResponse<Body> {
Single(AnyResponse<Body>),
Batch(Vec<AnyResponse<Body>>),
}

impl BatchResponse {
impl<Body> BatchResponse<Body> {
pub fn is_ok(&self) -> bool {
match self {
BatchResponse::Single(s) => s.is_ok,
Expand Down

1 comment on commit 336a2a1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 10.02ms 3.80ms 136.03ms 88.81%
Req/Sec 2.54k 258.15 3.03k 90.67%

303041 requests in 30.01s, 1.52GB read

Requests/sec: 10097.31

Transfer/sec: 51.83MB

Please sign in to comment.