Skip to content

Commit a46fcc6

Browse files
committed
feat: add msg/event rate limit to settings
1 parent 59c6f80 commit a46fcc6

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

src/@types/settings.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export interface PubkeyLimits {
2121

2222
export type EventKindsRange = [EventKinds, EventKinds]
2323

24+
export interface EventRateLimit {
25+
kinds?: (EventKinds | [EventKinds, EventKinds])[]
26+
rate: number
27+
period: number
28+
}
29+
2430
export interface KindLimits {
2531
whitelist?: (EventKinds | EventKindsRange)[]
2632
blacklist?: (EventKinds | EventKindsRange)[]
@@ -42,6 +48,7 @@ export interface EventLimits {
4248
pubkey?: PubkeyLimits
4349
kind?: KindLimits
4450
createdAt?: CreatedAtLimits
51+
rateLimits?: EventRateLimit[]
4552
}
4653

4754
export interface ClientSubscriptionLimits {
@@ -53,9 +60,20 @@ export interface ClientLimits {
5360
subscription?: ClientSubscriptionLimits
5461
}
5562

63+
export interface MessageRateLimit {
64+
rate: number
65+
period: number
66+
}
67+
68+
export interface MessageLimits {
69+
rateLimits?: MessageRateLimit[]
70+
ipWhitelist?: string[]
71+
}
72+
5673
export interface Limits {
5774
client?: ClientLimits
5875
event?: EventLimits
76+
message?: MessageLimits
5977
}
6078

6179
export interface Worker {

src/utils/settings.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { join } from 'path'
44
import { mergeDeepRight } from 'ramda'
55

66
import { createLogger } from '../factories/logger-factory'
7+
import { EventKinds } from '../constants/base'
78
import { ISettings } from '../@types/settings'
89
import packageJson from '../../package.json'
910

1011
const debug = createLogger('settings')
12+
1113
export class SettingsStatic {
1214
static _settings: ISettings
1315

@@ -48,13 +50,53 @@ export class SettingsStatic {
4850
maxPositiveDelta: 900,
4951
maxNegativeDelta: 0, // disabled
5052
},
53+
rateLimits: [
54+
{
55+
kinds: [EventKinds.TEXT_NOTE],
56+
period: 60000,
57+
rate: 60,
58+
},
59+
{
60+
kinds: [[EventKinds.EPHEMERAL_FIRST, EventKinds.EPHEMERAL_LAST]],
61+
period: 60000,
62+
rate: 240,
63+
},
64+
{
65+
period: 3600000,
66+
rate: 3600,
67+
},
68+
{
69+
period: 86400000,
70+
rate: 86400,
71+
},
72+
],
5173
},
5274
client: {
5375
subscription: {
5476
maxSubscriptions: 10,
5577
maxFilters: 10,
5678
},
5779
},
80+
message: {
81+
rateLimits: [
82+
{
83+
period: 60000, // minute
84+
rate: 240,
85+
},
86+
{
87+
period: 3600000, // hour
88+
rate: 3600,
89+
},
90+
{
91+
period: 86400000, // day
92+
rate: 86400,
93+
},
94+
],
95+
ipWhitelist: [
96+
'::1', // local host
97+
'::ffff:10.10.10.1', // host running docker
98+
],
99+
},
58100
},
59101
}
60102
}

test/unit/utils/settings.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ describe('SettingsStatic', () => {
7979
maxFilters: 10,
8080
},
8181
},
82+
message: {
83+
dailyRate: 86400,
84+
hourlyRate: 3600,
85+
minutelyRate: 240,
86+
ipWhitelist: [
87+
'::1',
88+
'::ffff:10.10.10.1',
89+
],
90+
},
8291
})
8392
})
8493
})

0 commit comments

Comments
 (0)