This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upload handler, peer: add component to handling upload slots (close #25)
- Loading branch information
1 parent
f7ae703
commit b63f7b9
Showing
3 changed files
with
69 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2014 Flávio Ribeiro <flavio@bem.tv>. | ||
// All rights reserved. | ||
// Use of this source code is governed by a Apache | ||
// license that can be found in the LICENSE file. | ||
|
||
var Settings = require("./settings") | ||
var _ = require("underscore") | ||
|
||
class UploadHandler { | ||
constructor() { | ||
this.maxUploadSlots = Settings.maxUploadSlots | ||
this.slots = {} | ||
} | ||
|
||
getSlot(peerId) { | ||
this.checkAndFreeSlots() | ||
if (_.contains(this.slots.keys, peerId) || (_.size(this.slots) < this.maxUploadSlots)) { | ||
this.slots[peerId] = Date.now() | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
checkAndFreeSlots() { | ||
var now = Date.now() - Settings.uploadSlotTimeout | ||
_.each(this.slots, function (timestamp, peerId) { | ||
if (timestamp <= now) { | ||
delete this.slots[peerId] | ||
} | ||
}, this) | ||
} | ||
} | ||
|
||
module.exports = UploadHandler |