nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
database.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_DATABASE_H_
2#define NANOMMO_SERVER_DATABASE_H_
3
4#include <mysql.h>
5
6#include <chrono>
7#include <cstddef>
8#include <cstdint>
9#include <functional>
10#include <map>
11#include <memory>
12#include <string>
13#include <string_view>
14#include <utility>
15#include <vector>
16
17#include "absl/status/status.h"
18#include "absl/status/statusor.h"
19
20namespace nanommo {
21
22/// Closes a statement that's been prepared, as a unique_ptr goes.
24 void operator()(MYSQL_STMT* statement) const { mysql_stmt_close(statement); }
25};
26using Statement = std::unique_ptr<MYSQL_STMT, CloseStatement>;
27
28struct Result {
29 std::vector<std::vector<std::string>> rows;
30 uint64_t insert_id = 0;
31 /// How many rows an INSERT, UPDATE or DELETE changed.
32 uint64_t affected_rows = 0;
33};
34
35/// How long MySQL has to answer, and what happens when it doesn't. A world's
36/// tick waits for the reads that load a player who joins, so a MySQL that
37/// hangs would hang the world.
39 /// For connecting.
40 std::chrono::seconds connect{1};
41 /// For each read and each write on the connection, longer than the two
42 /// seconds a statement waits for a lock, which fails by itself. MySQL's
43 /// client counts them in seconds.
44 std::chrono::seconds answer{3};
45 /// After MySQL didn't answer in time, or couldn't be reached, each call
46 /// fails at once for this long, without asking it.
47 std::chrono::milliseconds fail_fast{5000};
48};
49
50class Database {
51 public:
52 /// Connects to MySQL, at the host, which can name a port as host:port, or
53 /// returns why it couldn't.
54 static absl::StatusOr<std::unique_ptr<Database>> Create(
55 const std::string& host, const std::string& user,
56 const std::string& password, const std::string& name,
57 MysqlTimeouts timeouts = {});
58 ~Database();
59
60 Database(const Database&) = delete;
61 Database& operator=(const Database&) = delete;
62
63 /// The most reads a connection keeps prepared.
64 static constexpr std::size_t kMostReads = 64;
65
66 /// Values go apart from the SQL, one for each ?, so they stay values. A
67 /// duplicate key comes back as an AlreadyExists error, a lost connection as
68 /// Unavailable, and a lock waited for too long, or a deadlock, as Aborted.
69 /// A SELECT is prepared the first time it runs, and kept, so that it runs
70 /// in one round trip after that, not two. A statement MySQL doesn't answer
71 /// in time fails as Unavailable, and so does each after it, at once, until
72 /// the timeouts' fail_fast is over.
73 absl::StatusOr<Result> Run(std::string_view sql,
74 const std::vector<std::string>& values = {});
75
76 /// Runs the statements as one transaction, so that either all of them
77 /// happen, or none does: none if one of them fails, or if the connection
78 /// goes. A lost connection isn't tried again inside a transaction, as what
79 /// ran before it on that connection is gone.
80 absl::Status Transaction(const std::function<absl::Status()>& statements);
81
82 private:
83 Database(std::string host, std::string user, std::string password,
84 std::string name, MysqlTimeouts timeouts)
85 : host_(std::move(host)),
86 user_(std::move(user)),
87 password_(std::move(password)),
88 name_(std::move(name)),
89 timeouts_(timeouts) {}
90
91 absl::Status Connect();
92 absl::StatusOr<Result> RunOnce(std::string_view sql,
93 const std::vector<std::string>& values,
94 std::chrono::steady_clock::time_point start);
95 /// Whether to ask MySQL at all: not while failing fast.
96 absl::Status Ready() const;
97 /// Whether a call that began then and failed as Unavailable waited out its
98 /// timeout, rather than finding the connection gone.
99 bool TimedOut(std::chrono::steady_clock::time_point start) const;
100 /// Fails each call at once for the timeouts' fail_fast, after a call that
101 /// failed so.
102 void Notice(const absl::Status& status,
103 std::chrono::steady_clock::time_point start);
104 absl::StatusOr<Result> Execute(MYSQL_STMT* statement,
105 const std::vector<std::string>& values);
106
107 std::string host_;
108 std::string user_;
109 std::string password_;
110 std::string name_;
111 MysqlTimeouts timeouts_;
112 std::chrono::steady_clock::time_point failing_until_;
113 MYSQL* mysql_ = nullptr;
114 bool in_transaction_ = false;
115 /// The reads prepared on this connection, by their SQL.
116 std::map<std::string, Statement, std::less<>> reads_;
117};
118
119} // namespace nanommo
120
121#endif // NANOMMO_SERVER_DATABASE_H_
Database(const Database &)=delete
Database & operator=(const Database &)=delete
static absl::StatusOr< std::unique_ptr< Database > > Create(const std::string &host, const std::string &user, const std::string &password, const std::string &name, MysqlTimeouts timeouts={})
Definition database.cc:106
absl::Status Transaction(const std::function< absl::Status()> &statements)
Definition database.cc:299
static constexpr std::size_t kMostReads
The most reads a connection keeps prepared.
Definition database.h:64
absl::StatusOr< Result > Run(std::string_view sql, const std::vector< std::string > &values={})
Definition database.cc:186
Definition chat.cc:9
std::unique_ptr< MYSQL_STMT, CloseStatement > Statement
Definition database.h:26
Closes a statement that's been prepared, as a unique_ptr goes.
Definition database.h:23
void operator()(MYSQL_STMT *statement) const
Definition database.h:24
std::chrono::seconds answer
Definition database.h:44
std::chrono::milliseconds fail_fast
Definition database.h:47
std::chrono::seconds connect
For connecting.
Definition database.h:40
std::vector< std::vector< std::string > > rows
Definition database.h:29
uint64_t insert_id
Definition database.h:30
uint64_t affected_rows
How many rows an INSERT, UPDATE or DELETE changed.
Definition database.h:32