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 all commits
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
2 changes: 1 addition & 1 deletion packages/phone-number-privacy/signer/.env
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ALFAJORES_ODIS_BLOCKCHAIN_PROVIDER=https://alfajores-forno.celo-testnet.org
MAINNET_ODIS_BLOCKCHAIN_PROVIDER=https://forno.celo.org
ODIS_DOMAINS_TEST_KEY_VERSION=1
ODIS_PNP_TEST_KEY_VERSION=1
DEPLOYED_SIGNER_SERVICE_VERSION=3.0.0
DEPLOYED_SIGNER_SERVICE_VERSION=3.0.1
# PUBKEYS
STAGING_DOMAINS_PUBKEY=7FsWGsFnmVvRfMDpzz95Np76wf/1sPaK0Og9yiB+P8QbjiC8FV67NBans9hzZEkBaQMhiapzgMR6CkZIZPvgwQboAxl65JWRZecGe5V3XO4sdKeNemdAZ2TzQuWkuZoA
ALFAJORES_DOMAINS_PUBKEY=+ZrxyPvLChWUX/DyPw6TuGwQH0glDJEbSrSxUARyP5PuqYyP/U4WZTV1e0bAUioBZ6QCJMiLpDwTaFvy8VnmM5RBbLQUMrMg5p4+CBCqj6HhsMfcyUj8V0LyuNdStlCB
Expand Down
2 changes: 1 addition & 1 deletion packages/phone-number-privacy/signer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@celo/phone-number-privacy-signer",
"version": "3.0.1-dev",
"version": "3.0.2-dev",
"description": "Signing participator of ODIS",
"author": "Celo",
"license": "Apache-2.0",
Expand Down
74 changes: 48 additions & 26 deletions packages/phone-number-privacy/signer/src/common/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,41 @@ export function meteringHandler<R extends OdisRequest>(
histogram: client.Histogram<string>,
handler: PromiseHandler<R>
): PromiseHandler<R> {
return (req, res) => newMeter(histogram, req.url)(() => handler(req, res))
return (req, res) => newMeter(histogram, req.url)(async () => handler(req, res))
}

export function timeoutHandler<R extends OdisRequest>(
timeoutMs: number,
handler: PromiseHandler<R>
): PromiseHandler<R> {
return async (req, res) => {
const timeoutSignal = (AbortSignal as any).timeout(timeoutMs)
timeoutSignal.addEventListener(
'abort',
() => {
if (!res.headersSent) {
Counters.timeouts.inc()
sendFailure(ErrorMessage.TIMEOUT_FROM_SIGNER, 500, res, req.url)
}
},
{ once: true }
)
const timeoutId = setTimeout(() => {
if (!res.headersSent) {
Counters.timeouts.inc()
sendFailure(ErrorMessage.TIMEOUT_FROM_SIGNER, 500, res, req.url)
}
}, timeoutMs)

try {
await handler(req, res)
} finally {
// Clears the timeout if it answers first
clearTimeout(timeoutId)
}
}
}

export function connectionClosedHandler<R extends OdisRequest>(
handler: PromiseHandler<R>
): PromiseHandler<R> {
return async (req, res) => {
req.on('close', () => {
if (res.socket?.destroyed) {
res.locals.logger.info('connection closed')
Counters.connectionClosed.inc()
res.end()
}
})

await handler(req, res)
}
Expand Down Expand Up @@ -141,8 +157,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 +189,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()
}
}
4 changes: 4 additions & 0 deletions packages/phone-number-privacy/signer/src/common/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export const Counters = {
name: 'errors_thrown_after_response_sent',
help: 'Counter for the number of errors thrown after a response was already sent',
}),
connectionClosed: new Counter({
name: 'connection_closed',
help: 'Counter for the number of closed connections caught in Signer',
}),
}
const buckets = [
0.001, 0.01, 0.1, 0.2, 0.3, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.2, 1.4, 1.6, 1.8, 2, 2.3, 2.6, 2.9, 3.5,
Expand Down
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