Sessions rework

This commit is contained in:
Ramkoemar 2021-10-18 14:29:13 +02:00 committed by Thulinma
parent 3e85da2afd
commit 074e757028
27 changed files with 1222 additions and 1183 deletions

View file

@ -306,7 +306,6 @@ int main_loop(int argc, char **argv){
if (Controller::Storage["config"].isMember("accesslog")){
Controller::conf.getOption("accesslog", true)[0u] = Controller::Storage["config"]["accesslog"];
}
Controller::maxConnsPerIP = Controller::conf.getInteger("maxconnsperip");
Controller::Storage["config"]["prometheus"] = Controller::conf.getString("prometheus");
Controller::Storage["config"]["accesslog"] = Controller::conf.getString("accesslog");
Controller::normalizeTrustedProxies(Controller::Storage["config"]["trustedproxy"]);

View file

@ -594,6 +594,7 @@ void Controller::handleAPICommands(JSON::Value &Request, JSON::Value &Response){
out["prometheus"] = in["prometheus"];
Controller::prometheus = out["prometheus"].asStringRef();
}
if (in.isMember("sessionMode")){out["sessionMode"] = in["sessionMode"];}
if (in.isMember("defaultStream")){out["defaultStream"] = in["defaultStream"];}
if (in.isMember("location") && in["location"].isObject()){
out["location"]["lat"] = in["location"]["lat"].asDouble();

File diff suppressed because it is too large Load diff

View file

@ -18,8 +18,7 @@
namespace Controller{
extern bool killOnExit;
extern unsigned int maxConnsPerIP;
/// This function is ran whenever a stream becomes active.
void streamStarted(std::string stream);
/// This function is ran whenever a stream becomes inactive.
@ -35,34 +34,14 @@ namespace Controller{
uint64_t pktCount;
uint64_t pktLost;
uint64_t pktRetransmit;
std::string connectors;
};
enum sessType{SESS_UNSET = 0, SESS_INPUT, SESS_OUTPUT, SESS_VIEWER};
/// This is a comparison and storage class that keeps sessions apart from each other.
/// Whenever two of these objects are not equal, it will create a new session.
class sessIndex{
public:
sessIndex();
sessIndex(const Comms::Statistics &statComm, size_t id);
std::string ID;
std::string host;
unsigned int crc;
std::string streamName;
std::string connector;
bool operator==(const sessIndex &o) const;
bool operator!=(const sessIndex &o) const;
bool operator>(const sessIndex &o) const;
bool operator<=(const sessIndex &o) const;
bool operator<(const sessIndex &o) const;
bool operator>=(const sessIndex &o) const;
std::string toStr();
};
class statStorage{
public:
void update(Comms::Statistics &statComm, size_t index);
void update(Comms::Sessions &statComm, size_t index);
bool hasDataFor(unsigned long long);
statLog &getDataFor(unsigned long long);
std::map<unsigned long long, statLog> log;
@ -75,36 +54,33 @@ namespace Controller{
uint64_t firstActive;
uint64_t firstSec;
uint64_t lastSec;
uint64_t wipedUp;
uint64_t wipedDown;
uint64_t wipedPktCount;
uint64_t wipedPktLost;
uint64_t wipedPktRetransmit;
std::deque<statStorage> oldConns;
sessType sessionType;
bool tracked;
uint8_t noBWCount; ///< Set to 2 when not to count for external bandwidth
std::string streamName;
std::string host;
std::string curConnector;
std::string sessId;
public:
statSession();
uint32_t invalidate();
uint32_t kill();
char sync;
std::map<uint64_t, statStorage> curConns;
~statSession();
statStorage curData;
std::set<std::string> tags;
sessType getSessType();
void wipeOld(uint64_t);
void finish(uint64_t index);
void switchOverTo(statSession &newSess, uint64_t index);
void update(uint64_t index, Comms::Statistics &data);
void dropSession(const sessIndex &index);
void update(uint64_t index, Comms::Sessions &data);
uint64_t getStart();
uint64_t getEnd();
bool isViewerOn(uint64_t time);
bool isConnected();
bool isTracked();
bool hasDataFor(uint64_t time);
bool hasData();
std::string getStreamName();
std::string getHost();
std::string getSessId();
std::string getCurrentProtocols();
uint64_t newestDataPoint();
uint64_t getConnTime(uint64_t time);
uint64_t getConnTime();
uint64_t getLastSecond(uint64_t time);
uint64_t getDown(uint64_t time);
uint64_t getUp();
@ -122,8 +98,6 @@ namespace Controller{
uint64_t getBpsUp(uint64_t start, uint64_t end);
};
extern std::map<sessIndex, statSession> sessions;
extern std::map<unsigned long, sessIndex> connToSession;
extern tthread::mutex statsMutex;
extern uint64_t statDropoff;
@ -155,6 +129,7 @@ namespace Controller{
void sessions_shutdown(const std::string &streamname, const std::string &protocol = "");
bool hasViewers(std::string streamName);
void writeSessionCache(); /*LTS*/
void killConnections(std::string sessId);
#define PROMETHEUS_TEXT 0
#define PROMETHEUS_JSON 1

View file

@ -91,19 +91,6 @@ namespace Controller{
rlxAccs->setString("tags", tags, newEndPos);
rlxAccs->setEndPos(newEndPos + 1);
}
if (Triggers::shouldTrigger("USER_END", strm)){
std::stringstream plgen;
plgen << sessId << "\n"
<< strm << "\n"
<< conn << "\n"
<< host << "\n"
<< duration << "\n"
<< up << "\n"
<< down << "\n"
<< tags;
std::string payload = plgen.str();
Triggers::doTrigger("USER_END", payload, strm);
}
}
void normalizeTrustedProxies(JSON::Value &tp){
@ -450,7 +437,8 @@ namespace Controller{
systemBoot = globAccX.getInt("systemBoot");
}
if(!globAccX.getFieldAccX("defaultStream")
|| !globAccX.getFieldAccX("systemBoot")){
|| !globAccX.getFieldAccX("systemBoot")
|| !globAccX.getFieldAccX("sessionMode")){
globAccX.setReload();
globCfg.master = true;
globCfg.close();
@ -461,12 +449,16 @@ namespace Controller{
if (!globAccX.isReady()){
globAccX.addField("defaultStream", RAX_128STRING);
globAccX.addField("systemBoot", RAX_64UINT);
globAccX.addField("sessionMode", RAX_64UINT);
if (!Storage["config"]["sessionMode"]){
Storage["config"]["sessionMode"] = SESS_BUNDLE_STREAMNAME_HOSTNAME_SESSIONID;
}
globAccX.setRCount(1);
globAccX.setEndPos(1);
globAccX.setReady();
}
globAccX.setString("defaultStream", Storage["config"]["defaultStream"].asStringRef());
globAccX.setInt("systemBoot", systemBoot);
globAccX.setInt("sessionMode", Storage["config"]["sessionMode"].asInt());
globCfg.master = false; // leave the page after closing
}
}