Skip to content

Commit 9b6aa2d

Browse files
AUTH-98 | Reduce requestValidityPeriod from 10s to 1s
1 parent dde3b61 commit 9b6aa2d

7 files changed

Lines changed: 51 additions & 23 deletions

File tree

js/config.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/config.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/epicurus-node",
3-
"version": "0.3.2",
3+
"version": "0.3.4",
44
"description": "Redis request/response and PubSub sugar",
55
"main": "js/index.js",
66
"types": "js/index.d.ts",

ts/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export const config = {
2-
requestValidityPeriod: 10000 // milliseconds
2+
requestValidityPeriod: 1 * 1000 // milliseconds
33
}

ts/index.ts

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,44 @@
1-
import { subscribe, publish, setupSubscriptionListener, removeCallbacks, shutdownSubscribers } from './lib/pub_sub'
2-
import { request, server, closeAllClients, enableServers, disableServers } from './lib/request_response'
3-
import { EpicurusRedisConfig, serverCallback, subscribeCallback } from './interface'
4-
import * as redis from 'redis'
51
import * as bluebird from 'bluebird'
2+
import * as redis from 'redis'
63
import { RedisClient } from 'redis'
4+
5+
import { config } from './config'
6+
import {
7+
EpicurusRedisConfig,
8+
serverCallback,
9+
subscribeCallback,
10+
} from './interface'
11+
import {
12+
publish,
13+
removeCallbacks,
14+
setupSubscriptionListener,
15+
shutdownSubscribers,
16+
subscribe,
17+
} from './lib/pub_sub'
18+
import {
19+
closeAllClients,
20+
disableServers,
21+
enableServers,
22+
request,
23+
server,
24+
} from './lib/request_response'
25+
726
bluebird.promisifyAll(redis.RedisClient.prototype)
827
bluebird.promisifyAll(redis.Multi.prototype)
928

10-
export default function Epicurus (redisConfig: EpicurusRedisConfig = {
11-
host: 'localhost',
12-
port: 6379
13-
}): EpicurusPublicInterface {
29+
export default function Epicurus (
30+
redisConfig: EpicurusRedisConfig = {
31+
host: 'localhost',
32+
port: 6379
33+
},
34+
requestTimeout?: number
35+
): EpicurusPublicInterface {
1436
// A separate subscription Redis client is required as once a client has
1537
// called SUBSCRIBE, it is put into a slave mode the does not allow any other
1638
// kind of action
1739
const redisClient = redis.createClient(redisConfig)
1840
const redisSub = redis.createClient(redisConfig)
41+
const requestValidityPeriod = requestTimeout || config.requestValidityPeriod
1942

2043
setupSubscriptionListener(redisSub)
2144
enableServers()
@@ -25,8 +48,8 @@ export default function Epicurus (redisConfig: EpicurusRedisConfig = {
2548
getRedisSubClient: () => redisSub,
2649
subscribe: <T = any>(channel: string, callback: subscribeCallback<T>) => subscribe(redisSub, channel, callback),
2750
publish: (channel: string, body: any) => publish(redisClient, channel, body),
28-
server: <T = any, S = any>(channel: string, callback: serverCallback<T, S>) => server(redisClient, channel, callback),
29-
request: <T = any>(channel: string, body: any) => request<T>(redisClient, channel, body),
51+
server: <T = any, S = any>(channel: string, callback: serverCallback<T, S>) => server(redisClient, channel, callback, requestValidityPeriod),
52+
request: <T = any>(channel: string, body: any) => request<T>(redisClient, channel, body, requestValidityPeriod),
3053
shutdown: () => {
3154
shutdownSubscribers()
3255
disableServers()

ts/lib/request_response/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import * as v4 from 'uuid/v4'
2-
import { config } from '../../config'
2+
3+
import {
4+
EpicurusRequest,
5+
EpicurusResponse,
6+
serverCallback,
7+
} from '../../interface'
38
import { EpicurusError } from '../error'
4-
import { EpicurusRequest, EpicurusResponse, serverCallback } from '../../interface'
9+
510
let clients = []
611
let serversEnabled
712

8-
export function request<T>(redisClient, channel: string, body: any): Promise<T> {
13+
export function request<T>(redisClient, channel: string, body: any, requestValidityPeriod: number): Promise<T> {
914
return new Promise(async (res, rej) => {
1015
let responseValid = true
1116
const reqId = v4()
@@ -28,7 +33,7 @@ export function request<T>(redisClient, channel: string, body: any): Promise<T>
2833

2934
responseValid = false
3035
clientClone.quit()
31-
}, config.requestValidityPeriod)
36+
}, requestValidityPeriod)
3237
} else {
3338
rej(new EpicurusError('Server not found', {
3439
context: { originalRequest: body, channel: channel },
@@ -38,7 +43,7 @@ export function request<T>(redisClient, channel: string, body: any): Promise<T>
3843
responseValid = false
3944
clientClone.quit()
4045
}
41-
}, config.requestValidityPeriod + 100)
46+
}, requestValidityPeriod + 100)
4247

4348
clientClone.brpop(reqId, 0, function (_null, popInfo) {
4449
clearTimeout(timeout)
@@ -72,7 +77,7 @@ export function request<T>(redisClient, channel: string, body: any): Promise<T>
7277
})
7378
}
7479

75-
export async function server<T, S>(redisClient, channel: string, callback: serverCallback<T, S>): Promise<void> {
80+
export async function server<T, S>(redisClient, channel: string, callback: serverCallback<T, S>, serverValidityPeriod: number): Promise<void> {
7681
const clientClone = redisClient.duplicate()
7782
clients.push(clientClone)
7883

@@ -95,7 +100,7 @@ export async function server<T, S>(redisClient, channel: string, callback: serve
95100
const req: EpicurusRequest = JSON.parse(rawRequest)
96101
req.body.channel = channel
97102

98-
if (req.ttl > Date.now() - config.requestValidityPeriod) {
103+
if (req.ttl > Date.now() - serverValidityPeriod) {
99104
callback(req.body, async function (error, result) {
100105
const errorRef = error
101106
? { name: error.name, message: error.message, stack: error.stack, severity: error.severity || 1, status: error.status }

0 commit comments

Comments
 (0)