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) => {
const fetchKey = `#${scope} ${paramsPath}`
if (waitings[fetchKey]) return waitings[fetchKey]
console.time(fetchKey)
waitings[fetchKey] = fn(...paramsPath.split('/')).catch(e => {
let status = 'unknown'
waitings[fetchKey] = fn(...paramsPath.split('/')).then(
result => typeof result === 'object' ? result : { failed: true },
err => {
let status = 'unknown'
if (e.response && e.response.status === 404) {
status = 'not found'
} else if (e.code === 'ECONNABORTED') {
status = 'timeout'
}
if (err.response && err.response.status === 404) {
status = 'not found'
} else if (err.code === 'ECONNABORTED') {
status = 'timeout'
}
const info = status === 'unknown' ? e.stack : e.message
console.error(fetchKey, `LIVEFN_ERR<${status}>`, info)
return { status, failed: true }
}).then(result => {
const info = status === 'unknown' ? err.stack : err.message
console.error(fetchKey, `LIVEFN_ERR<${status}>`, info)
return { status, failed: true }
}).finally(() => {
console.timeEnd(fetchKey)
waitings[fetchKey] = undefined
if (typeof result === 'object') {
return result
} else {
return { failed: true }
}
})
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 { send } = require('micro')
const livePool = require('./live-pool.js')
module.exports = async (req, res) => {
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 = () => {