Skip to content

Commit

Permalink
fix: merge_right handle for Arc and HashMap (#2917)
Browse files Browse the repository at this point in the history
  • Loading branch information
meskill authored Sep 30, 2024
1 parent 2b518fd commit aab7e07
Show file tree
Hide file tree
Showing 15 changed files with 282 additions and 233 deletions.
175 changes: 0 additions & 175 deletions core/blueprint/wrapping_type.rs

This file was deleted.

7 changes: 4 additions & 3 deletions src/cli/server/http_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@ use hyper::server::conn::AddrIncoming;
use hyper::service::{make_service_fn, service_fn};
use hyper::Server;
use hyper_rustls::TlsAcceptor;
use rustls_pki_types::{CertificateDer, PrivateKeyDer};
use rustls_pki_types::CertificateDer;
use tokio::sync::oneshot;

use super::server_config::ServerConfig;
use crate::core::async_graphql_hyper::{GraphQLBatchRequest, GraphQLRequest};
use crate::core::config::PrivateKey;
use crate::core::http::handle_request;
use crate::core::Errata;

pub async fn start_http_2(
sc: Arc<ServerConfig>,
cert: Vec<CertificateDer<'static>>,
key: Arc<PrivateKeyDer<'static>>,
key: PrivateKey,
server_up_sender: Option<oneshot::Sender<()>>,
) -> anyhow::Result<()> {
let addr = sc.addr();
let incoming = AddrIncoming::bind(&addr)?;
let acceptor = TlsAcceptor::builder()
.with_single_cert(cert, key.clone_key())?
.with_single_cert(cert, key.into_inner())?
.with_http2_alpn()
.with_incoming(incoming);
let make_svc_single_req = make_service_fn(|_conn| {
Expand Down
13 changes: 5 additions & 8 deletions src/core/blueprint/server.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::net::{AddrParseError, IpAddr};
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;

use derive_setters::Setters;
use http::header::{HeaderMap, HeaderName, HeaderValue};
use rustls_pki_types::{CertificateDer, PrivateKeyDer};
use rustls_pki_types::CertificateDer;

use super::Auth;
use crate::core::blueprint::Cors;
use crate::core::config::{self, ConfigModule, HttpVersion, Routes};
use crate::core::config::{self, ConfigModule, HttpVersion, PrivateKey, Routes};
use crate::core::valid::{Valid, ValidationError, Validator};

#[derive(Clone, Debug, Setters)]
Expand Down Expand Up @@ -52,7 +51,7 @@ pub enum Http {
HTTP1,
HTTP2 {
cert: Vec<CertificateDer<'static>>,
key: Arc<PrivateKeyDer<'static>>,
key: PrivateKey,
},
}

Expand Down Expand Up @@ -99,14 +98,12 @@ impl TryFrom<crate::core::config::ConfigModule> for Server {

let cert = config_module.extensions().cert.clone();

let key_file: PrivateKeyDer<'_> = config_module
let key = config_module
.extensions()
.keys
.first()
.ok_or_else(|| ValidationError::new("Key is required for HTTP2".to_string()))?
.clone_key();

let key: Arc<PrivateKeyDer<'_>> = Arc::new(key_file);
.clone();

Valid::succeed(Http::HTTP2 { cert, key })
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ pub struct Config {
/// A list of all links in the schema.
#[serde(default, skip_serializing_if = "is_default")]
pub links: Vec<Link>,
#[serde(default, skip_serializing_if = "is_default")]

/// Enable [opentelemetry](https://opentelemetry.io) support
#[serde(default, skip_serializing_if = "is_default")]
pub telemetry: Telemetry,
}

Expand Down
24 changes: 22 additions & 2 deletions src/core/config/config_module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::{HashMap, HashSet};
use std::ops::Deref;
use std::sync::Arc;

use jsonwebtoken::jwk::JwkSet;
use prost_reflect::prost_types::{FileDescriptorProto, FileDescriptorSet};
Expand Down Expand Up @@ -108,6 +107,27 @@ impl<A> Deref for Content<A> {
}
}

#[derive(Debug)]
pub struct PrivateKey(PrivateKeyDer<'static>);

impl Clone for PrivateKey {
fn clone(&self) -> Self {
Self(self.0.clone_key())
}
}

impl From<PrivateKeyDer<'static>> for PrivateKey {
fn from(value: PrivateKeyDer<'static>) -> Self {
Self(value)
}
}

impl PrivateKey {
pub fn into_inner(self) -> PrivateKeyDer<'static> {
self.0
}
}

/// Extensions are meta-information required before we can generate the
/// blueprint. Typically, this information cannot be inferred without performing
/// an IO operation, i.e., reading a file, making an HTTP call, etc.
Expand All @@ -123,7 +143,7 @@ pub struct Extensions {
pub cert: Vec<CertificateDer<'static>>,

/// Contains the key used on HTTP2 with TLS
pub keys: Arc<Vec<PrivateKeyDer<'static>>>,
pub keys: Vec<PrivateKey>,

/// Contains the endpoints
pub endpoint_set: EndpointSet<Unchecked>,
Expand Down
1 change: 0 additions & 1 deletion src/core/config/cors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use serde::{Deserialize, Serialize};
use crate::core::http::Method;
use crate::core::is_default;
use crate::core::macros::MergeRight;
use crate::core::merge_right::MergeRight;

/// Type to configure Cross-Origin Resource Sharing (CORS) for a server.
#[derive(
Expand Down
1 change: 0 additions & 1 deletion src/core/config/directives/federation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use serde::{Deserialize, Serialize};
use tailcall_macros::MergeRight;

use crate::core::config::Resolver;
use crate::core::merge_right::MergeRight;

/// Directive `@key` for Apollo Federation
#[derive(
Expand Down
1 change: 0 additions & 1 deletion src/core/config/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::core::config::cors::Cors;
use crate::core::config::KeyValue;
use crate::core::is_default;
use crate::core::macros::MergeRight;
use crate::core::merge_right::MergeRight;

#[derive(
Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, schemars::JsonSchema, MergeRight,
Expand Down
11 changes: 4 additions & 7 deletions src/core/config/reader.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::path::Path;
use std::sync::Arc;

use rustls_pemfile;
use rustls_pki_types::{
CertificateDer, PrivateKeyDer, PrivatePkcs1KeyDer, PrivatePkcs8KeyDer, PrivateSec1KeyDer,
};
use url::Url;

use super::{ConfigModule, Content, Link, LinkType};
use super::{ConfigModule, Content, Link, LinkType, PrivateKey};
use crate::core::config::{Config, ConfigReaderContext, Source};
use crate::core::merge_right::MergeRight;
use crate::core::proto_reader::ProtoReader;
Expand Down Expand Up @@ -95,7 +94,7 @@ impl ConfigReader {
LinkType::Key => {
let source = self.resource_reader.read_file(path).await?;
let content = source.content;
extensions.keys = Arc::new(self.load_private_key(content).await?)
extensions.keys = self.load_private_key(content).await?
}
LinkType::Operation => {
let source = self.resource_reader.read_file(path).await?;
Expand Down Expand Up @@ -148,10 +147,7 @@ impl ConfigReader {
}

/// Reads a private key from a given file
async fn load_private_key(
&self,
content: String,
) -> anyhow::Result<Vec<PrivateKeyDer<'static>>> {
async fn load_private_key(&self, content: String) -> anyhow::Result<Vec<PrivateKey>> {
let keys = rustls_pemfile::read_all(&mut content.as_bytes())?;

Ok(keys
Expand All @@ -168,6 +164,7 @@ impl ConfigReader {
}
_ => None,
})
.map(PrivateKey::from)
.collect())
}

Expand Down
1 change: 0 additions & 1 deletion src/core/config/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use tailcall_macros::{CustomResolver, MergeRight};

use super::{Call, EntityResolver, Expr, GraphQL, Grpc, Http, JS};
use crate::core::directive::DirectiveCodec;
use crate::core::merge_right::MergeRight;
use crate::core::valid::{Valid, Validator};

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
2 changes: 1 addition & 1 deletion src/core/config/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::core::config::headers::Headers;
use crate::core::config::KeyValue;
use crate::core::is_default;
use crate::core::macros::MergeRight;
use crate::core::merge_right::MergeRight;

#[derive(
Serialize,
Expand Down Expand Up @@ -284,6 +283,7 @@ impl Server {
mod tests {
use super::*;
use crate::core::config::ScriptOptions;
use crate::core::merge_right::MergeRight;

fn server_with_script_options(so: ScriptOptions) -> Server {
Server { script: Some(so), ..Default::default() }
Expand Down
Loading

1 comment on commit aab7e07

@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 11.27ms 4.47ms 135.06ms 87.23%
Req/Sec 2.25k 182.42 2.78k 72.67%

268442 requests in 30.03s, 1.35GB read

Requests/sec: 8937.76

Transfer/sec: 45.87MB

Please sign in to comment.