add connection timeout option

This commit is contained in:
you21979 2017-10-30 17:50:42 +09:00
parent 71e7dc3cdf
commit 66bd1bbf1c
5 changed files with 73 additions and 11 deletions

View File

@ -1,5 +1,5 @@
'use strict';
const ElectrumClient = require('electrum-client');
const ElectrumClient = require('..');
const createRiaaClient = (port, host, protocol, options) => {
return (params, promise) => {
@ -22,7 +22,9 @@ const createRiaaClient = (port, host, protocol, options) => {
}
const main = async(hex) => {
const connect = createRiaaClient(50001, 'electrumx.tamami-foundation.org', 'tcp')
const hosts = ['electrum-mona.bitbank.cc', 'electrumx.tamami-foundation.org']
const host = hosts[Math.floor(Math.random() * hosts.length)]
const connect = createRiaaClient(50001, host, 'tcp')
await connect(['blockchainTransaction_broadcast', hex], async(client) => {
const ver = await client.server_version('2.7.11', '1.0')
console.log(ver)
@ -35,4 +37,4 @@ const getopt = () => {
return process.argv.slice(2)[0]
}
main(getopt())
main(getopt()).catch(console.log)

40
example/riaa_timeout.js Normal file
View File

@ -0,0 +1,40 @@
'use strict';
const ElectrumClient = require('..');
const createRiaaClient = (port, host, protocol, options) => {
return (params, promise) => {
const name = params.join(':')
const client = new ElectrumClient(port, host, protocol, options)
console.time(name)
return client.connect().then( () => {
return promise(client)
}).catch( e => {
client.close()
console.timeEnd(name)
throw e
}).then( res => {
client.close()
console.timeEnd(name)
return res
})
}
}
const main = async(hex) => {
const hosts = ['electrum-mona.bitbank.cc', 'electrumx.tamami-foundation.org']
const host = hosts[Math.floor(Math.random() * hosts.length)]
const connect = createRiaaClient(50000, host, 'tcp')
await connect(['blockchainTransaction_broadcast', hex], async(client) => {
const ver = await client.server_version('2.7.11', '1.0')
console.log(ver)
const result = await client.blockchainTransaction_broadcast(hex)
console.log(result)
})
}
const getopt = () => {
return process.argv.slice(2)[0]
}
main(getopt()).catch(console.log)

View File

@ -2,6 +2,7 @@
const EventEmitter = require('events').EventEmitter
const util = require('./util')
const initSocket = require('./init_socket')
const connectSocket = require('./connect_socket')
class Client{
constructor(port, host, protocol = 'tcp', options = void 0){
@ -22,14 +23,7 @@ class Client{
return Promise.resolve()
}
this.status = 1
return new Promise((resolve, reject) => {
const errorHandler = (e) => reject(e)
this.conn.connect(this.port, this.host, () => {
this.conn.removeListener('error', errorHandler)
resolve()
})
this.conn.on('error', errorHandler)
})
return connectSocket(this.conn, this.port, this.host)
}
close(){

14
lib/connect_socket.js Normal file
View File

@ -0,0 +1,14 @@
'use strict';
const connectSocket = (conn, port, host) => {
return new Promise((resolve, reject) => {
const errorHandler = (e) => reject(e)
conn.connect(port, host, () => {
conn.removeListener('error', errorHandler)
resolve()
})
conn.on('error', errorHandler)
})
}
module.exports = connectSocket

View File

@ -14,20 +14,32 @@ const getSocket = (protocol, options) => {
}
const initSocket = (self, protocol, options) => {
options = options || {}
const conn = getSocket(protocol, options);
conn.setTimeout(options.timeout || 5000)
conn.setEncoding('utf8')
conn.setKeepAlive(true, 0)
conn.setNoDelay(true)
conn.on('connect', () => {
conn.setTimeout(0)
self.onConnect()
})
conn.on('close', (e) => {
self.onClose(e)
})
conn.on('timeout', () => {
const e = new Error('ETIMEDOUT')
e.errorno = 'ETIMEDOUT'
e.code = 'ETIMEDOUT'
e.connect = false
conn.emit('error', e)
})
conn.on('data', (chunk) => {
conn.setTimeout(0)
self.onRecv(chunk)
})
conn.on('end', (e) => {
conn.setTimeout(0)
self.onEnd(e)
})
conn.on('error', (e) => {