nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
redis.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_REDIS_H_
2#define NANOMMO_SERVER_REDIS_H_
3
4#include <hiredis.h>
5
6#include <chrono>
7#include <map>
8#include <memory>
9#include <string>
10#include <vector>
11
12#include "absl/status/statusor.h"
13
14namespace nanommo {
15
16/// How long Redis has to answer, and what happens when it doesn't. A world's
17/// tick waits for its saves and its leases, so a Redis that hangs would hang
18/// every world.
20 /// For connecting, and then for each reply.
21 std::chrono::milliseconds answer{250};
22 /// After Redis didn't answer in time, or couldn't be reached, each call
23 /// fails at once for this long, without asking it, so that a tick pays for
24 /// one timeout a second, not one each call.
25 std::chrono::milliseconds fail_fast{1000};
26};
27
28class Redis {
29 public:
30 /// Connects to Redis, or returns why it couldn't.
31 static absl::StatusOr<std::unique_ptr<Redis>> Create(
32 const std::string& host, int port = 6379, RedisTimeouts timeouts = {});
33 ~Redis();
34
35 Redis(const Redis&) = delete;
36 Redis& operator=(const Redis&) = delete;
37
38 /// Returns the reply as text, or a NotFound error when Redis says nil. A
39 /// command that loses the connection fails, and the next one reconnects.
40 /// One that Redis doesn't answer in time fails as Unavailable, and so does
41 /// each after it, at once, until the timeouts' fail_fast is over; its
42 /// connection goes, as the reply might come later, as another's.
43 absl::StatusOr<std::string> Command(const std::vector<std::string>& words);
44
45 /// Sends all the commands before reading any reply, so they share a single
46 /// round trip. Each reply comes back as a list of values, empty for nil.
47 absl::StatusOr<std::vector<std::vector<std::string>>> Pipeline(
48 const std::vector<std::vector<std::string>>& commands);
49
50 /// Runs the commands as one transaction, between MULTI and EXEC, in a
51 /// single round trip: Redis runs them all at once, with no one else's
52 /// commands in between, or none of them if it couldn't take one. Fails if
53 /// any of them failed.
54 absl::Status Transaction(
55 const std::vector<std::vector<std::string>>& commands);
56
57 /// Runs the commands as one, as Transaction does, but only if each of the
58 /// keys holds the value it's paired with: a Lua script checks them and runs
59 /// the commands, in a single round trip, with no one else's commands in
60 /// between. Returns the keys that don't, when it ran none of the commands.
61 /// Fails if a command failed, which doesn't undo the others.
62 absl::StatusOr<std::vector<std::string>> TransactionIf(
63 const std::map<std::string, std::string>& holding,
64 const std::vector<std::vector<std::string>>& commands);
65
66 /// An entry of a stream: its id, and its fields and values, one after the
67 /// other.
68 struct Entry {
69 std::string id;
70 std::vector<std::string> fields;
71 };
72
73 /// Reads a stream, with XREAD or XREADGROUP on a single stream: its entries,
74 /// or none when the command waited and nothing came. One that BLOCKs waits
75 /// that long for Redis, and the timeout on top, and one that BLOCKs for 0
76 /// as long as Redis does: for ever, until an entry comes.
77 absl::StatusOr<std::vector<Entry>> Read(
78 const std::vector<std::string>& words);
79
80 private:
81 Redis(redisContext* context, RedisTimeouts timeouts)
82 : context_(context), timeouts_(timeouts) {}
83
84 /// Whether to ask Redis at all: not while failing fast, and only once the
85 /// connection's back, if it was lost.
86 absl::Status Ready();
87 /// The next reply, or why there's none.
88 absl::StatusOr<std::unique_ptr<redisReply, decltype(&freeReplyObject)>>
89 NextReply();
90 /// Fails each call at once for the timeouts' fail_fast.
91 void FailFast();
92
93 redisContext* context_;
94 RedisTimeouts timeouts_;
95 std::chrono::steady_clock::time_point failing_until_;
96};
97
98} // namespace nanommo
99
100#endif // NANOMMO_SERVER_REDIS_H_
absl::StatusOr< std::string > Command(const std::vector< std::string > &words)
Definition redis.cc:186
Redis(const Redis &)=delete
absl::StatusOr< std::vector< std::vector< std::string > > > Pipeline(const std::vector< std::vector< std::string > > &commands)
Definition redis.cc:275
Redis & operator=(const Redis &)=delete
absl::StatusOr< std::vector< std::string > > TransactionIf(const std::map< std::string, std::string > &holding, const std::vector< std::vector< std::string > > &commands)
Definition redis.cc:361
absl::Status Transaction(const std::vector< std::vector< std::string > > &commands)
Definition redis.cc:313
static absl::StatusOr< std::unique_ptr< Redis > > Create(const std::string &host, int port=6379, RedisTimeouts timeouts={})
Connects to Redis, or returns why it couldn't.
Definition redis.cc:121
absl::StatusOr< std::vector< Entry > > Read(const std::vector< std::string > &words)
Definition redis.cc:217
Definition chat.cc:9
std::chrono::milliseconds fail_fast
Definition redis.h:25
std::chrono::milliseconds answer
For connecting, and then for each reply.
Definition redis.h:21
std::vector< std::string > fields
Definition redis.h:70
std::string id
Definition redis.h:69