Load balancer now uses HTTP downloader

This commit is contained in:
Thulinma 2017-11-05 15:47:51 +01:00
parent 01ffc42f9f
commit be450a183f

View file

@ -4,7 +4,7 @@
#include <iostream> #include <iostream>
#include <mist/config.h> #include <mist/config.h>
#include <mist/defines.h> #include <mist/defines.h>
#include <mist/http_parser.h> #include <mist/downloader.h>
#include <mist/timing.h> #include <mist/timing.h>
#include <mist/tinythread.h> #include <mist/tinythread.h>
#include <set> #include <set>
@ -30,11 +30,11 @@ unsigned long hostsCounter = 0; // This is a pointer to guarantee atomic accesse
#define STATE_OFF 0 #define STATE_OFF 0
#define STATE_BOOT 1 #define STATE_BOOT 1
#define STATE_ONLINE 2 #define STATE_ERROR 2
#define STATE_GODOWN 3 #define STATE_ONLINE 3
#define STATE_REQCLEAN 4 #define STATE_GODOWN 4
const char *stateLookup[] ={"Offline", "Starting monitoring", "Monitored", "Requesting stop", #define STATE_REQCLEAN 5
"Requesting clean"}; const char *stateLookup[] ={"Offline", "Starting monitoring", "Monitored (error)", "Monitored (online)", "Requesting stop", "Requesting clean"};
struct streamDetails{ struct streamDetails{
uint32_t total; uint32_t total;
@ -536,81 +536,52 @@ int handleRequest(Socket::Connection &conn){
void handleServer(void *hostEntryPointer){ void handleServer(void *hostEntryPointer){
hostEntry *entry = (hostEntry *)hostEntryPointer; hostEntry *entry = (hostEntry *)hostEntryPointer;
std::string name = entry->name;
HTTP::Parser H;
std::string host;
int port = 4242;
JSON::Value bandwidth = 128 * 1024 * 1024ll; // assume 1G connection JSON::Value bandwidth = 128 * 1024 * 1024ll; // assume 1G connection
HTTP::URL url(entry->name);
size_t slash = name.find('/'); if (!url.protocol.size()){url.protocol = "http";}
if (slash != std::string::npos){ if (!url.port.size()){url.port = "4242";}
bandwidth = name.substr(slash + 1, std::string::npos); if (url.path.size()){
bandwidth = url.path;
bandwidth = bandwidth.asInt() * 1024 * 1024; bandwidth = bandwidth.asInt() * 1024 * 1024;
name = name.substr(0, slash); url.path.clear();
} }
url.path = passphrase + ".json";
size_t colon = name.find(':'); INFO_MSG("Monitoring %s", url.getUrl().c_str());
if (colon != std::string::npos && colon != 0 && colon != name.size()){
host = name.substr(0, colon);
port = atoi(name.substr(colon + 1, std::string::npos).c_str());
}else{
host = name;
}
INFO_MSG("Monitoring %s on port %d using passphrase %s", host.c_str(), port, passphrase.c_str());
entry->details->availBandwidth = bandwidth.asInt(); entry->details->availBandwidth = bandwidth.asInt();
entry->details->host = host; entry->details->host = url.host;
entry->state = STATE_ONLINE; entry->state = STATE_BOOT;
bool down = true; bool down = true;
Socket::Connection servConn(host, port, false); HTTP::Downloader DL;
while (cfg->is_active && (entry->state == STATE_ONLINE)){ while (cfg->is_active && (entry->state != STATE_GODOWN)){
if (!servConn){ if (DL.get(url) && DL.isOk()){
HIGH_MSG("Reconnecting to %s", host.c_str()); JSON::Value servData = JSON::fromString(DL.data());
servConn = Socket::Connection(host, port, false); if (!servData){
} FAIL_MSG("Can't decode server %s load information", url.host.c_str());
if (!servConn){ entry->details->badNess();
MEDIUM_MSG("Cannot reach server %s", host.c_str()); DL.getSocket().close();
entry->details->badNess(); down = true;
Util::wait(5000); entry->state = STATE_ERROR;
down = true; }else{
continue; if (down){
} WARN_MSG("Connection established with %s", url.host.c_str());
entry->state = STATE_ONLINE;
// retrieve update information down = false;
H.url = "/" + passphrase + ".json"; }
H.method = "GET"; entry->details->update(servData);
H.SendRequest(servConn);
H.Clean();
unsigned int startTime = Util::epoch();
while (cfg->is_active && servConn &&
!((servConn.spool() || servConn.Received().size()) && H.Read(servConn))){
if (Util::epoch() - startTime > 25){
FAIL_MSG("Server %s timed out", host.c_str());
servConn.close();
H.Clean();
} }
Util::sleep(250);
}
JSON::Value servData = JSON::fromString(H.body);
if (!servData){
FAIL_MSG("Can't retrieve server %s load information", host.c_str());
std::cerr << H.body << std::endl;
down = true;
entry->details->badNess();
servConn.close();
}else{ }else{
if (down){ FAIL_MSG("Can't retrieve server %s load information", url.host.c_str());
WARN_MSG("Connection established with %s", host.c_str()); entry->details->badNess();
down = false; DL.getSocket().close();
} down = true;
entry->details->update(servData); entry->state = STATE_ERROR;
} }
H.Clean();
Util::wait(5000); Util::wait(5000);
} }
servConn.close(); WARN_MSG("Monitoring thread for %s stopping", url.host.c_str());
DL.getSocket().close();
entry->state = STATE_REQCLEAN; entry->state = STATE_REQCLEAN;
} }