seqeron 0.18.0
Loading...
Searching...
No Matches
Env.hpp
Go to the documentation of this file.
1#pragma once
2
3// Process-startup configuration read from the environment: the lookups every seqeron binary's main()
4// does before it can connect to anything.
5
6#include <cstdint>
7#include <cstdlib>
8#include <filesystem>
9#include <string>
10#include <system_error>
11
13
20inline std::int32_t envInt(const char* name, const std::int32_t fallback)
21{
22 const char* value = std::getenv(name);
23 return (value != nullptr && *value != '\0') ? static_cast<std::int32_t>(std::strtol(value, nullptr, 10)) : fallback;
24}
25
32inline std::string envString(const char* name, const std::string& fallback)
33{
34 const char* value = std::getenv(name);
35 return (value != nullptr && *value != '\0') ? std::string{ value } : fallback;
36}
37
43inline bool envFlag(const char* name)
44{
45 const char* value = std::getenv(name);
46 return value != nullptr && *value != '\0';
47}
48
56inline std::string joinPath(const std::string& dir, const std::string& name)
57{
58 if (dir.empty())
59 {
60 return name;
61 }
62 return dir.back() == '/' ? dir + name : dir + '/' + name;
63}
64
73inline std::string resolveAeronDir(const char* envName, const std::int32_t memberId)
74{
75 const char* value = std::getenv(envName);
76 if (value != nullptr && *value != '\0')
77 {
78 return value;
79 }
80 const std::string name = "seqeron-seq-aeron-" + std::to_string(memberId);
81 std::error_code error;
82 if (std::filesystem::is_directory("/dev/shm", error))
83 {
84 return joinPath("/dev/shm", name);
85 }
86 const char* tmpDir = std::getenv("TMPDIR");
87 return joinPath(tmpDir != nullptr && *tmpDir != '\0' ? tmpDir : "/tmp", name);
88}
89
97inline std::string resolveEgressEndpoint(const char* envName, const std::uint16_t defaultPort)
98{
99 return envString(envName, "localhost:" + std::to_string(defaultPort));
100}
101
102// Per-message logging blocks the poll thread on stdout under load, so it is opt-in via SEQERON_VERBOSE_LOG.
103// Cached: checked once per message.
105{
106 static const bool enabled = envFlag("SEQERON_VERBOSE_LOG");
107 return enabled;
108}
109
110} // namespace org::limitless::seqeron::util
Definition Env.hpp:12
bool verboseLoggingEnabled()
Definition Env.hpp:104
std::string resolveEgressEndpoint(const char *envName, const std::uint16_t defaultPort)
Resolves a "host:port" cluster-egress endpoint.
Definition Env.hpp:97
std::string resolveAeronDir(const char *envName, const std::int32_t memberId)
Resolves the co-located cluster member's Aeron directory, which an app shares to reach that node's qu...
Definition Env.hpp:73
std::string joinPath(const std::string &dir, const std::string &name)
Joins a directory and a name with exactly one separator.
Definition Env.hpp:56
std::int32_t envInt(const char *name, const std::int32_t fallback)
Reads a base-10 int from the environment.
Definition Env.hpp:20
bool envFlag(const char *name)
Whether a variable is set and non-empty — the convention every SEQERON_* opt-in switch uses.
Definition Env.hpp:43
std::string envString(const char *name, const std::string &fallback)
Reads a string from the environment.
Definition Env.hpp:32