Skip to content

Commit

Permalink
Remove inmemory wrapper around redis
Browse files Browse the repository at this point in the history
  • Loading branch information
afsalthaj committed Dec 9, 2024
1 parent 36804ce commit 35e188c
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 117 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion golem-worker-service-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ tracing-subscriber = { workspace = true }
url = { workspace = true }
uuid = { workspace = true }
wasm-wave = { workspace = true }
log = "0.4.22"

[dev-dependencies]
criterion = { version = "0.3", features = ["html_reports"] }
Expand Down
74 changes: 0 additions & 74 deletions golem-worker-service-base/src/gateway_execution/gateway_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ use bincode::enc::Encoder;
use bincode::error::EncodeError;
use bytes::Bytes;
use fred::interfaces::RedisResult;
use golem_common::cache::{BackgroundEvictionMode, Cache, FullCacheEvictionMode, SimpleCache};
use golem_common::redis::RedisPool;
use golem_common::SafeDisplay;
use log::info;
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Arc;
use std::time::Duration;
use tracing::error;

#[async_trait]
Expand Down Expand Up @@ -206,74 +203,3 @@ impl GatewaySession for RedisGatewaySession {
}
}
}

pub struct GatewaySessionWithInMemoryCache<A> {
backend: A,
cache: Cache<(SessionId, DataKey), (), DataValue, GatewaySessionError>,
}

impl<A> GatewaySessionWithInMemoryCache<A> {
pub fn new(
inner: A,
in_memory_expiration_in_seconds: i64,
eviction_period_in_seconds: u64,
) -> Self {
let cache = Cache::new(
Some(1024),
FullCacheEvictionMode::None,
BackgroundEvictionMode::OlderThan {
ttl: Duration::from_secs(in_memory_expiration_in_seconds as u64),
period: Duration::from_secs(eviction_period_in_seconds),
},
"gateway_session_in_memory",
);

Self {
backend: inner,
cache,
}
}
}

#[async_trait]
impl<A: GatewaySession + Sync + Clone + Send + 'static> GatewaySession
for GatewaySessionWithInMemoryCache<A>
{
async fn insert(
&self,
session_id: SessionId,
data_key: DataKey,
data_value: DataValue,
) -> Result<(), GatewaySessionError> {
info!("Inserting session data to the backend");

self.backend
.insert(session_id, data_key, data_value)
.await?;

info!("Inserted session data into cache");

Ok(())
}

async fn get(
&self,
session_id: &SessionId,
data_key: &DataKey,
) -> Result<DataValue, GatewaySessionError> {
info!("Getting session data from cache");
let result = self
.cache
.get_or_insert_simple(&(session_id.clone(), data_key.clone()), || {
let inner = self.backend.clone();
let session_id = session_id.clone();
let data_key = data_key.clone();

Box::pin(async move { inner.get(&session_id, &data_key).await })
})
.await?;

info!("Got session data from cache");
Ok(result)
}
}
2 changes: 1 addition & 1 deletion golem-worker-service-base/src/gateway_middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl HttpMiddlewares {
HttpMiddleware::AddCorsHeaders(_) => {}
HttpMiddleware::AuthenticateRequest(auth) => {
let result = auth
.apply_http_auth(&http_request_details, &session_store, &identity_provider)
.apply_http_auth(http_request_details, &session_store, &identity_provider)
.await?;

match result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1619,8 +1619,7 @@ mod internal {
use rib::RibResult;

use golem_worker_service_base::gateway_execution::gateway_session::{
DataKey, DataValue, GatewaySession, GatewaySessionError, GatewaySessionStore,
GatewaySessionWithInMemoryCache, SessionId,
DataKey, DataValue, GatewaySession, GatewaySessionError, GatewaySessionStore, SessionId,
};
use serde_json::Value;
use std::collections::HashMap;
Expand Down Expand Up @@ -2008,11 +2007,7 @@ mod internal {
}

pub fn get_session_store() -> GatewaySessionStore {
Arc::new(GatewaySessionWithInMemoryCache::new(
TestSessionBackEnd::new(),
60 * 60,
60,
))
Arc::new(TestSessionBackEnd::new())
}

struct NoopTestSessionBackend;
Expand Down
34 changes: 1 addition & 33 deletions golem-worker-service-base/tests/services_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use golem_worker_service_base::gateway_api_deployment::{
ApiDeploymentRequest, ApiSite, ApiSiteString,
};
use golem_worker_service_base::gateway_execution::gateway_session::{
DataKey, DataValue, GatewaySession, GatewaySessionError, GatewaySessionWithInMemoryCache,
DataKey, DataValue, GatewaySession, GatewaySessionError,
RedisGatewaySession, SessionId,
};
use golem_worker_service_base::gateway_security::{
Expand Down Expand Up @@ -216,17 +216,6 @@ pub async fn test_gateway_session_expiry() {
Err(GatewaySessionError::MissingValue { .. })
));

// Redis backed by in-memory cache should return value
let result = insert_and_get_with_redis_with_in_memory_cache(
SessionId("test2".to_string()),
DataKey::nonce(),
data_value.clone(),
&redis,
)
.await
.expect("Expecting a value from redis cache backed by in-memory");

assert_eq!(result, data_value);
}

async fn insert_and_get_with_redis(
Expand All @@ -249,27 +238,6 @@ async fn insert_and_get_with_redis(
session_store.get(&session_id, &data_key).await
}

async fn insert_and_get_with_redis_with_in_memory_cache(
session_id: SessionId,
data_key: DataKey,
data_value: DataValue,
redis: &RedisPool,
) -> Result<DataValue, GatewaySessionError> {
let redis_session = RedisGatewaySession::new(redis.clone(), 60 * 60);
let redis_with_in_memory = Arc::new(GatewaySessionWithInMemoryCache::new(
redis_session.clone(),
60 * 60,
60,
));

redis_with_in_memory
.insert(session_id.clone(), data_key.clone(), data_value.clone())
.await
.unwrap();

redis_with_in_memory.get(&session_id, &data_key).await
}

#[test]
pub async fn test_with_sqlite_db() {
let db = SqliteDb::default();
Expand Down

0 comments on commit 35e188c

Please sign in to comment.