[Pkg-javascript-commits] [ltx] 82/469: router: shape streams

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:01:05 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository ltx.

commit 5d5ea71cf1003639093a58a27848cddfdcd45df0
Author: Astro <astro at spaceboyz.net>
Date:   Mon Sep 6 00:26:28 2010 +0200

    router: shape streams
---
 lib/stream_shaper.js | 15 +++++++++++++++
 lib/xmpp/router.js   | 13 ++++++++++---
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/lib/stream_shaper.js b/lib/stream_shaper.js
new file mode 100644
index 0000000..cbb0df4
--- /dev/null
+++ b/lib/stream_shaper.js
@@ -0,0 +1,15 @@
+exports.attach = function(stream, rateLimit) {
+    var timer;
+    stream.rateLimit = rateLimit;  // makes it readjustable after attachment
+    stream.addListener('data', function(data) {
+			   if (timer !== undefined)
+			       clearTimeout(timer);
+
+			   stream.pause();
+			   var sleep = Math.floor(data.length * 1000 / stream.rateLimit);
+			   timer = setTimeout(function() {
+			       timer = undefined;
+			       stream.resume();
+			   }, sleep);
+		       });
+};
diff --git a/lib/xmpp/router.js b/lib/xmpp/router.js
index c6c0860..4def02c 100644
--- a/lib/xmpp/router.js
+++ b/lib/xmpp/router.js
@@ -1,6 +1,7 @@
 var net = require('net');
 var Server = require('./server');
 var JID = require('./jid');
+var StreamShaper = require('./../stream_shaper');
 
 
 dbgStream = function(tag, stream) {
@@ -23,7 +24,8 @@ dbgStream = function(tag, stream) {
 /**
  * Represents a domain we host with connections to federated servers
  */
-function DomainContext(domain) {
+function DomainContext(router, domain) {
+    this.router = router;
     this.domain = domain;
     this.s2sIn = {};
     this.s2sOut = {};
@@ -85,6 +87,8 @@ DomainContext.prototype.getOutStream = function(domain) {
 	var outStream = Server.makeOutgoingServer(domain);
 	this.s2sOut[domain] = outStream;
 	dbgStream('outgoing', outStream);
+
+	StreamShaper.attach(outStream, this.router.rateLimit);
 	this.setupStream(domain, outStream);
 
 	outStream.addListener('close', function() {
@@ -210,10 +214,10 @@ DomainContext.prototype.receive = function(stanza) {
 
 /**
  * TODO:
- * * karma
  * * nameprep
  * * timeouts
  * * keepAlive
+ * * stanza size limit
  * * TLS
  */
 function Router(s2sPort) {
@@ -226,10 +230,13 @@ function Router(s2sPort) {
 }
 exports.Router = Router;
 
+Router.prototype.rateLimit = 1E6;  // 100 KB/s, it's S2S after all
+
 Router.prototype.acceptConnection = function(inStream) {
     var self = this;
 
     Server.makeIncomingServer(inStream);
+    StreamShaper.attach(inStream, this.rateLimit);
     console.log('INCOMING from ' + inStream.remoteAddress);
     dbgStream('incoming', inStream);
 
@@ -309,7 +316,7 @@ Router.prototype.getContext = function(domain) {
     if (this.ctxs.hasOwnProperty(domain))
 	return this.ctxs[domain];
     else
-	return (this.ctxs[domain] = new DomainContext(domain));
+	return (this.ctxs[domain] = new DomainContext(this, domain));
 };
 
 Router.prototype.verifyDialback = function(from, to, id, key) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git



More information about the Pkg-javascript-commits mailing list