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

[Relay] Replace mysql with postgres #182

Merged
merged 2 commits into from
Nov 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
22 changes: 11 additions & 11 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ jobs:

runs-on: ${{ matrix.operating-system }}

services:
postgres:
image: postgres:12.0
env:
POSTGRES_USER: root
POSTGRES_PASSWORD: 12345678
POSTGRES_DB: relay
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

strategy:
fail-fast: false
matrix:
Expand All @@ -21,17 +32,6 @@ jobs:
cache: 'yarn'
node-version: ${{ matrix.node-version }}

- name: Setup MySQL 8.0
uses: ankane/setup-mysql@v1
with:
mysql-version: 8.0
database: relay

- name: Config MySQL
run: |
mysqladmin -u root password '12345678'
exit;

- name: Install dependencies
run: yarn install --pure-lockfile
- name: Check formatting of Contract
Expand Down
7 changes: 3 additions & 4 deletions packages/relay/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ server:
database:
host: "${DATABASE_HOST}"
user: "${DATABASE_USER}"
database: "${DATABASE_NAME}"
password: "${DATABASE_PASSWORD}"
database: "${DATABASE_NAME}"
port: "${DATABASE_PORT}"
waitForConnections: true
connectionLimit: 30
queueLimit: 0
connectionTimeoutMillis: 2000
max: 100

################################################################################
## Logging options ##
Expand Down
7 changes: 3 additions & 4 deletions packages/relay/config/config_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ server:
database:
host: "${DATABASE_HOST}"
user: "${DATABASE_USER}"
database: "${DATABASE_NAME}"
password: "${DATABASE_PASSWORD}"
database: "${DATABASE_NAME}"
port: "${DATABASE_PORT}"
waitForConnections: true
connectionLimit: 30
queueLimit: 0
connectionTimeoutMillis: 2000
max: 100

################################################################################
## Logging options ##
Expand Down
29 changes: 18 additions & 11 deletions packages/relay/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
version: '3.3'

services:
mysql:
image: "mysql:8.0"
cap_add:
- SYS_NICE
postgres:
image: postgres:12.0
container_name: postgres
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
- '5432:5432'
restart: always
volumes:
- mysql_db:/var/lib/mysql
command:
[
"postgres",
"-c", "shared_preload_libraries=pg_stat_statements",
"-c", "max_connections=1000"
]
environment:
MYSQL_ROOT_PASSWORD: "12345678"
POSTGRES_PASSWORD: 12345678
POSTGRES_USER: root
POSTGRES_DB: relay
PGDATA: /postgresql/data
POSTGRES_INITDB_ARGS: "-E UTF8 --locale=C"
volumes:
- postgres_db:/postgresql/data

volumes:
mysql_db:
postgres_db:
2 changes: 1 addition & 1 deletion packages/relay/env/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ DATABASE_HOST=127.0.0.1
DATABASE_USER=root
DATABASE_NAME=relay
DATABASE_PASSWORD=12345678
DATABASE_PORT=3306
DATABASE_PORT=5432

