Permalink
Browse files

http-timeout server

  • Loading branch information...
1 parent 33ad956 commit 50e1e5c65d00438bac9f06e40967dcc0fb0df2fd @tjanczuk committed Mar 9, 2012
Showing with 465 additions and 0 deletions.
  1. +15 −0 src/cert.pem
  2. +87 −0 src/http-timeout.js
  3. +15 −0 src/key.pem
  4. +30 −0 src/master.js
  5. +318 −0 src/worker.js
View
@@ -0,0 +1,15 @@
+-----BEGIN CERTIFICATE-----
+MIICRTCCAa4CCQCywR0r8/wSgjANBgkqhkiG9w0BAQUFADBnMQswCQYDVQQGEwJV
+UzELMAkGA1UECBMCV0ExEDAOBgNVBAcTB1JlZG1vbmQxITAfBgNVBAoTGEludGVy
+bmV0IFdpZGdpdHMgUHR5IEx0ZDEWMBQGA1UEAxQNKi5qYW5jenVrLm9yZzAeFw0x
+MjAyMDIxOTE5MjNaFw0xMjAzMDMxOTE5MjNaMGcxCzAJBgNVBAYTAlVTMQswCQYD
+VQQIEwJXQTEQMA4GA1UEBxMHUmVkbW9uZDEhMB8GA1UEChMYSW50ZXJuZXQgV2lk
+Z2l0cyBQdHkgTHRkMRYwFAYDVQQDFA0qLmphbmN6dWsub3JnMIGfMA0GCSqGSIb3
+DQEBAQUAA4GNADCBiQKBgQC6KNZ5b9Sb+n1zZ0ImhDEZST45m64tM+nLjDd6jfA1
+OBEdJo9hrYcqeEaiP3NneYGRUwWWtvcAuhXuajG0Df8RzGDJDUa7WlZIEZkXTUr9
+9Ykixj8F85sQHk5sVsTfjCEw9bbqdS/uUMzjFcGGf+9r7qe1E2xOiDAwd5ZNMyr4
+yQIDAQABMA0GCSqGSIb3DQEBBQUAA4GBAKfimB87gNf/Jzn7KZ8B+lG+IZbTQq8Q
+fCXZph1oUx2mmYjXQwAn8gtCuu5TbBXng9UMaeFBD9UGX50MyTZf+jzgwdKCRH66
+m9CHYZmAsLr3zfoYNNoyiLfOaUM6FA2YNjfsGkLQk/yj4STOtv6SihtB8YghC6b/
+fkPRx5Zpub7I
+-----END CERTIFICATE-----
View
@@ -0,0 +1,87 @@
+var cluster = require('cluster')
+ , fs = require('fs')
+
+var argv = require('optimist')
+ .usage('Usage: $0')
+ .options('m', {
+ alias: 'mongo',
+ description: 'Mongo DB connecton string',
+ default: 'mongodb://localhost/httpd'
+ })
+ .options('w', {
+ alias: 'workers',
+ description: 'Number of worker processes',
+ default: require('os').cpus().length
+ })
+ .options('p', {
+ alias: 'port',
+ description: 'HTTP listen port',
+ default: 80
+ })
+ .options('s', {
+ alias: 'sslport',
+ description: 'HTTPS listen port',
+ default: 443
+ })
+ .options('c', {
+ alias: 'cert',
+ description: 'Server certificate for SSL',
+ default: __dirname + '/cert.pem'
+ })
+ .options('k', {
+ alias: 'key',
+ description: 'Private key for SSL',
+ default: __dirname + '/key.pem'
+ })
+ .options('x', {
+ alias: 'proxy',
+ description: 'HTTP proxy in host:port format for outgoing requests',
+ default: ''
+ })
+ .options('l', {
+ alias: 'lockDuration',
+ description: 'Peek lock duration (milliseconds)',
+ default: 20000
+ })
+ .options('i', {
+ alias: 'peekInterval',
+ description: 'Cluster-wide peek interval (milliseconds)',
+ default: 5000
+ })
+ .options('a', {
+ alias: 'maxPostSize',
+ description: 'Maximum size of a POST request in bytes',
+ default: 8192
+ })
+ .options('t', {
+ alias: 'maxAttempts',
+ description: 'Maximum number of attempts before abandoning an event',
+ default: 5
+ })
+ .options('r', {
+ alias: 'maxRedirects',
+ description: 'Maximum number of redirects when delivering the event',
+ default: 0
+ })
+ .check(function (args) { return !args.help; })
+ .check(function (args) { return args.p != args.s; })
+ .check(function (args) {
+ args.cert = fs.readFileSync(args.c)
+ args.key = fs.readFileSync(args.k)
+ return true
+ })
+ .check(function (args) {
+ var proxy = args.x === '' ? process.env.HTTP_PROXY : args.x
+ if (proxy) {
+ var i = proxy.indexOf(':')
+ args.proxyHost = i == -1 ? proxy : proxy.substring(0, i)
+ args.proxyPort = i == -1 ? 80 : proxy.substring(i + 1)
+ }
+ return true;
+ })
+ .argv
+
+if (cluster.isMaster)
+ require('./master.js').main(argv)
+else
+ require('./worker.js').main(argv)
View
@@ -0,0 +1,15 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIICXAIBAAKBgQC6KNZ5b9Sb+n1zZ0ImhDEZST45m64tM+nLjDd6jfA1OBEdJo9h
+rYcqeEaiP3NneYGRUwWWtvcAuhXuajG0Df8RzGDJDUa7WlZIEZkXTUr99Ykixj8F
+85sQHk5sVsTfjCEw9bbqdS/uUMzjFcGGf+9r7qe1E2xOiDAwd5ZNMyr4yQIDAQAB
+AoGAeVJaDIR0RD8oeQhnlSB7uyX/tp2eEvmNOcmk8msEjDqA9MWHljn4KBaAugau
+GFaYuXQo5UNSkJe16U4uHFEu1HWTQ4OkVjhJ+CacyaZcJV54KT6N4hC8+TiN1Y/b
+4y1TWxHvFByY6+MI6/ZcqdkyzvqTsl0EM3+7CBeSv5Ia2U0CQQDyFzcsxaXRi+9z
+s8jmIiswIYAGcswCJe5u1a3sgACW5AkHRil5FG3s3PDtFcpTLJlDqILYIIRiT4+D
+26vOpT4/AkEAxNr10VQylRgwzXsMDpjCNm6k6J7eJRToZljpAjn30aBvODeGaNu0
+98+PxPd4mhVfw9lXBGI7tNTsh6gBxf2W9wJBANrFT/8NvYNXydPtLCeLySt9mow5
+QVLPpGBUiQ+nvOCeweno5aGdbJkYMECP6H6xVu9lYJifCgMtkqu938ymV1ECQFs6
+rllIj/iQsW1I7RmGqdrYBAzaM1E0E0/7PGEPxE2d8G05Lk1CJOgDhTlfBsFBzpPR
+EYayj8EKPGPR9KBxGZkCQAEA1hwpb4uk6EIv67SiSN09yy5qISJMRH9gyc6AlZmw
+U7ryfkIU8oiJGiYh6C63NoUS8i0ZTZaahyxNxlX9v5I=
+-----END RSA PRIVATE KEY-----
View
@@ -0,0 +1,30 @@
+var cluster = require('cluster');
+var argv;
+
+function log(thing) {
+ console.log(process.pid + ' (master): ' + thing);
+}
+
+exports.main = function (args) {
+ argv = args;
+ log('http-timeout: distributed timeouts for http')
+ log('Number of workers: ' + argv.w)
+ log('HTTP port: ' + argv.p)
+ log('HTTPS port: ' + argv.s)
+ log('HTTP proxy: ' + (argv.proxyHost ? (argv.proxyHost + ':' + argv.proxyPort) : 'none'))
+ log('Peek lock duration [ms]: ' + argv.l)
+ log('Peek interval [ms]: ' + argv.i)
+ log('Max POST size [bytes]: ' + argv.a)
+ log('Max delivery attempts: ' + argv.t)
+ log('Max redirects: ' + argv.r)
+
+ cluster.on('death', function (worker) {
+ log(new Date() + ' Worker ' + worker.process.pid + ' exited, creating replacement worker.')
+ cluster.fork()
+ });
+
+ for (var i = 0; i < argv.w; i++)
+ cluster.fork()
+
+ log('http-timeout started. Ctrl-C to terminate.')
+}
Oops, something went wrong.

0 comments on commit 50e1e5c

Please sign in to comment.