Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

harpc: move types into harpc-types #5388

Merged
merged 2 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions libs/@local/harpc/codec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,26 @@ publish.workspace = true
[dependencies]
# Public workspace dependencies
error-stack = { workspace = true, public = true, features = ["serde"] }
harpc-types = { workspace = true, public = true }

# Public third-party dependencies
bytes = { workspace = true, public = true }
futures-core = { workspace = true, public = true }
serde = { workspace = true, public = true, features = ["derive"]}

# Private workspace dependencies
proptest = { workspace = true, optional = true }
test-strategy = { workspace = true, optional = true }

# Private third-party dependencies
futures-util = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true, public = true }
pin-project-lite = { workspace = true, optional = true }
memchr = { workspace = true, optional = true }

# Private third-party dependencies

[dev-dependencies]
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }

[lints]
workspace = true

[features]
proptest = ["dep:proptest", "dep:test-strategy"]
json = ["dep:serde_json", "dep:pin-project-lite", "dep:futures-util", "dep:memchr"]
3 changes: 2 additions & 1 deletion libs/@local/harpc/codec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"license": "AGPL-3",
"dependencies": {
"@rust/error-stack": "0.5.0"
"@rust/error-stack": "0.5.0",
"@rust/harpc-types": "0.0.0-private"
}
}
131 changes: 2 additions & 129 deletions libs/@local/harpc/codec/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,137 +1,10 @@
use core::{marker::PhantomData, num::NonZero};
use core::marker::PhantomData;

use bytes::{Buf, BufMut, Bytes, BytesMut};
use harpc_types::error_code::ErrorCode;

use self::kind::ErrorKind;

macro_rules! non_zero {
($n:expr) => {{
// ensure that the value is not 0, in case it is, panic during compile time
const {
assert!($n != 0, "value must not be 0");
}

#[expect(unsafe_code, reason = "checked that it is never 0")]
// SAFETY: $value is not 0
unsafe {
NonZero::new_unchecked($n)
}
}};
}

