Skip to content

Commit

Permalink
keep-alive时上报hits和bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Mar 4, 2020
1 parent 7bd3153 commit 1d5f9d8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
20 changes: 19 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"express": "^4.17.1",
"fs-extra": "^8.1.0",
"got": "^10.5.2",
"lodash.clone": "^4.5.0",
"morgan": "^1.9.1",
"ms": "^2.1.2",
"progress": "^2.0.3",
Expand All @@ -35,6 +36,7 @@
"@types/dotenv": "^6.1.1",
"@types/express": "^4.17.0",
"@types/fs-extra": "^8.0.0",
"@types/lodash.clone": "^4.5.6",
"@types/morgan": "^1.7.36",
"@types/ms": "^0.7.30",
"@types/node": "^12.6.9",
Expand Down
21 changes: 19 additions & 2 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@ import * as colors from 'colors/safe'
import * as express from 'express'
// eslint-disable-next-line no-duplicate-imports
import {NextFunction, Request, Response} from 'express'
import {outputFile, pathExists} from 'fs-extra'
import {outputFile, pathExists, stat} from 'fs-extra'
import got, {Got} from 'got'
import {createServer, Server} from 'http'
import {join} from 'path'
import * as ProgressBar from 'progress'
import * as io from 'socket.io-client'
import morgan = require('morgan')
import clone = require('lodash.clone')
import Socket = SocketIOClient.Socket

interface IFileList {
files: {path: string; hash: string; size: number}[]
}

interface ICounters {
hits: number
bytes: number
}

export class Cluster {
public readonly counters: ICounters = {hits: 0, bytes: 0}

private readonly prefixUrl = process.env.CLUSTER_BMCLAPI || 'https://openbmclapi.bangbang93.com'
private readonly cacheDir = join(__dirname, '..', 'cache')
private readonly host: string
Expand Down Expand Up @@ -111,6 +119,9 @@ export class Cluster {
if (name) {
res.attachment(name)
}
const stats = await stat(path)
this.counters.bytes += stats.size
this.counters.hits++
return res.sendFile(path)
} catch (err) {
return next(err)
Expand Down Expand Up @@ -162,7 +173,13 @@ export class Cluster {

public async keepAlive(): Promise<boolean> {
return new Promise((resolve) => {
this.io.emit('keep-alive', new Date(), (date) => {
const counters = clone(this.counters)
this.io.emit('keep-alive', {
time: new Date(),
counters,
}, (date) => {
this.counters.hits -= counters.hits
this.counters.bytes -= counters.bytes
resolve(date)
})
})
Expand Down

0 comments on commit 1d5f9d8

Please sign in to comment.