Browse Source

Garbage collection on watches.

cl-refactor
Gav Wood 10 years ago
parent
commit
7fc1660bfb
  1. 9
      libdevcore/CommonData.h
  2. 21
      libethereum/Client.cpp
  3. 3
      libethereum/Client.h
  4. 8
      libjsqrc/ethereumjs/dist/ethereum.js
  5. 4
      libjsqrc/ethereumjs/dist/ethereum.js.map
  6. 2
      libjsqrc/ethereumjs/dist/ethereum.min.js
  7. 8
      libjsqrc/ethereumjs/lib/providermanager.js

9
libdevcore/CommonData.h

@ -246,4 +246,13 @@ inline std::set<_T> operator+(std::set<_T> const& _a, std::set<_T> const& _b)
/// Make normal string from fixed-length string.
std::string toString(string32 const& _s);
template<class T, class U>
std::vector<T> keysOf(std::map<T, U> const& _m)
{
std::vector<T> ret;
for (auto const& i: _m)
ret.push_back(i.first);
return ret;
}
}

21
libethereum/Client.cpp

@ -223,7 +223,10 @@ void Client::uninstallWatch(unsigned _i)
auto fit = m_filters.find(id);
if (fit != m_filters.end())
if (!--fit->second.refCount)
{
cwatch << "*X*" << fit->first << ":" << fit->second.filter;
m_filters.erase(fit);
}
}
void Client::noteChanged(h256Set const& _filters)
@ -250,8 +253,11 @@ LocalisedLogEntries Client::peekWatch(unsigned _watchId) const
Guard l(m_filterLock);
try {
return m_watches.at(_watchId).changes;
auto& w = m_watches.at(_watchId);
w.lastPoll = chrono::system_clock::now();
return w.changes;
} catch (...) {}
return LocalisedLogEntries();
}
@ -261,7 +267,9 @@ LocalisedLogEntries Client::checkWatch(unsigned _watchId)
LocalisedLogEntries ret;
try {
std::swap(ret, m_watches.at(_watchId).changes);
auto& w = m_watches.at(_watchId);
std::swap(ret, w.changes);
w.lastPoll = chrono::system_clock::now();
} catch (...) {}
return ret;
@ -556,6 +564,15 @@ void Client::doWork()
cworkout << "WORK";
this_thread::sleep_for(chrono::milliseconds(100));
if (chrono::system_clock::now() - m_lastGarbageCollection > chrono::seconds(5))
{
// garbage collect on watches
Guard l(m_filterLock);
for (auto key: keysOf(m_watches))
if (chrono::system_clock::now() - m_watches[key].lastPoll > chrono::seconds(5))
uninstallWatch(key);
m_lastGarbageCollection = chrono::system_clock::now();
}
}
unsigned Client::numberOf(int _n) const

3
libethereum/Client.h

@ -94,6 +94,7 @@ struct ClientWatch
h256 id;
LocalisedLogEntries changes = LocalisedLogEntries{ InitialChange };
mutable std::chrono::system_clock::time_point lastPoll;
};
struct WatchChannel: public LogChannel { static const char* name() { return "(o)"; } static const int verbosity = 7; };
@ -328,6 +329,8 @@ private:
mutable std::mutex m_filterLock;
std::map<h256, InstalledFilter> m_filters;
std::map<unsigned, ClientWatch> m_watches;
mutable std::chrono::system_clock::time_point m_lastGarbageCollection;
};
}

8
libjsqrc/ethereumjs/dist/ethereum.js

@ -1055,7 +1055,11 @@ var ProviderManager = function() {
var result = results[index];
if (!jsonrpc.isValidResponse(result)) {
console.log(result);
console.log("INVALID RESPONSE");
console.log("results:" + JSON.stringify(results));
console.log("data:" + JSON.stringify(data));
console.log("index:" + index);
console.log("result:" + JSON.stringify(result));
return;
}
@ -1067,7 +1071,7 @@ var ProviderManager = function() {
data.callback(result);
});
});il
}
setTimeout(poll, 1000);

4
libjsqrc/ethereumjs/dist/ethereum.js.map

File diff suppressed because one or more lines are too long

2
libjsqrc/ethereumjs/dist/ethereum.min.js

File diff suppressed because one or more lines are too long

8
libjsqrc/ethereumjs/lib/providermanager.js

@ -54,7 +54,11 @@ var ProviderManager = function() {
var result = results[index];
if (!jsonrpc.isValidResponse(result)) {
console.log(result);
console.log("INVALID RESPONSE");
console.log("results:" + JSON.stringify(results));
console.log("data:" + JSON.stringify(data));
console.log("index:" + index);
console.log("result:" + JSON.stringify(result));
return;
}
@ -66,7 +70,7 @@ var ProviderManager = function() {
data.callback(result);
});
});il
}
setTimeout(poll, 1000);

Loading…
Cancel
Save