// we use a macro here to define the error codes, as the code is quite repetetive and also error
// prone, we might not be able to increment values correctly, another problem is that rustfmt will
// reorder the constants, making keeping tracks of the ids harder than it should be.
macro_rules! define_error_code_consts {
($(
$(#[$meta:meta])*
$base:literal => [$(
$(#[$name_meta:meta])*
$name:ident
),+]
),*) => {
$(
$(#[$meta])*
impl ErrorCode {
$(
$(#[$name_meta])*
///
///
/// **Error Code**: `
#[doc = stringify!($base)]
#[doc = "+"]
#[doc = stringify!(${index(0)})]
#[doc = "`"]
pub const $name: Self = Self(non_zero!($base + ${index(0)}));
)+
}
)*
};
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize)]
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
pub struct ErrorCode(NonZero<u16>);

impl ErrorCode {
#[must_use]
pub const fn new(value: NonZero<u16>) -> Self {
Self(value)
}

#[must_use]
pub const fn value(self) -> NonZero<u16> {
self.0
}
}

impl<'de> serde::Deserialize<'de> for ErrorCode {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = u16::deserialize(deserializer)?;

NonZero::new(value)
.map(Self)
.ok_or_else(|| serde::de::Error::custom("value must not be 0"))
}
}

define_error_code_consts! {
// 0xFE_xx = client errors
/// Errors initiated by the client, but that occur on the server.
///
/// These errors are issued on the higher-level tower implementation.
0xFE_10 => [
/// The combination of service and version requirement could not be found on the server.
///
/// The HTTP equivalent is 404 Not Found.
NOT_FOUND
],
// 0xFF_xx = server errors
/// Errors that occur in a session and are issued by the server.
///
/// These errors are issued on the lower-level network layer.
0xFF_00 => [
/// Server is shutting down.
///
/// The server is in the process of shutting down and no longer acceptts new connections.
CONNECTION_SHUTDOWN,
/// Connection transaction limit reached.
///
/// The total count of concurrent transaction per connection has been reached.
CONNECTION_TRANSACTION_LIMIT_REACHED,
/// Instance transaction limit reached.
///
/// The total count of concurrent transaction per server node has been reached.
INSTANCE_TRANSACTION_LIMIT_REACHED,
/// Transaction is lagging behind.
///
/// The client sent too many packets that haven't been processed by the server yet,
/// which lead to packets dropping and the transaction being cancelled.
TRANSACTION_LAGGING
],
/// Errors that occur due to malformed payloads in the tower layer.
0xFF_10 => [
/// Encoded error encountered an invalid error tag.
///
/// The returned payload for an encoded error does not have a valid error tag to distinguish
/// between the different error encodings and could therefore not be properly encoded.
///
/// This is a fault in the implementation of the server, either in the `codec` or
/// the `tower` layer.
PACK_INVALID_ERROR_TAG
],
/// Generic server errors.
0xFF_F0 => [
/// An internal server error occurred.
///
/// An unknown error occurred on the server.
INTERNAL_SERVER_ERROR
]
}

/// An error that is has been fully encoded and can be sent or received over the network.
///
/// Essentially a compiled version of a `NetworkError` or `Report<C>` into it's wire format.
Expand Down
3 changes: 2 additions & 1 deletion libs/@local/harpc/codec/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ use bytes::{Buf, BufMut, Bytes, BytesMut};
use error_stack::Report;
use futures_core::Stream;
use futures_util::stream::StreamExt;
use harpc_types::error_code::ErrorCode;
use serde::{de::DeserializeOwned, ser::Error as _};

use crate::{
decode::{Decoder, ErrorDecoder},
encode::{Encoder, ErrorEncoder},
error::{EncodedError, ErrorBuffer, ErrorCode, NetworkError},
error::{EncodedError, ErrorBuffer, NetworkError},
};

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
Expand Down
4 changes: 3 additions & 1 deletion libs/@local/harpc/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ authors.workspace = true

[dependencies]
# Public workspace dependencies
harpc-codec = { workspace = true, public = true }


# Public third-party dependencies
futures-core = { workspace = true, public = true }
Expand All @@ -30,7 +32,7 @@ tokio = { workspace = true, public = true, features = ["io-util", "macros"] }
codec = { workspace = true, features = ["harpc"] }
error-stack = { workspace = true }
harpc-wire-protocol = { workspace = true }
harpc-codec = { workspace = true }
harpc-types = { workspace = true }

# Private third-party dependencies
bytes = { workspace = true }
Expand Down
4 changes: 1 addition & 3 deletions libs/@local/harpc/net/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"@rust/codec": "0.0.0-private",
"@rust/error-stack": "0.5.0",
"@rust/harpc-codec": "0.0.0-private",
"@rust/harpc-types": "0.0.0-private",
"@rust/harpc-wire-protocol": "0.0.0-private"
},
"devDependencies": {
"@rust/harpc-types": "0.0.0-private"
}
}
6 changes: 2 additions & 4 deletions libs/@local/harpc/net/src/session/client/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ use alloc::sync::Arc;
use bytes::Bytes;
use error_stack::Report;
use futures::{Sink, Stream, StreamExt, prelude::future::FutureExt};
use harpc_wire_protocol::{
request::{Request, procedure::ProcedureDescriptor, service::ServiceDescriptor},
response::Response,
};
use harpc_types::{procedure::ProcedureDescriptor, service::ServiceDescriptor};
use harpc_wire_protocol::{request::Request, response::Response};
use scc::ebr::Guard;
use tachyonix::SendTimeoutError;
use tokio::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::io;
use bytes::{Buf, BufMut, Bytes, BytesMut};
use error_stack::Report;
use futures::{StreamExt, stream};
use harpc_types::response_kind::ResponseKind;
use harpc_wire_protocol::{
flags::BitFlagsOp,
payload::Payload,
Expand All @@ -28,7 +29,6 @@ use harpc_wire_protocol::{
flags::{ResponseFlag, ResponseFlags},
frame::ResponseFrame,
header::ResponseHeader,
kind::ResponseKind,
},
test_utils::mock_request_id,
};
Expand Down
7 changes: 5 additions & 2 deletions libs/@local/harpc/net/src/session/client/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ use core::ops::ControlFlow;

