-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathHueApiRateLimits.ts
61 lines (51 loc) · 1.7 KB
/
HueApiRateLimits.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
export type RateLimit = {
maxConcurrent: number,
minTime: number,
}
// Set up a limiter on the group state changes from the library to once per second as per guidance documentation
const DEFAULT_GROUP_RATE_LIMITS: RateLimit = {
maxConcurrent: 1,
minTime: 1000 // how long to wait before launching another request in ms
}
// As per Bridge documentation guidance, limit the number of calls to the light state changes to 10 per second max
const DEFAULT_LIGHT_RATE_LIMITS: RateLimit = {
maxConcurrent: 1,
minTime: 60 // how long to wait before launching another request in ms
}
// Limiting the general rate limits across the API to a hue bridge
const DEFAULT_RATE_LIMITS: RateLimit = {
maxConcurrent: 4,
minTime: 50 // how long to wait before launching another request in ms
}
export type RateLimits = {
transport?: RateLimit,
group?: RateLimit,
light?: RateLimit,
}
export class HueApiRateLimits {
private _rateLimits: RateLimits;
constructor(rateLimits?: RateLimits) {
if (rateLimits) {
this._rateLimits = {
transport: rateLimits.transport || DEFAULT_RATE_LIMITS,
group: rateLimits.group || DEFAULT_GROUP_RATE_LIMITS,
light: rateLimits.light || DEFAULT_LIGHT_RATE_LIMITS
}
} else {
this._rateLimits = {
transport: DEFAULT_RATE_LIMITS,
group: DEFAULT_GROUP_RATE_LIMITS,
light: DEFAULT_LIGHT_RATE_LIMITS,
}
}
}
get groupRateLimit(): RateLimit {
return this._rateLimits.group || DEFAULT_GROUP_RATE_LIMITS;
}
get lightRateLimit(): RateLimit {
return this._rateLimits.light || DEFAULT_LIGHT_RATE_LIMITS;
}
get transportRateLimit(): RateLimit {
return this._rateLimits.transport || DEFAULT_RATE_LIMITS;
}
}