Skip to content

Commit

Permalink
feat: add native option
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Dec 15, 2021
1 parent deca3c5 commit 347d644
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 53 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const database = {
database: requireEnv('DB_NAME'),
password: requireEnv('DB_PASSWORD'),
port: parseInt(requireEnv('DB_PORT')),
native: true, // optional, use pg-native
}

const registrar = new ShardRegistrar({
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"@babel/runtime": "^7.1.5",
"@jcoreio/typed-event-emitter": "^1.0.0",
"debug": "^4.1.1",
"pg": "^7.7.1",
"pg": "^8.7.1",
"uuid": "^3.3.2"
}
}
29 changes: 17 additions & 12 deletions src/ShardRegistrar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,39 @@
* @prettier
*/
import EventEmitter from '@jcoreio/typed-event-emitter'
import { Client, type Result } from 'pg'
import uuidv4 from 'uuid/v4'
import debug from 'debug'
import ShardReservationCluster from './schema/ShardReservationCluster'
import ShardReservation from './schema/ShardReservation'
const pg = require('pg')

const RESHARD_DEBUG = debug.enabled('ShardRegistrar:reshard')

export type ShardRegistrarEvents = {
shardChanged: [{ shard: number, numShards: number }],
export type ShardRegistrarEvents = {|
shardChanged: [{| shard: number, numShards: number |}],
error: [Error],
}
|}

export type ShardRegistrarOptions = {
export type ShardRegistrarOptions = $ReadOnly<{
cluster: string,
database: {
database: $ReadOnly<{
database: string,
user: string,
password: string,
host: string,
port: number,
},
native?: boolean,
}>,
heartbeatInterval: number,
gracePeriod: number,
reshardInterval: number,
}
}>