# 0xDc245797409fb79446523Fa1A4ca97294eef22EE
DEPLOYER=0x2b5d5cc406b66c0398d0b8327d340cb4f6e30540621802e115506fe001398ba3
Expand Down
3 changes: 2 additions & 1 deletion packages/relay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@types/urijs": "^1.19.12"
},
"dependencies": {
"@types/pg": "^8.10.7",
"argparse": "^2.0.1",
"assert": "^2.0.0",
"axios": "^0.26.0",
Expand All @@ -74,7 +75,7 @@
"ip": "^1.1.5",
"moment": "^2.29.1",
"mybatis-mapper": "^0.7.1",
"mysql2": "^3.6.3",
"pg": "^8.11.3",
"prettier": "^2.5.1",
"prettier-plugin-solidity": "^1.1.1",
"smart-buffer": "^4.1.0",
Expand Down
111 changes: 34 additions & 77 deletions packages/relay/src/common/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,59 +187,38 @@ export class DatabaseConfig implements IDatabaseConfig {
port: number;

/**
* multiple Statements exec config
* number of milliseconds to wait before timing out when connecting a new client
* by default this is 0 which means no timeout
*/
multipleStatements: boolean;
connectionTimeoutMillis: number;

/**
* Determines the pool's action when no connections are available
* and the limit has been reached.
* If true, the pool will queue the connection request and call
* it when one becomes available.
* If false, the pool will immediately call back with an error.
* maximum number of clients the pool should contain
* by default this is set to 10.
*/
waitForConnections: boolean;

/**
* The maximum number of connections to create at once.
*/
connectionLimit: number;

/**
* The maximum number of connection requests the pool
* will queue before returning an error from getConnection.
* If set to 0, there is no limit to the number of queued connection requests.
*/
queueLimit: number;
max: number;

/**
* Constructor
* @param host Mysql database host
* @param user Mysql database user
* @param password Mysql database password
* @param database Mysql database name
* @param port Mysql database port
* @param multipleStatements Mysql allow multiple statement to execute (true / false)
* @param waitForConnections Determines the pool's action when no connections are available
* and the limit has been reached.
* If true, the pool will queue the connection request and call
* it when one becomes available.
* If false, the pool will immediately call back with an error.
* @param connectionLimit The maximum number of connections to create at once.
* @param queueLimit The maximum number of connection requests the pool
* will queue before returning an error from getConnection.
* If set to 0, there is no limit to the number of queued connection requests.
* @param host Postgresql database host
* @param user Postgresql database user
* @param password Postgresql database password
* @param database Postgresql database name
* @param port Postgresql database port
* @param connectionTimeoutMillis Number of milliseconds to wait before
* timing out when connecting a new client.
* By default this is 0 which means no timeout.
* @param max Number of milliseconds to wait before timing out when
* connecting a new client by default this is 0 which means no timeout.
*/
constructor(
host?: string,
user?: string,
password?: string,
database?: string,
port?: number,
multipleStatements?: boolean,
waitForConnections?: boolean,
connectionLimit?: number,
queueLimit?: number
connectionTimeoutMillis?: number,
max?: number
) {
const conf = extend(true, {}, DatabaseConfig.defaultValue());
extend(true, conf, {
Expand All @@ -248,20 +227,16 @@ export class DatabaseConfig implements IDatabaseConfig {
password,
database,
port,
multipleStatements,
waitForConnections,
connectionLimit,
queueLimit,
connectionTimeoutMillis,
max,
});
this.host = conf.host;
this.user = conf.user;
this.password = conf.password;
this.database = conf.database;
this.port = conf.port;
this.multipleStatements = conf.multipleStatements;
this.waitForConnections = conf.waitForConnections;
this.connectionLimit = conf.connectionLimit;
this.queueLimit = conf.queueLimit;
this.connectionTimeoutMillis = conf.connectionTimeoutMillis;
this.max = conf.max;
}

/**
Expand All @@ -272,12 +247,10 @@ export class DatabaseConfig implements IDatabaseConfig {
host: "localhost",
user: "root",
password: "12345678",
database: "boascan",
port: 3306,
multipleStatements: true,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
database: "relay",
port: 5432,
connectionTimeoutMillis: 2000,
max: 20,
};
}

Expand All @@ -293,10 +266,8 @@ export class DatabaseConfig implements IDatabaseConfig {
this.password = conf.password;
this.database = conf.database;
this.port = conf.port;
this.multipleStatements = conf.multipleStatements;
this.waitForConnections = conf.waitForConnections;
this.connectionLimit = conf.connectionLimit;
this.queueLimit = conf.queueLimit;
this.connectionTimeoutMillis = conf.connectionTimeoutMillis;
this.max = conf.max;
}
}

Expand Down Expand Up @@ -494,30 +465,16 @@ export interface IDatabaseConfig {
port: number;

/**
* Multiple Statements execution statement Option
*/
multipleStatements: boolean;

/**
* Determines the pool's action when no connections are available
* and the limit has been reached.
* If true, the pool will queue the connection request and call
* it when one becomes available.
* If false, the pool will immediately call back with an error.
*/
waitForConnections: boolean;

/**
* The maximum number of connections to create at once.
* number of milliseconds to wait before timing out when connecting a new client
* by default this is 0 which means no timeout
*/
connectionLimit: number;
connectionTimeoutMillis: number;

/**
* The maximum number of connection requests the pool
* will queue before returning an error from getConnection.
* If set to 0, there is no limit to the number of queued connection requests.
* maximum number of clients the pool should contain
* by default this is set to 10.
*/
queueLimit: number;
max: number;
}

/**
Expand Down
41 changes: 19 additions & 22 deletions packages/relay/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Config } from "./common/Config";
import { logger, Logger } from "./common/Logger";
import { DefaultServer } from "./DefaultServer";
import { RelayStorage } from "./storage/RelayStorage";
import { Storage } from "./storage/Storage";
import { ContractUtils } from "./utils/ContractUtils";

let server: DefaultServer;

Expand Down Expand Up @@ -34,27 +34,24 @@ async function main() {
logger.info(`address: ${config.server.address}`);
logger.info(`port: ${config.server.port}`);

Storage.waiteForConnection(config.database)
.then(() => {
return RelayStorage.make(config.database);
})
.then(async (storage) => {
server = new DefaultServer(config, storage);
return server.start().catch((error: any) => {
// handle specific listen errors with friendly messages
switch (error.code) {
case "EACCES":
logger.error(`${config.server.port} requires elevated privileges`);
break;
case "EADDRINUSE":
logger.error(`Port ${config.server.port} is already in use`);
break;
default:
logger.error(`An error occurred while starting the server: ${error.stack}`);
}
process.exit(1);
});
});
await ContractUtils.delay(3000);
const storage = await RelayStorage.make(config.database);

server = new DefaultServer(config, storage);
return server.start().catch((error: any) => {
// handle specific listen errors with friendly messages
switch (error.code) {
case "EACCES":
logger.error(`${config.server.port} requires elevated privileges`);
break;
case "EADDRINUSE":
logger.error(`Port ${config.server.port} is already in use`);
break;
default:
logger.error(`An error occurred while starting the server: ${error.stack}`);
}
process.exit(1);
});
}

// We recommend this pattern to be able to use async/await everywhere
Expand Down
Loading
Loading