You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+31-2Lines changed: 31 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -249,6 +249,9 @@ Below is a table containing all of the possible configuration options for `serve
249
249
| zombieMaxTimeout |`Integer`| The maximum number of seconds that a connection can stay idle before being recycled. |`900`|
250
250
| zombieMinTimeout |`Integer`| The minimum number of *seconds* that a connection must be idle before the module will recycle it. |`3`|
251
251
| returnFinalSqlQuery |`Boolean`| Flag indicating whether to attach the final SQL query (with substituted values) to the results. When enabled, the SQL query will be available as a non-enumerable `sql` property on array results or as a regular property on object results. |`false`|
252
+
| maxQueryRetries |`Integer`| Maximum number of times to retry a query before giving up. |`0`|
253
+
| queryRetryBackoff |`String` or `Function`| Backoff algorithm to be used when retrying queries. Possible values are `full` and `decorrelated`, or you can also specify your own algorithm. See [Connection Backoff](#connection-backoff) for more information. |`full`|
254
+
| onQueryRetry |`function`|[Event](#events) callback when queries are retried. ||
252
255
253
256
### Connection Backoff
254
257
If `manageConns` is not set to `false`, then this module will automatically kill idle connections or disconnect the current connection if the `connUtilization` limit is reached. Even with this aggressive strategy, it is possible that multiple functions will be competing for available connections. The `backoff` setting uses the strategy outlined [here](https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/) to use *Jitter* instead of *Exponential Backoff* when attempting connection retries.
@@ -323,6 +326,12 @@ The `onConnect` event recieves the MySQL `connection` object, `onKill` receives
`onQueryRetry` also receives *four* arguments. The `error` object, the number of `retries`, the `delay` until the next retry, and the `backoff` algorithm used (`full`, `decorrelated` or `custom`).
There really isn't anything special that needs to be done in order for your MySQL server (including RDS, Aurora, and Aurora Serverless) to use `serverless-mysql`. You should just be aware of the following two scenarios.
328
337
@@ -391,5 +400,25 @@ Other tests that use larger configurations were extremely successful too, but I'
391
400
## Contributions
392
401
Contributions, ideas and bug reports are welcome and greatly appreciated. Please add [issues](https://github.com/jeremydaly/serverless-mysql/issues) for suggestions and bug reports or create a pull request.
393
402
394
-
## TODO
395
-
- Add connection retries on failed queries
403
+
## Query Retries
404
+
The module supports automatic retries for transient query errors. When a query fails with a retryable error (such as deadlocks, timeouts, or connection issues), the module will automatically retry the query using the configured backoff strategy.
405
+
406
+
By default, query retries are disabled (maxQueryRetries = 0) for backward compatibility with previous versions. To enable this feature, set `maxQueryRetries` to a value greater than 0.
407
+
408
+
You can configure the maximum number of retries with the `maxQueryRetries` option (default: 0) and the backoff strategy with the `queryRetryBackoff` option (default: 'full'). The module will use the same backoff algorithms as for connection retries.
409
+
410
+
```javascript
411
+
constmysql=require('serverless-mysql')({
412
+
config: {
413
+
host:process.env.ENDPOINT,
414
+
database:process.env.DATABASE,
415
+
user:process.env.USERNAME,
416
+
password:process.env.PASSWORD
417
+
},
418
+
maxQueryRetries:5, // Enable query retries with 5 maximum attempts
419
+
queryRetryBackoff:'decorrelated',
420
+
onQueryRetry: (err, retries, delay, type) => {
421
+
console.log(`Retrying query after error: ${err.code}, attempt: ${retries}, delay: ${delay}ms`)
* This also attaches the SQL query to error objects when a query fails, making it easier to debug. false
93
93
*/
94
94
returnFinalSqlQuery?: boolean;
95
+
/**
96
+
* Integer Maximum number of times to retry a query before giving up. 0
97
+
*/
98
+
maxQueryRetries?: number;
99
+
/**
100
+
* String or Function Backoff algorithm to be used when retrying queries. Possible values are full and decorrelated, or you can also specify your own algorithm. full
101
+
*/
102
+
queryRetryBackoff?: string|Function;
103
+
/**
104
+
* function Event callback when queries are retried.
0 commit comments