Action Engine
Loading...
Searching...
No Matches
close_writes.lua.h
1// Copyright 2025 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef ACTIONENGINE_REDIS_CHUNK_STORE_OPS_CLOSE_WRITES_LUA_H_
16#define ACTIONENGINE_REDIS_CHUNK_STORE_OPS_CLOSE_WRITES_LUA_H_
17
18#include <string_view>
19
20#include "actionengine/redis/chunk_store_ops/unindent.h"
21
22namespace act::redis {
23
24const std::array<std::string, 4> kCloseWritesScriptKeys = {
25 "{}:status", "{}:closed", "{}:final_seq", "{}:events"};
26
27constexpr std::string_view kCloseWritesScriptCode = R"(
28 -- close_writes.lua
29 -- KEYS[1]: <id>:status
30 -- KEYS[2]: <id>:closed
31 -- KEYS[3]: <id>:final_seq
32 -- KEYS[4]: <id>:events
33 -- ARGV[1]: status
34
35 local status = ARGV[1]
36
37 local status_key = KEYS[1]
38 local closed_key = KEYS[2]
39 local final_seq_key = KEYS[3]
40 local events_channel = KEYS[4]
41
42 -- Use SET with NX to prevent a race condition where two clients try to close simultaneously
43 local was_set = redis.call('SET', closed_key, '1', 'NX')
44
45 if was_set then
46 redis.call('SET', status_key, status)
47 -- Notify all listeners that the stream is now closed
48 -- This allows blocking operations to stop waiting and return an error.
49 redis.call('PUBLISH', events_channel, 'CLOSE:' .. status)
50 return 'OK'
51 else
52 return {err = 'ALREADY_CLOSED'}
53 end
54)"_unindent;
55
56} // namespace act::redis
57
58#endif // ACTIONENGINE_REDIS_CHUNK_STORE_OPS_CLOSE_WRITES_LUA_H_