Action Engine
Loading...
Searching...
No Matches
pubsub.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_PUBSUB_H_
16#define ACTIONENGINE_REDIS_PUBSUB_H_
17
18#include <thread/channel.h>
19
21#include "actionengine/redis/reply.h"
22
23namespace act::redis {
24class Subscription {
25 public:
26 static constexpr size_t kDefaultChannelCapacity = 16;
27
28 explicit Subscription(size_t capacity = kDefaultChannelCapacity);
29 explicit Subscription(absl::AnyInvocable<void(Reply)> on_message);
30
31 ~Subscription();
32
33 thread::Case OnSubscribe() const;
34 thread::Case OnUnsubscribe() const;
35
36 thread::Reader<Reply>* GetReader() { return channel_.reader(); }
37
38 private:
39 friend class Redis;
40
41 void Message(Reply reply);
42 void Subscribe();
43 void Unsubscribe();
44
45 void CloseWriter() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_);
46
47 mutable act::Mutex mu_;
48 bool writer_closed_ ABSL_GUARDED_BY(mu_) = false;
49
50 thread::Channel<Reply> channel_;
51 absl::AnyInvocable<void(Reply)> on_message_;
52 thread::PermanentEvent subscribe_event_;
53 thread::PermanentEvent unsubscribe_event_;
54};
55} // namespace act::redis
56
57#endif // ACTIONENGINE_REDIS_PUBSUB_H_
Concurrency utilities for ActionEngine.