Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit 81c8eb7

Browse files
committed
feat: Joins metric - fix: config
1 parent 585525e commit 81c8eb7

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
22

33
const Hapi = require('hapi')
4-
const merge = require('merge-recursive').recursive
54
const path = require('path')
65
const epimetheus = require('epimetheus')
6+
const merge = require('merge-recursive').recursive
77

88
exports = module.exports
99

@@ -13,7 +13,7 @@ exports.start = (options, callback) => {
1313
options = {}
1414
}
1515

16-
const config = merge(require('./config'), options)
16+
const config = merge(Object.assign({}, require('./config')), Object.assign({}, options))
1717
const log = config.log
1818

1919
const port = options.port || config.hapi.port

src/routes.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = (config, http) => {
1818
const log = config.log
1919
const io = new SocketIO(http.listener)
2020
const proto = new util.Protocol(log)
21+
const getConfig = () => config
2122

2223
proto.addRequest('ss-join', ['multiaddr', 'string', 'function'], join)
2324
proto.addRequest('ss-leave', ['multiaddr'], leave)
@@ -31,9 +32,12 @@ module.exports = (config, http) => {
3132
const nonces = {}
3233

3334
const peersMetric = config.metrics ? new client.Gauge({ name: 'rendezvous_peers', help: 'peers online now' }) : fake.gauge
34-
const dialsSucessTotal = config.metrics ? new client.Counter({ name: 'rendezvous_dials_total_success', help: 'sucessfully completed dials since server started' }) : fake.counter
35+
const dialsSuccessTotal = config.metrics ? new client.Counter({ name: 'rendezvous_dials_total_success', help: 'sucessfully completed dials since server started' }) : fake.counter
3536
const dialsFailureTotal = config.metrics ? new client.Counter({ name: 'rendezvous_dials_total_failure', help: 'failed dials since server started' }) : fake.counter
3637
const dialsTotal = config.metrics ? new client.Counter({ name: 'rendezvous_dials_total', help: 'all dials since server started' }) : fake.counter
38+
const joinsSuccessTotal = config.metrics ? new client.Counter({ name: 'rendezvous_joins_total_success', help: 'sucessfully completed joins since server started' }) : fake.counter
39+
const joinsFailureTotal = config.metrics ? new client.Counter({ name: 'rendezvous_joins_total_failure', help: 'failed joins since server started' }) : fake.counter
40+
const joinsTotal = config.metrics ? new client.Counter({ name: 'rendezvous_joins_total', help: 'all joins since server started' }) : fake.counter
3741

3842
const getPeers = () => this._peers
3943
const refreshMetrics = () => peersMetric.set(Object.keys(getPeers()).length)
@@ -63,12 +67,16 @@ module.exports = (config, http) => {
6367
function join (socket, multiaddr, pub, cb) {
6468
const log = config.log.bind(config.log, '[' + socket.id + ']')
6569

66-
if (config.strictMultiaddr && !util.validateMa(multiaddr)) {
70+
if (getConfig().strictMultiaddr && !util.validateMa(multiaddr)) {
71+
joinsTotal.inc()
72+
joinsFailureTotal.inc()
6773
return cb('Invalid multiaddr')
6874
}
6975

70-
if (config.cryptoChallenge) {
76+
if (getConfig().cryptoChallenge) {
7177
if (!pub.length) {
78+
joinsTotal.inc()
79+
joinsFailureTotal.inc()
7280
return cb('Crypto Challenge required but no Id provided')
7381
}
7482

@@ -80,18 +88,23 @@ module.exports = (config, http) => {
8088
log('response cryptoChallenge', multiaddr)
8189

8290
nonces[socket.id][multiaddr].key.verify(nonces[socket.id][multiaddr].nonce, Buffer.from(pub, 'hex'), (err, ok) => {
83-
if (err) { return cb('Crypto error') } // the errors NEED to be strings otherwise JSON.stringify() turns them into {}
91+
if (err || !ok) {
92+
joinsTotal.inc()
93+
joinsFailureTotal.inc()
94+
}
95+
if (err) { return cb('Crypto error') } // the errors NEED to be a string otherwise JSON.stringify() turns them into {}
8496
if (!ok) { return cb('Signature Invalid') }
8597

8698
joinFinalize(socket, multiaddr, cb)
8799
})
88100
} else {
101+
joinsTotal.inc()
89102
const addr = multiaddr.split('ipfs/').pop()
90103

91104
log('do cryptoChallenge', multiaddr, addr)
92105

93106
util.getIdAndValidate(pub, addr, (err, key) => {
94-
if (err) return cb(err)
107+
if (err) { joinsFailureTotal.inc(); return cb(err) }
95108
const nonce = uuid() + uuid()
96109

97110
socket.once('disconnect', () => {
@@ -102,19 +115,23 @@ module.exports = (config, http) => {
102115
cb(null, nonce)
103116
})
104117
}
105-
} else joinFinalize(socket, multiaddr, cb)
118+
} else {
119+
joinsTotal.inc()
120+
joinFinalize(socket, multiaddr, cb)
121+
}
106122
}
107123

108124
function joinFinalize (socket, multiaddr, cb) {
109-
const log = config.log.bind(config.log, '[' + socket.id + ']')
125+
const log = getConfig().log.bind(getConfig().log, '[' + socket.id + ']')
110126
getPeers()[multiaddr] = socket
127+
joinsSuccessTotal.inc()
111128
refreshMetrics()
112129
socket.addrs.push(multiaddr)
113130
log('registered as', multiaddr)
114131

115132
// discovery
116133

117-
let refreshInterval = setInterval(sendPeers, config.refreshPeerListIntervalMS)
134+
let refreshInterval = setInterval(sendPeers, getConfig().refreshPeerListIntervalMS)
118135

119136
socket.once('ss-leave', function handleLeave (ma) {
120137
if (ma === multiaddr) {
@@ -166,7 +183,7 @@ module.exports = (config, http) => {
166183
}
167184

168185
function dial (socket, from, to, dialId, cb) {
169-
const log = config.log.bind(config.log, '[' + dialId + ']')
186+
const log = getConfig().log.bind(getConfig().log, '[' + dialId + ']')
170187
const s = socket.addrs.filter((a) => a === from)[0]
171188

172189
dialsTotal.inc()
@@ -192,7 +209,7 @@ module.exports = (config, http) => {
192209
return cb(err)
193210
}
194211

195-
dialsSucessTotal.inc()
212+
dialsSuccessTotal.inc()
196213
peer.createProxy(dialId + '.listener', socket)
197214
cb()
198215
})

0 commit comments

Comments
 (0)