Skip to content

Commit

Permalink
Added ping route
Browse files Browse the repository at this point in the history
  • Loading branch information
arunesh90 committed May 2, 2020
1 parent 0de491d commit f06c0e6
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/lib/api/bulkDelete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequestHandler } from 'fastify';
import { mainDB, callbackHandler } from '../db/redis';
import { RequestHandler } from 'fastify'
import { mainDB, callbackHandler } from '../db/redis'

const bulkDeleteKeyRoute: RequestHandler = async (request, reply) => {
const { keys } = request.query
Expand Down
4 changes: 2 additions & 2 deletions src/lib/api/delete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequestHandler } from 'fastify';
import { mainDB, callbackHandler } from '../db/redis';
import { RequestHandler } from 'fastify'
import { mainDB, callbackHandler } from '../db/redis'

const deleteKeyRoute: RequestHandler = async (request, reply) => {
const { key } = request.params
Expand Down
26 changes: 26 additions & 0 deletions src/lib/api/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { RequestHandler, FastifyReply } from 'fastify'
import { performance } from 'perf_hooks'
import { pingAsync } from '../utils/promisfiedCommands'
import { ServerResponse } from 'http'

const timeout = 1000

const timeoutHandler = (reply: FastifyReply<ServerResponse>) => {
reply.status(408).send('Ping attempt timed out')
}

const pingRoute: RequestHandler = async (_request, reply) => {
const beginTime = performance.now()
const timeoutTimer = setTimeout(() => timeoutHandler(reply), timeout)
const ping = await pingAsync()

clearTimeout(timeoutTimer)

if (ping !== 'PONG') {
return reply.status(500).send(`Received unexpected ping response: ${ping}`)
}

reply.send(performance.now() - beginTime)
}

export default pingRoute
2 changes: 2 additions & 0 deletions src/lib/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import getKeyRoute from './api/get'
import setKeyRoute from './api/set'
import deleteKeyRoute from './api/delete'
import bulkDeleteKeyRoute from './api/bulkDelete'
import pingRoute from './api/ping'

const authKey = process.env.AUTH_KEY

Expand All @@ -23,6 +24,7 @@ const apiRouter = async (app: FastifyInstance) => {
next()
})

app.get('/ping', pingRoute)
app.get('/get/:key', getKeyRoute)
app.post('/set/:key', setKeyRoute)
app.delete('/delete/:key', deleteKeyRoute)
Expand Down
4 changes: 4 additions & 0 deletions src/lib/utils/promisfiedCommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { promisify } from 'util'
import { mainDB } from '../db/redis'

export const pingAsync = promisify(mainDB.ping).bind(mainDB)
3 changes: 3 additions & 0 deletions src/lib/utils/sleep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function sleep (ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cluster from 'cluster'
import os from 'os'

if (process.env.DISABLE_CLUSTER !== "true" && cluster.isMaster) {
const numCPUs = os.cpus().length;
const numCPUs = os.cpus().length
for (let i = 0; i < numCPUs; i++) {
cluster.fork()
}
Expand Down

0 comments on commit f06c0e6

Please sign in to comment.