Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Odis Signer 3.0.1 #10543

Merged
merged 19 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions packages/phone-number-privacy/signer/src/common/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ export function timeoutHandler<R extends OdisRequest>(
}
}

export function connectionClosedHandler<R extends OdisRequest>(
handler: PromiseHandler<R>
): PromiseHandler<R> {
return async (req, res) => {
req.on('close', () => {
if (res.socket?.closed) {
alecps marked this conversation as resolved.
Show resolved Hide resolved
res.end()
}
})

await handler(req, res)
}
}

export async function disabledHandler<R extends OdisRequest>(
req: Request<{}, {}, R>,
response: Response<OdisResponse<R>, Locals>
Expand All @@ -141,8 +155,11 @@ export function resultHandler<R extends OdisRequest>(
): PromiseHandler<R> {
return async (req, res) => {
const result = await resHandler(req, res)
send(res, result.body, result.status, res.locals.logger)
Counters.responses.labels(req.url, result.status.toString()).inc()
// Check if the response was ended
if (!res.writableEnded) {
send(res, result.body, result.status, res.locals.logger)
Counters.responses.labels(req.url, result.status.toString()).inc()
}
}
}

Expand Down Expand Up @@ -170,16 +187,19 @@ function sendFailure(
endpoint: string,
body?: Record<any, any> // TODO remove any
) {
send(
response,
{
success: false,
version: getSignerVersion(),
error,
...body,
},
status,
response.locals.logger
)
Counters.responses.labels(endpoint, status.toString()).inc()
// Check if the response was ended
if (!response.writableEnded) {
send(
response,
{
success: false,
version: getSignerVersion(),
error,
...body,
},
status,
response.locals.logger
)
Counters.responses.labels(endpoint, status.toString()).inc()
}
}
6 changes: 5 additions & 1 deletion packages/phone-number-privacy/signer/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { IncomingMessage, ServerResponse } from 'node:http'
import * as PromClient from 'prom-client'
import {
catchErrorHandler,
connectionClosedHandler,
disabledHandler,
Locals,
meteringHandler,
Expand Down Expand Up @@ -144,7 +145,10 @@ function createHandler<R extends OdisRequest>(
tracingHandler(
meteringHandler(
Histograms.responseLatency,
timeoutHandler(timeoutMs, enabled ? resultHandler(action) : disabledHandler)
timeoutHandler(
timeoutMs,
enabled ? connectionClosedHandler(resultHandler(action)) : disabledHandler
)
)
)
)
Expand Down