use bytes::Bytes;
use futures::{Stream, StreamExt, prelude::future::FutureExt};
use harpc_types::{
procedure::ProcedureDescriptor, response_kind::ResponseKind, service::ServiceDescriptor,
};
use harpc_wire_protocol::{
flags::BitFlagsOp,
request::{Request, id::RequestId, procedure::ProcedureDescriptor, service::ServiceDescriptor},
request::{Request, id::RequestId},
response::{
Response, begin::ResponseBegin, body::ResponseBody, flags::ResponseFlag,
frame::ResponseFrame, kind::ResponseKind,
frame::ResponseFrame,
},
};
use tokio::{pin, select, sync::mpsc};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::sync::atomic::{AtomicU8, Ordering};

use bytes::Bytes;
use futures::{Stream, StreamExt, stream::FusedStream};
use harpc_codec::error::ErrorCode;
use harpc_types::error_code::ErrorCode;

use crate::stream::TerminatedChannelStream;

Expand Down
3 changes: 1 addition & 2 deletions libs/@local/harpc/net/src/session/client/transaction/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::{assert_matches::assert_matches, num::NonZero, time::Duration};

use bytes::{Bytes, BytesMut};
use futures::StreamExt;
use harpc_codec::error::ErrorCode;
use harpc_types::{error_code::ErrorCode, response_kind::ResponseKind};
use harpc_wire_protocol::{
flags::BitFlagsOp,
payload::Payload,
Expand All @@ -19,7 +19,6 @@ use harpc_wire_protocol::{
flags::{ResponseFlag, ResponseFlags},
frame::ResponseFrame,
header::ResponseHeader,
kind::ResponseKind,
},
test_utils::mock_request_id,
};
Expand Down
2 changes: 1 addition & 1 deletion libs/@local/harpc/net/src/session/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::error::{Error, Request};

use harpc_codec::error::ErrorCode;
use harpc_types::error_code::ErrorCode;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, thiserror::Error)]
#[error("The session layer has encountered an error, the connection has been closed")]
Expand Down
3 changes: 2 additions & 1 deletion libs/@local/harpc/net/src/session/server/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use std::io;

use futures::{FutureExt, Sink, Stream, StreamExt, stream};
use harpc_codec::encode::ErrorEncoder;
use harpc_types::response_kind::ResponseKind;
use harpc_wire_protocol::{
request::{Request, body::RequestBody, id::RequestId},
response::{Response, kind::ResponseKind},
response::Response,
};
use libp2p::PeerId;
use tokio::{
Expand Down
4 changes: 2 additions & 2 deletions libs/@local/harpc/net/src/session/server/connection/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::io;

use bytes::{Buf, Bytes};
use futures::{StreamExt, prelude::sink::SinkExt};
use harpc_codec::{error::ErrorCode, json::JsonCodec};
use harpc_codec::json::JsonCodec;
use harpc_types::{error_code::ErrorCode, response_kind::ResponseKind};
use harpc_wire_protocol::{
flags::BitFlagsOp,
payload::Payload,
Expand All @@ -22,7 +23,6 @@ use harpc_wire_protocol::{
flags::{ResponseFlag, ResponseFlags},
frame::ResponseFrame,
header::ResponseHeader,
kind::ResponseKind,
},
test_utils::mock_request_id,
};
Expand Down
8 changes: 5 additions & 3 deletions libs/@local/harpc/net/src/session/server/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ use bytes::{Bytes, BytesMut};
use error_stack::{Report, ResultExt};
use futures::{SinkExt, Stream, StreamExt};
use harpc_codec::json::JsonCodec;
use harpc_types::{procedure::ProcedureId, service::ServiceId, version::Version};
use harpc_types::{
procedure::{ProcedureDescriptor, ProcedureId},
service::{ServiceDescriptor, ServiceId},
version::Version,
};
use harpc_wire_protocol::{
flags::BitFlagsOp,
payload::Payload,
Expand All @@ -22,8 +26,6 @@ use harpc_wire_protocol::{
flags::{RequestFlag, RequestFlags},
frame::RequestFrame,
header::RequestHeader,
procedure::ProcedureDescriptor,
service::ServiceDescriptor,
},
response::{Response, flags::ResponseFlag},
test_utils::mock_request_id,
Expand Down
Loading
Loading