@@ -108,7 +108,7 @@ class NatsStorage : public DistributedStorage {
108
108
109
109
kvConf.bucket = bucket.c_str ();
110
110
kvConf.history = 1 ;
111
- kvConf.ttl = 3600000000000 ; // 1 hour TTL in nanoseconds (3600 * 1e9)
111
+ kvConf.ttl = 86400000000000 ; // 24 hours TTL in nanoseconds (86400 * 1e9)
112
112
113
113
s = g_natsLoader.js_CreateKeyValue (&kv, js, &kvConf);
114
114
if (s != NATS_OK) {
@@ -133,7 +133,7 @@ class NatsStorage : public DistributedStorage {
133
133
if (nc) g_natsLoader.natsConnection_Destroy (nc);
134
134
}
135
135
136
- bool tryAcquire (const std::string& key, int64_t maxTokens ) override {
136
+ bool tryAcquire (const std::string& key, int64_t tokens ) override {
137
137
if (!kv) return false ; // Safety check
138
138
139
139
// NATS JetStream KV Store does not allow colons in key names
@@ -148,7 +148,7 @@ class NatsStorage : public DistributedStorage {
148
148
149
149
if (s == NATS_NOT_FOUND) {
150
150
// Key doesn't exist, initialize it with maxTokens, then decrement by 1
151
- std::string value = std::to_string (maxTokens );
151
+ std::string value = std::to_string (tokens );
152
152
uint64_t rev;
153
153
s = g_natsLoader.kvStore_CreateString (&rev, kv, fullKey.c_str (), value.c_str ());
154
154
@@ -157,8 +157,8 @@ class NatsStorage : public DistributedStorage {
157
157
}
158
158
159
159
// Now decrement by 1 (acquiring 1 token)
160
- if (maxTokens > 0 ) {
161
- std::string newValue = std::to_string (maxTokens - 1 );
160
+ if (tokens > 0 ) {
161
+ std::string newValue = std::to_string (tokens - 1 );
162
162
uint64_t newRev;
163
163
s = g_natsLoader.kvStore_UpdateString (&newRev, kv, fullKey.c_str (), newValue.c_str (), rev);
164
164
return s == NATS_OK;
@@ -245,4 +245,18 @@ class NatsStorage : public DistributedStorage {
245
245
uint64_t newRev;
246
246
g_natsLoader.kvStore_UpdateString (&newRev, kv, fullKey.c_str (), newValue.c_str (), revision);
247
247
}
248
+
249
+ void reset (const std::string& key, int64_t maxTokens) override {
250
+ if (!kv) return ; // Safety check
251
+
252
+ // NATS JetStream KV Store does not allow colons in key names
253
+ std::string sanitizedKey = prefix + key;
254
+ std::replace (sanitizedKey.begin (), sanitizedKey.end (), ' :' , ' _' );
255
+ const std::string fullKey = sanitizedKey;
256
+
257
+ // Set the value to maxTokens regardless of current value
258
+ std::string value = std::to_string (maxTokens);
259
+ uint64_t rev;
260
+ g_natsLoader.kvStore_Put (&rev, kv, fullKey.c_str (), value.c_str (), value.length ());
261
+ }
248
262
};
0 commit comments