Pass network-time-based expiration time to backends instead of an interval

This commit is contained in:
Tim Wojtulewicz 2025-02-06 15:18:24 -07:00
parent a485b1d237
commit cad48cebd4
4 changed files with 9 additions and 10 deletions

View file

@ -71,7 +71,8 @@ public:
* @param value the value for the pair. * @param value the value for the pair.
* @param overwrite whether an existing value for a key should be overwritten. * @param overwrite whether an existing value for a key should be overwritten.
* @param expiration_time the time when this entry should be automatically * @param expiration_time the time when this entry should be automatically
* removed. Set to zero to disable expiration. * removed. Set to zero to disable expiration. This time is based on the current network
* time.
* @param cb An optional callback object if being called via an async context. * @param cb An optional callback object if being called via an async context.
* @return An optional value potentially containing an error string if * @return An optional value potentially containing an error string if
* needed Will be unset if the operation succeeded. * needed Will be unset if the operation succeeded.

View file

@ -213,8 +213,6 @@ ErrorResult Redis::DoPut(ValPtr key, ValPtr value, bool overwrite, double expira
if ( expiration_time > 0.0 && ! zeek::run_state::reading_traces ) if ( expiration_time > 0.0 && ! zeek::run_state::reading_traces )
format.append(" PXAT %d"); format.append(" PXAT %d");
double expire_time = expiration_time + run_state::network_time;
auto json_key = key->ToJSON()->ToStdString(); auto json_key = key->ToJSON()->ToStdString();
auto json_value = value->ToJSON()->ToStdString(); auto json_value = value->ToJSON()->ToStdString();
@ -222,7 +220,7 @@ ErrorResult Redis::DoPut(ValPtr key, ValPtr value, bool overwrite, double expira
int status; int status;
if ( expiration_time > 0.0 ) if ( expiration_time > 0.0 )
status = redisAsyncCommand(async_ctx, redisPut, cb, format.c_str(), key_prefix.data(), json_key.data(), status = redisAsyncCommand(async_ctx, redisPut, cb, format.c_str(), key_prefix.data(), json_key.data(),
json_value.data(), static_cast<uint64_t>(expire_time * 1e6)); json_value.data(), static_cast<uint64_t>(expiration_time * 1e6));
else else
status = redisAsyncCommand(async_ctx, redisPut, cb, format.c_str(), key_prefix.data(), json_key.data(), status = redisAsyncCommand(async_ctx, redisPut, cb, format.c_str(), key_prefix.data(), json_key.data(),
json_value.data()); json_value.data());
@ -234,7 +232,7 @@ ErrorResult Redis::DoPut(ValPtr key, ValPtr value, bool overwrite, double expira
redisReply* reply; redisReply* reply;
if ( expiration_time > 0.0 && ! zeek::run_state::reading_traces ) if ( expiration_time > 0.0 && ! zeek::run_state::reading_traces )
reply = (redisReply*)redisCommand(ctx, format.c_str(), key_prefix.data(), json_key.data(), reply = (redisReply*)redisCommand(ctx, format.c_str(), key_prefix.data(), json_key.data(),
json_value.data(), static_cast<uint64_t>(expire_time * 1e6)); json_value.data(), static_cast<uint64_t>(expiration_time * 1e6));
else else
reply = reply =
(redisReply*)redisCommand(ctx, format.c_str(), key_prefix.data(), json_key.data(), json_value.data()); (redisReply*)redisCommand(ctx, format.c_str(), key_prefix.data(), json_key.data(), json_value.data());
@ -254,7 +252,7 @@ ErrorResult Redis::DoPut(ValPtr key, ValPtr value, bool overwrite, double expira
format += " %f %s"; format += " %f %s";
redisReply* reply = redisReply* reply =
(redisReply*)redisCommand(ctx, format.c_str(), key_prefix.data(), expire_time, json_key.data()); (redisReply*)redisCommand(ctx, format.c_str(), key_prefix.data(), expiration_time, json_key.data());
if ( ! reply ) if ( ! reply )
return util::fmt("ZADD operation failed: %s", ctx->errstr); return util::fmt("ZADD operation failed: %s", ctx->errstr);

View file

@ -162,9 +162,6 @@ ErrorResult SQLite::DoPut(ValPtr key, ValPtr value, bool overwrite, double expir
return res; return res;
} }
if ( expiration_time != 0 )
expiration_time += run_state::network_time;
if ( auto res = checkError(sqlite3_bind_double(stmt, 3, expiration_time)); res.has_value() ) { if ( auto res = checkError(sqlite3_bind_double(stmt, 3, expiration_time)); res.has_value() ) {
sqlite3_reset(stmt); sqlite3_reset(stmt);
return res; return res;

View file

@ -114,6 +114,9 @@ function Storage::Async::__put%(backend: opaque of Storage::BackendHandle, key:
else if ( ! b->backend->IsOpen() ) else if ( ! b->backend->IsOpen() )
return val_mgr->Bool(false); return val_mgr->Bool(false);
if ( expire_time > 0.0 )
expire_time += run_state::network_time;
auto cb = new ErrorResultCallback(trigger, frame->GetTriggerAssoc()); auto cb = new ErrorResultCallback(trigger, frame->GetTriggerAssoc());
auto key_v = IntrusivePtr<Val>{NewRef{}, key}; auto key_v = IntrusivePtr<Val>{NewRef{}, key};
auto val_v = IntrusivePtr<Val>{NewRef{}, value}; auto val_v = IntrusivePtr<Val>{NewRef{}, value};