Skip to content

Commit

Permalink
Fix: improve tenderly connection (Uniswap#438)
Browse files Browse the repository at this point in the history
* Log tenderly http status metrics

* keep-alive tenderly connections
  • Loading branch information
jsy1218 authored Nov 15, 2023
1 parent d8daab3 commit bb3fd7a
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/providers/tenderly-simulation-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import {
import { IV2PoolProvider } from './v2/pool-provider';
import { ArbitrumGasData, OptimismGasData } from './v3/gas-data-provider';
import { IV3PoolProvider } from './v3/pool-provider';
import http from 'http';
import https from 'https';

export type TenderlyResponseUniversalRouter = {
config: {
Expand Down Expand Up @@ -166,6 +168,12 @@ export class TenderlySimulator extends Simulator {
private v3PoolProvider: IV3PoolProvider;
private overrideEstimateMultiplier: { [chainId in ChainId]?: number };
private tenderlyRequestTimeout?: number;
private tenderlyServiceInstance = axios.create({
// keep connections alive,
// maxSockets default is Infinity, so Infinity is read as 50 sockets
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
});

constructor(
chainId: ChainId,
Expand Down Expand Up @@ -304,15 +312,18 @@ export class TenderlySimulator extends Simulator {
this.tenderlyProject
);

metric.putMetric('TenderlySimulationUniversalRouterRequests', 1, MetricLoggerUnit.Count);

const before = Date.now();

const resp = (
await axios.post<TenderlyResponseUniversalRouter>(url, body, opts)
).data;
const { data: resp, status: httpStatus } = (
await this.tenderlyServiceInstance.post<TenderlyResponseUniversalRouter>(url, body, opts)
);

const latencies = Date.now() - before
log.info(`Tenderly simulation universal router request body: ${body}, having latencies ${latencies} in milliseconds.`)
metric.putMetric('TenderlySimulationUniversalRouterLatencies', Date.now() - before, MetricLoggerUnit.Milliseconds);
metric.putMetric(`TenderlySimulationUniversalRouterResponseStatus${httpStatus}`, 1, MetricLoggerUnit.Count);

// Validate tenderly response body
if (
Expand Down Expand Up @@ -396,11 +407,15 @@ export class TenderlySimulator extends Simulator {
this.tenderlyProject
);

metric.putMetric('TenderlySimulationSwapRouter02Requests', 1, MetricLoggerUnit.Count);

const before = Date.now()

const resp = (
await axios.post<TenderlyResponseSwapRouter02>(url, body, opts)
).data;
const { data: resp, status: httpStatus } = (
await this.tenderlyServiceInstance.post<TenderlyResponseSwapRouter02>(url, body, opts)
);

metric.putMetric(`TenderlySimulationSwapRouter02ResponseStatus${httpStatus}`, 1, MetricLoggerUnit.Count);

const latencies = Date.now() - before
log.info(`Tenderly simulation swap router02 request body: ${body}, having latencies ${latencies} in milliseconds.`)
Expand Down

0 comments on commit bb3fd7a

Please sign in to comment.