Use debug

This commit is contained in:
syuilo 2017-01-23 18:25:52 +09:00
parent 6af87eef22
commit 585b8eec23
3 changed files with 27 additions and 10 deletions

View File

@ -79,6 +79,10 @@ Note that `$(pwd)` is the working directory.
## Launch ## Launch
`sudo npm start` `sudo npm start`
## Debug
### Show debug messages
Misskey uses [debug](https://github.com/visionmedia/debug) and namespace is 'misskey:*'.
## Contribute ## Contribute
Do you have feature request or problem with Misskey? Do you have feature request or problem with Misskey?
Please create issue to report it if it is about the Misskey implementation itself. Please create issue to report it if it is about the Misskey implementation itself.

View File

@ -29,6 +29,7 @@
"@types/chalk": "0.4.31", "@types/chalk": "0.4.31",
"@types/compression": "0.0.33", "@types/compression": "0.0.33",
"@types/cors": "2.8.0", "@types/cors": "2.8.0",
"@types/debug": "0.0.29",
"@types/elasticsearch": "5.0.12", "@types/elasticsearch": "5.0.12",
"@types/escape-html": "0.0.19", "@types/escape-html": "0.0.19",
"@types/event-stream": "3.3.30", "@types/event-stream": "3.3.30",
@ -82,6 +83,7 @@
"cors": "2.8.1", "cors": "2.8.1",
"cropperjs": "1.0.0-beta", "cropperjs": "1.0.0-beta",
"crypto": "0.0.3", "crypto": "0.0.3",
"debug": "2.6.0",
"deepcopy": "0.6.3", "deepcopy": "0.6.3",
"del": "2.2.2", "del": "2.2.2",
"elasticsearch": "12.1.3", "elasticsearch": "12.1.3",

View File

@ -1,8 +1,11 @@
import * as Limiter from 'ratelimiter'; import * as Limiter from 'ratelimiter';
import * as debug from 'debug';
import limiterDB from '../db/redis'; import limiterDB from '../db/redis';
import { IEndpoint } from './endpoints'; import { IEndpoint } from './endpoints';
import { IAuthContext } from './authenticate'; import { IAuthContext } from './authenticate';
const log = debug('misskey:limitter');
export default (endpoint: IEndpoint, ctx: IAuthContext) => new Promise((ok, reject) => { export default (endpoint: IEndpoint, ctx: IAuthContext) => new Promise((ok, reject) => {
const limitKey = endpoint.hasOwnProperty('limitKey') const limitKey = endpoint.hasOwnProperty('limitKey')
? endpoint.limitKey ? endpoint.limitKey
@ -24,7 +27,7 @@ export default (endpoint: IEndpoint, ctx: IAuthContext) => new Promise((ok, reje
} }
// Short-term limit // Short-term limit
function min(): void { function min() {
const minIntervalLimiter = new Limiter({ const minIntervalLimiter = new Limiter({
id: `${ctx.user._id}:${limitKey}:min`, id: `${ctx.user._id}:${limitKey}:min`,
duration: endpoint.minInterval, duration: endpoint.minInterval,
@ -32,10 +35,14 @@ export default (endpoint: IEndpoint, ctx: IAuthContext) => new Promise((ok, reje
db: limiterDB db: limiterDB
}); });
minIntervalLimiter.get((limitErr, limit) => { minIntervalLimiter.get((err, info) => {
if (limitErr) { if (err) {
reject('ERR'); return reject('ERR');
} else if (limit.remaining === 0) { }
log(`min remaining: ${info.remaining}`);
if (info.remaining === 0) {
reject('BRIEF_REQUEST_INTERVAL'); reject('BRIEF_REQUEST_INTERVAL');
} else { } else {
if (hasRateLimit) { if (hasRateLimit) {
@ -48,7 +55,7 @@ export default (endpoint: IEndpoint, ctx: IAuthContext) => new Promise((ok, reje
} }
// Long term limit // Long term limit
function max(): void { function max() {
const limiter = new Limiter({ const limiter = new Limiter({
id: `${ctx.user._id}:${limitKey}`, id: `${ctx.user._id}:${limitKey}`,
duration: endpoint.limitDuration, duration: endpoint.limitDuration,
@ -56,10 +63,14 @@ export default (endpoint: IEndpoint, ctx: IAuthContext) => new Promise((ok, reje
db: limiterDB db: limiterDB
}); });
limiter.get((limitErr, limit) => { limiter.get((err, info) => {
if (limitErr) { if (err) {
reject('ERR'); return reject('ERR');
} else if (limit.remaining === 0) { }
log(`max remaining: ${info.remaining}`);
if (info.remaining === 0) {
reject('RATE_LIMIT_EXCEEDED'); reject('RATE_LIMIT_EXCEEDED');
} else { } else {
ok(); ok();