Skip to content

Commit

Permalink
Merge pull request #36 from MatteoGioioso/fix/setConfig
Browse files Browse the repository at this point in the history
Fix/set config
  • Loading branch information
MatteoGioioso authored Jan 29, 2021
2 parents f23fc7b + 552e939 commit 57d4084
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
46 changes: 46 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,5 +413,51 @@ describe("Serverless client", function() {
}
})
})

it("should persist the config object even when setConfig is called", async function() {
const client = new ServerlessClient({
user: "postgres",
host: "localhost",
database: "postgres",
port: 5433,
connUtilization: 0.09,
debug: true,
maxConnections: 500
});
client.setConfig({
password: "postgres"
})

await client.connect()
await client.end()

expect(client._debug).toBe(true)
expect(client._maxConns.cache.total).toBe(500)
})

it("should setConfig correctly and override previous options", async function() {
const client = new ServerlessClient({
user: "wrong user",
host: "localhost",
database: "postgres",
password: "wrong password",
port: 5433,
connUtilization: 0.09,
debug: true,
maxConnections: 500
});
client.setConfig({
user: "postgres",
password: "postgres",
debug: false,
maxConnections: 8
})

await client.connect()
await client.end()

expect(client._debug).toBe(false)
expect(client._maxConns.cache.total).toBe(8)
})
});
});
28 changes: 14 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,21 +304,21 @@ ServerlessClient.prototype.setConfig = function (config) {

this._maxConns = {
// Cache expiration for getting the max connections value in milliseconds
freqMs: config.maxConnsFreqMs || 60000,
freqMs: this._config.maxConnsFreqMs || 60000,
// If this parameters is set to true it will query to get the maxConnections values,
// to maximize performance you should set the maxConnections yourself.
// Is suggested to manually set the maxConnections and keep this setting to false.
manualMaxConnections: config.manualMaxConnections,
manualMaxConnections: this._config.manualMaxConnections,
cache: {
total: config.maxConnections || 100,
total: this._config.maxConnections || 100,
updated: 0
}
}

this._processCount = {
// Cache expiration for getting the process count value value in milliseconds
freqMs: config.processCountFreqMs || 6000,
cacheEnabled: config.processCountCacheEnabled,
freqMs: this._config.processCountFreqMs || 6000,
cacheEnabled: this._config.processCountCacheEnabled,
cache: {
count: 0,
updated: 0
Expand All @@ -327,29 +327,29 @@ ServerlessClient.prototype.setConfig = function (config) {

// Strategy
this._strategy = {
name: config.strategy || 'minimum_idle_time',
name: this._config.strategy || 'minimum_idle_time',
// The minimum number of seconds that a connection must be idle before the module will recycle it.
minConnIdleTimeSec: config.minConnectionIdleTimeSec || 0.5,
minConnIdleTimeSec: this._config.minConnectionIdleTimeSec || 0.5,
// The bigger, the more idle connections will be killed
// this parameters control how aggressive is going to be your strategy
// default is null which will means LIMIT ALL
maxIdleConnectionsToKill: config.maxIdleConnectionsToKill || null,
maxIdleConnectionsToKill: this._config.maxIdleConnectionsToKill || null,

// The percentage of total connections to use when connecting to your Postgres server.
// A value of 0.75 would use 75% of your total available connections.
// Past this threshold the connection killer will kick in.
connUtilization: config.connUtilization || 0.8
connUtilization: this._config.connUtilization || 0.8
}

// Activate debugging logger
this._debug = config.debug
this._debug = this._config.debug

// Backoff
this._backoff = {
capMs: config.capMs || 1000,
baseMs: config.baseMs || 2,
delayMs: config.delayMs || 1000,
maxRetries: config.maxRetries || 3,
capMs: this._config.capMs || 1000,
baseMs: this._config.baseMs || 2,
delayMs: this._config.delayMs || 1000,
maxRetries: this._config.maxRetries || 3,
retries: 0,
queryRetries: 0
}
Expand Down

0 comments on commit 57d4084

Please sign in to comment.