Skip to content

Instantly share code, notes, and snippets.

@yudai09
Last active February 14, 2017 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yudai09/b9cef595df75ea8e28d3a1d3c463b707 to your computer and use it in GitHub Desktop.
Save yudai09/b9cef595df75ea8e28d3a1d3c463b707 to your computer and use it in GitHub Desktop.
diff --git a/modules/geobackend/geobackend.cc b/modules/geobackend/geobackend.cc
index 287db2f..9f9ef35 100644
--- a/modules/geobackend/geobackend.cc
+++ b/modules/geobackend/geobackend.cc
@@ -25,20 +25,13 @@ using boost::trim_right;
// Static members
-IPPrefTree * GeoBackend::ipt;
-vector<string> GeoBackend::nsRecords;
-map<string, GeoRecord*> GeoBackend::georecords;
-string GeoBackend::soaMasterServer;
-string GeoBackend::soaHostmaster;
-string GeoBackend::zoneName;
-uint32_t GeoBackend::geoTTL;
-uint32_t GeoBackend::nsTTL;
-time_t GeoBackend::lastDiscoverTime = 0;
const string GeoBackend::logprefix = "[geobackend] ";
-bool GeoBackend::first = true;
int GeoBackend::backendcount = 0;
pthread_mutex_t GeoBackend::startup_lock;
pthread_mutex_t GeoBackend::ipt_lock;
+IPPrefTree * GeoBackend::ipt;
+time_t GeoBackend::lastDiscoverTime = 0;
+bool GeoBackend::first = true;
// Class GeoRecord
@@ -48,37 +41,36 @@ GeoRecord::GeoRecord() : origin(".") {}
GeoBackend::GeoBackend(const string &suffix) : forceReload(false) {
setArgPrefix("geo" + suffix);
-
- // Make sure only one (the first) backend instance is initializing static things
- Lock lock(&startup_lock);
-
- backendcount++;
- if (!first)
- return;
- first = false;
-
- ipt = NULL;
+ if (first) {
+ ipt = NULL;
+ lastDiscoverTime = 0;
+ }
+ first = false;
loadZoneName();
loadTTLValues();
loadSOAValues();
loadNSRecords();
reload();
+
+ // Make sure only one (the first) backend instance is initializing static things
+ Lock lock(&startup_lock);
+ backendcount++;
+ // debug
+ L << Logger::Debug << logprefix << "backendcount: " << backendcount << endl;
}
GeoBackend::~GeoBackend() {
+ for (map<string, GeoRecord*>::iterator i = georecords.begin(); i != georecords.end(); ++i)
+ delete i->second;
+
+ if (ipt != NULL) {
+ delete ipt;
+ ipt = NULL;
+ }
Lock lock(&startup_lock);
backendcount--;
- if (backendcount == 0) {
- for (map<string, GeoRecord*>::iterator i = georecords.begin(); i != georecords.end(); ++i)
- delete i->second;
-
- if (ipt != NULL) {
- delete ipt;
- ipt = NULL;
- }
- }
}
bool GeoBackend::getSOA(const string &name, SOAData &soadata, DNSPacket *p) {
@@ -155,9 +147,9 @@ bool GeoBackend::get(DNSResourceRecord &r) {
}
void GeoBackend::reload() {
- forceReload = true;
+ // forceReload = true;
rediscover();
- forceReload = false;
+ // forceReload = false;
}
void GeoBackend::rediscover(string *status) {
diff --git a/modules/geobackend/geobackend.hh b/modules/geobackend/geobackend.hh
index 2be48f5..5f23373 100644
--- a/modules/geobackend/geobackend.hh
+++ b/modules/geobackend/geobackend.hh
@@ -43,17 +43,19 @@ public:
private:
// Static resources, shared by all instances
- static IPPrefTree *ipt;
- static vector<string> nsRecords;
- static map<string, GeoRecord*> georecords;
- static string soaMasterServer;
- static string soaHostmaster;
- static string zoneName;
- static uint32_t geoTTL;
- static uint32_t nsTTL;
- static time_t lastDiscoverTime;
const static string logprefix;
-
+
+ // Instance variables: to load multiple domains
+ static IPPrefTree *ipt;
+ vector<string> nsRecords;
+ map<string, GeoRecord*> georecords;
+ string soaMasterServer;
+ string soaHostmaster;
+ string zoneName;
+ uint32_t geoTTL;
+ uint32_t nsTTL;
+ static time_t lastDiscoverTime;
+
bool forceReload;
// Locking
@yudai09
Copy link
Author

yudai09 commented Feb 13, 2017

original patch has problem under multi thread environment.
the ip table should be loaded only once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment