Comparing version 4.1.0-beta to 4.1.0-beta10
30
hiio.js
@@ -360,5 +360,11 @@ 'use strict'; | ||
this.session.on('close', () => { | ||
this.session.on('close', async () => { | ||
this.connected = false | ||
if (this.keepalive && typeof this.reconn === 'function') { | ||
await new Promise((rv, rj) => { | ||
setTimeout(() => { | ||
rv() | ||
}, this.reconnDelay) | ||
}) | ||
this.debug && console.log('Connect closed, reconnect...') | ||
@@ -681,2 +687,8 @@ this.reconn() | ||
on (evt, callback) { | ||
for (let a of this.pool) { | ||
a.on(evt, callback) | ||
} | ||
} | ||
} | ||
@@ -794,14 +806,16 @@ | ||
pending: false, | ||
debug: options.debug === undefined ? false : options.debug, | ||
keepalive : options.keepalive === undefined ? false : true, | ||
reconnDelay : options.reconnDelay === undefined ? 100 : options.reconnDelay | ||
debug: !!options.debug, | ||
keepalive : !!options.keepalive, | ||
reconnDelay : options.reconnDelay === undefined ? 500 : options.reconnDelay | ||
}) | ||
/** | ||
* 延迟重连不能使用setTimeout,因为timer循环会在下一个循环开始执行, | ||
* 而在本次循环,error的处理会先执行,此时,error事件还没有监听。 | ||
*/ | ||
if (options.keepalive) { | ||
newReq.reconn = () => { | ||
options.sessionRequest = newReq | ||
setTimeout(() => { | ||
this.connect(url, options) | ||
newReq.init() | ||
}, newReq.reconnDelay) | ||
this.connect(url, options) | ||
newReq.init() | ||
} | ||
@@ -808,0 +822,0 @@ } |
'use strict' | ||
const hiio = require('./hiio') | ||
const urlparse = require('url') | ||
@@ -67,4 +66,10 @@ let h2cli = new hiio() | ||
this.config = {} | ||
for (let k in options) { | ||
switch (k) { | ||
case 'config': | ||
this.config = options[k] | ||
break | ||
case 'maxBody': | ||
@@ -80,2 +85,3 @@ case 'addIP': | ||
this.setHostProxy(this.config) | ||
@@ -94,3 +100,3 @@ } | ||
hiiproxy.checkConfig = function (tmp) { | ||
hiiproxy.prototype.checkConfig = function (tmp, k) { | ||
@@ -151,5 +157,3 @@ if (typeof tmp !== 'object' || (tmp instanceof Array) ) { | ||
backend_obj.timeout = this.timeout | ||
if (tmp.timeout !== undefined) | ||
if (tmp.timeout !== undefined && typeof tmp.timeout === 'number') | ||
backend_obj.timeout = tmp.timeout | ||
@@ -167,3 +171,2 @@ | ||
for (let k in cfg) { | ||
this.checkConfig(cfg, k) | ||
@@ -173,3 +176,3 @@ for (let i = 0; i < cfg[k].length; i++) { | ||
if (!this.checkConfig(tmp)) continue | ||
if (!this.checkConfig(tmp, k)) continue | ||
@@ -181,6 +184,2 @@ if (this.hostProxy[k] === undefined) { | ||
tmp.urlobj = parseUrl(tmp.url) | ||
tmp.urlobj.timeout = tmp.timeout || this.timeout | ||
pt = fmtpath(tmp.path) | ||
@@ -190,3 +189,2 @@ | ||
url : tmp.url, | ||
urlobj : tmp.urlobj, | ||
headers : null, | ||
@@ -199,4 +197,5 @@ path : tmp.path, | ||
max: 100, | ||
debug: false, | ||
h2Pool: null | ||
debug: this.debug, | ||
h2Pool: null, | ||
timeout: this.timeout | ||
} | ||
@@ -237,3 +236,3 @@ | ||
hiiproxy.prototype.checkAlive = function (pr) { | ||
if (!pr.h2Pool || !pr.h2Pool.pool[0].connected) { | ||
@@ -246,7 +245,7 @@ return false | ||
hiiproxy.prototype.getBackend = function (c) { | ||
hiiproxy.prototype.getBackend = function (c, host) { | ||
let prlist = this.hostProxy[c.host][c.routepath] | ||
let prlist = this.hostProxy[host][c.routepath] | ||
let pxybalance = this.proxyBalance[c.host][c.routepath] | ||
let pxybalance = this.proxyBalance[host][c.routepath] | ||
@@ -300,3 +299,20 @@ let pr | ||
if (!self.hostProxy[c.host] || !self.hostProxy[c.host][c.routepath]) { | ||
let host = c.host | ||
let hind = c.host.length - 1 | ||
if (hind > 4) { | ||
let eind = hind - 5 | ||
while (hind >= eind) { | ||
if (c.host[hind] === ':') { | ||
host = c.host.substring(0, hind) | ||
break | ||
} | ||
hind -= 1 | ||
} | ||
} | ||
if (!self.hostProxy[host] || !self.hostProxy[host][c.routepath]) { | ||
if (self.full) { | ||
@@ -309,3 +325,3 @@ return c.send(error_502_text, 502) | ||
let pr = self.getBackend(c) | ||
let pr = self.getBackend(c, host) | ||
@@ -339,2 +355,5 @@ if (pr === null) { | ||
stm.on('response', (headers, flags) => { | ||
if (pr.headers) { | ||
for (let k in pr.headers) headers[k] = pr.headers[k] | ||
} | ||
c.reply.respond(headers) | ||
@@ -378,3 +397,3 @@ }) | ||
app.config.timeout = this.timeout; | ||
app.config.timeout = this.timeout | ||
@@ -385,52 +404,9 @@ for (let p in this.pathTable) { | ||
app.use(this.mid(), {pre: true, group: `titbit_h2_proxy`}) | ||
app.use(this.mid(), { | ||
pre: true, | ||
group: `titbit_h2_proxy` | ||
}) | ||
for (let k in this.hostProxy) { | ||
this.proxyIntervals[k] = {}; | ||
for (let p in this.hostProxy[k]) { | ||
this.proxyIntervals[k][p] = this.setTimer(this.hostProxy[k][p]); | ||
} | ||
} | ||
} | ||
function parseUrl (url) { | ||
let u = new urlparse.URL(url) | ||
let urlobj = { | ||
hash : u.hash, | ||
hostname : u.hostname, | ||
protocol : u.protocol, | ||
path : u.pathname, | ||
method : 'GET', | ||
headers : { | ||
':method' : 'GET', | ||
}, | ||
} | ||
if (u.search.length > 0) { | ||
urlobj.path += u.search | ||
} | ||
if (u.protocol === 'unix:') { | ||
urlobj.protocol = 'http:' | ||
let sockarr = u.pathname.split('.sock') | ||
urlobj.socketPath = `${sockarr[0]}.sock` | ||
urlobj.path = sockarr[1] | ||
} else { | ||
urlobj.host = u.host | ||
urlobj.port = u.port | ||
} | ||
if (u.protocol === 'https:') { | ||
urlobj.requestCert = false | ||
urlobj.rejectUnauthorized = false | ||
} | ||
return urlobj | ||
} | ||
function fmtpath (path) { | ||
@@ -437,0 +413,0 @@ path = path.trim() |
@@ -5,2 +5,3 @@ 'use strict' | ||
const h2c = require('./hiio.js') | ||
const http2proxy = require('./http2proxy.js') | ||
@@ -10,4 +11,5 @@ module.exports = { | ||
httpii : h2c, | ||
http2proxy, | ||
httpcli : new goh(), | ||
http2cli : new h2c(), | ||
} |
{ | ||
"name": "gohttp", | ||
"version": "4.1.0-beta", | ||
"version": "4.1.0-beta10", | ||
"description": "http & https client for HTTP/1.1 and HTTP/2", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
82305
1855