Skip to content

Conversation

@yciabaud
Copy link
Contributor

@yciabaud yciabaud commented Jun 8, 2016

As discussed in #4, we have to limit the peers stored in the tracker server. This PR uses a configurable lru cache with 2 rules:

  • Length: To limit the memory usage of the peer list, the oldest peer is evicted when a new peer makes the cache reach its limit.
  • Date: To avoid giving non active peers on announce, we check that the peers selected have been seen recently and they are evicted if they are older than a threshold.

Notice that peers last used date are refreshed on each request to the server.

while ((peerId = ite()) && peers.length < numwant) {
var peer = this.peers[peerId]
// Don't mark the peer as most recently used on announce
var peer = this.peers.peek(peerId)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a problem here, on one hand, if I use LRU.get, the peer will be marked as used and the max condition can evict a more recent peer.
On the other hand, if I use LRU.peek, we never access old peers and the maxAge eviction will not happen.

I think I will clean old peers manually as they are picked in announce...

@yciabaud yciabaud changed the title [WIP] Limit peers in tracker server with LRU based cache Limit peers in tracker server with LRU based cache Jun 9, 2016
while ((peerId = ite()) && peers.length < numwant) {
var peer = this.peers[peerId]
// Check manually if the peer is active
if (peers.maxAge && (Date.now() - peers.cache[peerId].modified) > peers.maxAge) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be this.peers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this deserves a PR to LRU too, I am trying to access an element without bringing it up in the chain but with evicting it if its too old.

@DiegoRBaquero
Copy link
Member

@yciabaud Aside from the comments I left LGTM. We need to evaluate the case I stated here: #4 (comment)

It's a problem in the long run. Each swarm has an LRU cache which in some case might never remove peers, consuming RAM. OpenWebTorrent trackers, who's been running without restarting for the longest shows us this, it's full of peers who left ungracefully: https://tracker.openwebtorrent.com/stats

The default limit of 10K peers for a single torrent is huge IMHO.

On another topic, what do you guys think about adding the version number in the stats page & json? @feross @jakefb

@yciabaud
Copy link
Contributor Author

yciabaud commented Jun 9, 2016

Well, the case you're pointing out is important but lets stay at the swarm level for this PR and then try to work on removing old torrents with its swarm in another one.

Yeah, 10K is huge but it was infinite before. What would you set? 1K?

And to answer your last question, I do agree that some more infos in the stats would not hurt, I am thinking of name, version, uptime...

@DiegoRBaquero
Copy link
Member

DiegoRBaquero commented Jun 10, 2016

1K peers per torrent should do. This PR should update LRU dep to 3.0.0 and use the new .keys method and peek maxAge check.

@yciabaud
Copy link
Contributor Author

Sure will update this today. If peek checks maxAge, calling stats will prune old peers on unused torrents on my peer stat PR :-D

@DiegoRBaquero
Copy link
Member

Oh, yes! I hadn't thought about it, we will peek them now on stats and that prunes them. All problems solved now! (Well at least in small scale (1k dead peers on a tracker) don't accumulate in months as far as I can see from owt's tracker) I remember when 400 peers would kill my server's memory with the memleaks there were. Now 900k peers go for about 380MB of Ram.

Diego R.

On Jun 10, 2016, at 1:10 AM, Yoann Ciabaud [email protected] wrote:

Sure will update this today. If peek checks maxAge, calling stats will prune old peers on unused torrents on my peer stat PR :-D


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

@yciabaud
Copy link
Contributor Author

We should not rely on this but this is a quick way to clean the memory. ;)

this.peers = {}
this.peers = new LRU({
max: server.peersCacheLength || 1000,
maxAge: server.peersCacheTtl || 900 // 900s = 15 minutes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in milliseconds, according to the docs for lru

@DiegoRBaquero
Copy link
Member

LGTM now. @feross ?

@yciabaud
Copy link
Contributor Author

@feross, I would rather have a last feedback from you since it is core related before merging this.
Do you want to squash the commits into one?

@feross
Copy link
Member

feross commented Aug 5, 2016

Finally got a chance to look at this. LGTM!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants