server: add fetching pool stats

pull/115/head
Amio 2018-08-13 22:45:56 +08:00
rodzic c0a7402abc
commit 86dfa3f6e2
3 zmienionych plików z 29 dodań i 18 usunięć

Wyświetl plik

@ -1,30 +1,29 @@
const waitings = {} // Cache ongoing fetching, prevent redundant request // Cache ongoing fetching, prevent redundant request
const { waitings } = require('./live-pool.js')
module.exports = async (scope, fn, paramsPath) => { module.exports = async (scope, fn, paramsPath) => {
const fetchKey = `#${scope} ${paramsPath}` const fetchKey = `#${scope} ${paramsPath}`
if (waitings[fetchKey]) return waitings[fetchKey] if (waitings[fetchKey]) return waitings[fetchKey]
console.time(fetchKey) console.time(fetchKey)
waitings[fetchKey] = fn(...paramsPath.split('/')).catch(e => { waitings[fetchKey] = fn(...paramsPath.split('/')).then(
let status = 'unknown' result => typeof result === 'object' ? result : { failed: true },
err => {
let status = 'unknown'
if (e.response && e.response.status === 404) { if (err.response && err.response.status === 404) {
status = 'not found' status = 'not found'
} else if (e.code === 'ECONNABORTED') { } else if (err.code === 'ECONNABORTED') {
status = 'timeout' status = 'timeout'
} }
const info = status === 'unknown' ? e.stack : e.message const info = status === 'unknown' ? err.stack : err.message
console.error(fetchKey, `LIVEFN_ERR<${status}>`, info) console.error(fetchKey, `LIVEFN_ERR<${status}>`, info)
return { status, failed: true }
}).then(result => { return { status, failed: true }
}).finally(() => {
console.timeEnd(fetchKey) console.timeEnd(fetchKey)
waitings[fetchKey] = undefined waitings[fetchKey] = undefined
if (typeof result === 'object') {
return result
} else {
return { failed: true }
}
}) })
return waitings[fetchKey] return waitings[fetchKey]

10
libs/live-pool.js 100644
Wyświetl plik

@ -0,0 +1,10 @@
const waitings = {}
const list = () => {
return waitings
}
module.exports = {
waitings,
list
}

Wyświetl plik

@ -1,10 +1,12 @@
const axios = require('axios') const axios = require('axios')
const { send } = require('micro') const { send } = require('micro')
const livePool = require('./live-pool.js')
module.exports = async (req, res) => { module.exports = async (req, res) => {
const [githubRateLimit] = await Promise.all([getGithubRateLimit()]) const [githubRateLimit] = await Promise.all([getGithubRateLimit()])
const fetching = Object.values(livePool.list()).filter(Boolean).length
send(res, 200, { githubRateLimit }) send(res, 200, { githubRateLimit, fetching })
} }
const getGithubRateLimit = () => { const getGithubRateLimit = () => {