Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add limit incoming

  • Loading branch information...
commit ee415ec98d53b162c376a748836fae4d38961955 1 parent 0410a9c
Paul Querna authored
1  limiting_concurrency/server_limit_client.js
@@ -57,6 +57,7 @@ http.createServer(function(req, res) {
57 57 done();
58 58 });
59 59 });
  60 +
60 61 function finishreq() {
61 62 bufs.forEach(function(buf){
62 63 preq.write(buf);
54 limiting_concurrency/server_limit_incoming.js
... ... @@ -0,0 +1,54 @@
  1 +#!/usr/bin/env node
  2 +/* This code is PUBLIC DOMAIN, and is distributed on an "AS IS" BASIS,
  3 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND. See the accompanying
  4 + * LICENSE file.
  5 + */
  6 +
  7 +var http = require('http');
  8 +var sys = require('sys');
  9 +var destination = "nodejs.org";
  10 +var port = 8080;
  11 +var maxClients = 2;
  12 +var currentClients = 0;
  13 +var active = true;
  14 +
  15 +var hs = null;
  16 +
  17 +function activate() {
  18 + if (!active && currentClients < maxClients) {
  19 + hs.watcher.start();
  20 + active = true;
  21 + }
  22 +}
  23 +
  24 +hs = http.createServer(function(req, res) {
  25 + var proxy = http.createClient(80, destination);
  26 + var preq = proxy.request(req.method, req.url, req.headers);
  27 +
  28 + console.log(req.connection.remoteAddress +" "+ req.method +" "+req.url);
  29 + preq.on('response', function(pres) {
  30 + res.writeHead(pres.statusCode, pres.headers);
  31 + sys.pump(pres, res);
  32 + pres.on('end', function() {
  33 + preq.end();
  34 + res.end();
  35 + currentClients--;
  36 + activate();
  37 + });
  38 + });
  39 + req.on('data', function(chunk) {
  40 + preq.write(chunk, 'binary');
  41 + });
  42 + req.on('end', function() {
  43 + preq.end();
  44 + });
  45 +
  46 + currentClients++;
  47 + if (currentClients >= maxClients) {
  48 + hs.watcher.stop();
  49 + active = false;
  50 + }
  51 +});
  52 +
  53 +hs.listen(port);
  54 +

0 comments on commit ee415ec

Please sign in to comment.
Something went wrong with that request. Please try again.