export default class ShardRegistrar extends EventEmitter<ShardRegistrarEvents> {
_options: ShardRegistrarOptions
_heartbeatTimeout: ?TimeoutID
_holder: string = uuidv4()
_client: Client
_client: pg.Client
_shard: ?number
_numShards: ?number
_running: boolean = false
Expand All @@ -45,7 +46,9 @@ export default class ShardRegistrar extends EventEmitter<ShardRegistrarEvents> {
constructor(options: ShardRegistrarOptions) {
super()
this._options = options
this._client = new Client(options.database)
this._client = new (this._options.database.native
? pg.native.Client
: pg.Client)(options.database)
}

shardInfo(): { shard: number, numShards: number } {
Expand Down Expand Up @@ -81,7 +84,9 @@ export default class ShardRegistrar extends EventEmitter<ShardRegistrarEvents> {
}
await this._client.end()
this._client.removeListener('error', this._onError)
this._client = new Client(this._options.database)
this._client = new (this._options.database.native
? pg.native.Client
: pg.Client)(this._options.database)
}

_onError = (err: Error) => this.emit('error', err)
Expand Down Expand Up @@ -131,7 +136,7 @@ export default class ShardRegistrar extends EventEmitter<ShardRegistrarEvents> {
}
}

async _query(sql: string, params?: Array<any>): Promise<Result> {
async _query(sql: string, params?: Array<any>): Promise<pg.Result> {
this._debug(sql, params)
if (!this._running) throw new Error('already stopped')
const result = await (this._lastQuery = this._client.query(sql, params))
Expand Down
86 changes: 46 additions & 40 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6089,10 +6089,10 @@ package-json@^4.0.0:
registry-url "^3.0.3"
semver "^5.1.0"

packet-reader@0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/packet-reader/-/packet-reader-0.3.1.tgz#cd62e60af8d7fea8a705ec4ff990871c46871f27"
integrity sha1-zWLmCvjX/qinBexP+ZCHHEaHHyc=
packet-reader@1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz#9238e5480dedabacfe1fe3f2771063f164157d74"
integrity sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==

pacote@^8.1.6:
version "8.1.6"
Expand Down Expand Up @@ -6204,38 +6204,49 @@ performance-now@^2.1.0:
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=

pg-connection-string@0.1.3:
version "0.1.3"
resolved "https://registry.yarnpkg.com/pg-connection-string/-/pg-connection-string-0.1.3.tgz#da1847b20940e42ee1492beaf65d49d91b245df7"
integrity sha1-2hhHsglA5C7hSSvq9l1J2RskXfc=
pg-connection-string@^2.5.0:
version "2.5.0"
resolved "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.5.0.tgz#538cadd0f7e603fc09a12590f3b8a452c2c0cf34"
integrity sha512-r5o/V/ORTA6TmUnyWZR9nCj1klXCO2CEKNRlVuJptZe85QuhFayC7WeMic7ndayT5IRIR0S0xFxFi2ousartlQ==

pg-pool@^2.0.4:
version "2.0.6"
resolved "https://registry.yarnpkg.com/pg-pool/-/pg-pool-2.0.6.tgz#7b561a482feb0a0e599b58b5137fd2db3ad8111c"
integrity sha512-hod2zYQxM8Gt482q+qONGTYcg/qVcV32VHVPtktbBJs0us3Dj7xibISw0BAAXVMCzt8A/jhfJvpZaxUlqtqs0g==
pg-int8@1.0.1:
version "1.0.1"
resolved "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz#943bd463bf5b71b4170115f80f8efc9a0c0eb78c"
integrity sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==

pg-pool@^3.4.1:
version "3.4.1"
resolved "https://registry.npmjs.org/pg-pool/-/pg-pool-3.4.1.tgz#0e71ce2c67b442a5e862a9c182172c37eda71e9c"
integrity sha512-TVHxR/gf3MeJRvchgNHxsYsTCHQ+4wm3VIHSS19z8NC0+gioEhq1okDY1sm/TYbfoP6JLFx01s0ShvZ3puP/iQ==

pg-types@~1.12.1:
version "1.12.1"
resolved "https://registry.yarnpkg.com/pg-types/-/pg-types-1.12.1.tgz#d64087e3903b58ffaad279e7595c52208a14c3d2"
integrity sha1-1kCH45A7WP+q0nnnWVxSIIoUw9I=
pg-protocol@^1.5.0:
version "1.5.0"
resolved "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.5.0.tgz#b5dd452257314565e2d54ab3c132adc46565a6a0"
integrity sha512-muRttij7H8TqRNu/DxrAJQITO4Ac7RmX3Klyr/9mJEOBeIpgnF8f9jAfRz5d3XwQZl5qBjF9gLsUtMPJE0vezQ==

pg-types@^2.1.0:
version "2.2.0"
resolved "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz#2d0250d636454f7cfa3b6ae0382fdfa8063254a3"
integrity sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==
dependencies:
postgres-array "~1.0.0"
pg-int8 "1.0.1"
postgres-array "~2.0.0"
postgres-bytea "~1.0.0"
postgres-date "~1.0.0"
postgres-date "~1.0.4"
postgres-interval "^1.1.0"

pg@^7.7.1:
version "7.7.1"
resolved "https://registry.yarnpkg.com/pg/-/pg-7.7.1.tgz#546b192ff484322b69689391f885de3ba91a30d4"
integrity sha512-p3I0mXOmUvCoVlCMFW6iYSrnguPol6q8He15NGgSIdM3sPGjFc+8JGCeKclw8ZR4ETd+Jxy2KNiaPUcocHZeMw==
pg@^8.7.1:
version "8.7.1"
resolved "https://registry.npmjs.org/pg/-/pg-8.7.1.tgz#9ea9d1ec225980c36f94e181d009ab9f4ce4c471"
integrity sha512-7bdYcv7V6U3KAtWjpQJJBww0UEsWuh4yQ/EjNf2HeO/NnvKjpvhEIe/A/TleP6wtmSKnUnghs5A9jUoK6iDdkA==
dependencies:
buffer-writer "2.0.0"
packet-reader "0.3.1"
pg-connection-string "0.1.3"
pg-pool "^2.0.4"
pg-types "~1.12.1"
packet-reader "1.0.0"
pg-connection-string "^2.5.0"
pg-pool "^3.4.1"
pg-protocol "^1.5.0"
pg-types "^2.1.0"
pgpass "1.x"
semver "4.3.2"

pgpass@1.x:
version "1.0.2"
Expand Down Expand Up @@ -6312,20 +6323,20 @@ posix-character-classes@^0.1.0:
resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab"
integrity sha1-AerA/jta9xoqbAL+q7jB/vfgDqs=

postgres-array@~1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/postgres-array/-/postgres-array-1.0.3.tgz#c561fc3b266b21451fc6555384f4986d78ec80f5"
integrity sha512-5wClXrAP0+78mcsNX3/ithQ5exKvCyK5lr5NEEEeGwwM6NJdQgzIJBVxLvRW+huFpX92F2QnZ5CcokH0VhK2qQ==
postgres-array@~2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz#48f8fce054fbc69671999329b8834b772652d82e"
integrity sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==

postgres-bytea@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/postgres-bytea/-/postgres-bytea-1.0.0.tgz#027b533c0aa890e26d172d47cf9ccecc521acd35"
integrity sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=

postgres-date@~1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/postgres-date/-/postgres-date-1.0.3.tgz#e2d89702efdb258ff9d9cee0fe91bd06975257a8"
integrity sha1-4tiXAu/bJY/52c7g/pG9BpdSV6g=
postgres-date@~1.0.4:
version "1.0.7"
resolved "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz#51bc086006005e5061c591cee727f2531bf641a8"
integrity sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==

postgres-interval@^1.1.0:
version "1.1.2"
Expand Down Expand Up @@ -7046,11 +7057,6 @@ semver-regex@^2.0.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-5.6.0.tgz#7e74256fbaa49c75aa7c7a205cc22799cac80004"
integrity sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==

semver@4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/semver/-/semver-4.3.2.tgz#c7a07158a80bedd052355b770d82d6640f803be7"
integrity sha1-x6BxWKgL7dBSNVt3DYLWZA+AO+c=

semver@5.5.0:
version "5.5.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.0.tgz#dc4bbc7a6ca9d916dee5d43516f0092b58f7b8ab"
Expand Down

0 comments on commit 347d644

Please sign in to comment.