Skip to content
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
50 changes: 50 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ members = [
axum-test = "17"
base64 = "0.22"
bcrypt = "0.13.0"
ciborium = "0.2"
bytes = "1.6.0"
cjson = "0.1"
colored_json = "5.0.0"
Expand Down
5 changes: 5 additions & 0 deletions engine/artifacts/errors/guard.invalid_request.json

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

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

5 changes: 5 additions & 0 deletions engine/artifacts/errors/guard.query_duplicate_param.json

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

5 changes: 5 additions & 0 deletions engine/artifacts/errors/guard.query_empty_actor_name.json

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

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

5 changes: 5 additions & 0 deletions engine/artifacts/errors/guard.query_invalid_base64_input.json

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

5 changes: 5 additions & 0 deletions engine/artifacts/errors/guard.query_invalid_cbor_input.json

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

5 changes: 5 additions & 0 deletions engine/artifacts/errors/guard.query_invalid_params.json

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

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

5 changes: 5 additions & 0 deletions engine/artifacts/errors/guard.query_missing_runner_name.json

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

5 changes: 5 additions & 0 deletions engine/artifacts/errors/guard.query_no_runner_configs.json

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

5 changes: 5 additions & 0 deletions engine/artifacts/errors/guard.query_param_missing_equals.json

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

5 changes: 5 additions & 0 deletions engine/artifacts/errors/guard.query_path_token_syntax.json

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

5 changes: 5 additions & 0 deletions engine/artifacts/errors/guard.query_unknown_param.json

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

6 changes: 6 additions & 0 deletions engine/packages/guard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ path = "src/lib.rs"
[dependencies]
anyhow.workspace = true
axum.workspace = true
base64.workspace = true
bytes.workspace = true
ciborium.workspace = true
futures.workspace = true
gas.workspace = true
http-body-util.workspace = true
Expand All @@ -23,19 +25,23 @@ tower.workspace = true
hyper = "1.6.0"
indoc.workspace = true
lazy_static.workspace = true
namespace.workspace = true
once_cell.workspace = true
pegboard-envoy.workspace = true
pegboard-gateway.workspace = true
pegboard-gateway2.workspace = true
pegboard-runner.workspace = true
pegboard.workspace = true
regex.workspace = true
rivet-api-types.workspace = true
rivet-api-util.workspace = true
rivet-api-builder.workspace = true
rivet-api-public.workspace = true
rivet-cache.workspace = true
rivet-config.workspace = true
rivet-data.workspace = true
rivet-error.workspace = true
rivet-types.workspace = true
rivet-guard-core.workspace = true
rivet-logs.workspace = true
rivet-metrics.workspace = true
Expand Down
69 changes: 62 additions & 7 deletions engine/packages/guard/src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use rivet_guard_core::{CacheKeyFn, request_context::RequestContext};
pub mod pegboard_gateway;

use crate::routing::{
SEC_WEBSOCKET_PROTOCOL, WS_PROTOCOL_TARGET, X_RIVET_TARGET, parse_actor_path,
SEC_WEBSOCKET_PROTOCOL, WS_PROTOCOL_TARGET, X_RIVET_TARGET,
actor_path::{self, QueryActorPathInfo},
};

/// Creates the main cache key function that handles all incoming requests
Expand All @@ -21,13 +22,25 @@ pub fn create_cache_key_function() -> CacheKeyFn {

// MARK: Path-based cache key
// Check for path-based actor routing
if let Some(actor_path_info) = parse_actor_path(req_ctx.path()) {
tracing::debug!("using path-based cache key for actor");
if let Some(actor_path_info) = actor_path::parse_actor_path(req_ctx.path())? {
match actor_path_info {
actor_path::ParsedActorPath::Direct(actor_path_info) => {
tracing::debug!("using path-based cache key for actor");

if let Ok(cache_key) =
pegboard_gateway::build_cache_key_path_based(req_ctx, &actor_path_info)
{
return Ok(cache_key);
if let Ok(cache_key) =
pegboard_gateway::build_cache_key_path_based(req_ctx, &actor_path_info)
{
return Ok(cache_key);
}
}
actor_path::ParsedActorPath::Query(query_path_info) => {
// Hash only the routing-relevant query fields (namespace,
// name, method, key, input, region, crashPolicy). The token
// is excluded because it does not affect which actor the
// request routes to.
tracing::debug!("using query-path cache key for actor");
return Ok(query_path_cache_key(&query_path_info, req_ctx));
}
}
}

Expand Down Expand Up @@ -74,3 +87,45 @@ fn host_path_method_cache_key(req_ctx: &RequestContext) -> u64 {
req_ctx.method().as_str().hash(&mut hasher);
hasher.finish()
}

/// Build a cache key from only the routing-relevant fields of a query gateway
/// path. Token is intentionally excluded so requests with different tokens but
/// the same query resolve to the same cached route.
fn query_path_cache_key(info: &QueryActorPathInfo, req_ctx: &RequestContext) -> u64 {
use crate::routing::actor_path::QueryActorQuery;

let mut hasher = DefaultHasher::new();
match &info.query {
QueryActorQuery::Get {
namespace,
name,
key,
} => {
"get".hash(&mut hasher);
namespace.hash(&mut hasher);
name.hash(&mut hasher);
key.hash(&mut hasher);
}
QueryActorQuery::GetOrCreate {
namespace,
name,
runner_name,
key,
input,
region,
crash_policy,
} => {
"getOrCreate".hash(&mut hasher);
namespace.hash(&mut hasher);
name.hash(&mut hasher);
runner_name.hash(&mut hasher);
key.hash(&mut hasher);
input.hash(&mut hasher);
region.hash(&mut hasher);
crash_policy.hash(&mut hasher);
}
}
info.stripped_path.hash(&mut hasher);
req_ctx.method().as_str().hash(&mut hasher);
hasher.finish()
}
3 changes: 2 additions & 1 deletion engine/packages/guard/src/cache/pegboard_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use gas::prelude::*;
use rivet_guard_core::request_context::RequestContext;

use crate::routing::{
ActorPathInfo, SEC_WEBSOCKET_PROTOCOL, WS_PROTOCOL_ACTOR, pegboard_gateway::X_RIVET_ACTOR,
SEC_WEBSOCKET_PROTOCOL, WS_PROTOCOL_ACTOR, actor_path::ActorPathInfo,
pegboard_gateway::X_RIVET_ACTOR,
};

/// Build cache key for path-based actor routing
Expand Down
Loading
Loading