jinaga-server 3.3.6 → 3.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -11,7 +11,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.ConnectionFactory = void 0;
|
|
13
13
|
const jinaga_1 = require("jinaga");
|
|
14
|
-
const pg_1 = require("pg");
|
|
15
14
|
const promise_1 = require("../util/promise");
|
|
16
15
|
class ConnectionFactory {
|
|
17
16
|
constructor(postgresPool) {
|
|
@@ -19,38 +18,24 @@ class ConnectionFactory {
|
|
|
19
18
|
}
|
|
20
19
|
withTransaction(callback) {
|
|
21
20
|
return this.with((connection) => __awaiter(this, void 0, void 0, function* () {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
catch (e) {
|
|
32
|
-
jinaga_1.Trace.warn("Postgres transaction error: " + describeError(e));
|
|
33
|
-
yield connection.query('ROLLBACK');
|
|
34
|
-
if (e instanceof pg_1.DatabaseError && e.code === '23505') {
|
|
35
|
-
jinaga_1.Trace.warn("Postgres duplicate key: retrying");
|
|
36
|
-
attempts--;
|
|
37
|
-
if (attempts === 0) {
|
|
38
|
-
throw e;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
jinaga_1.Trace.error(e);
|
|
43
|
-
throw e;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
21
|
+
try {
|
|
22
|
+
yield connection.query('BEGIN');
|
|
23
|
+
const result = yield callback(connection);
|
|
24
|
+
yield connection.query('COMMIT');
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
yield connection.query('ROLLBACK');
|
|
29
|
+
throw e;
|
|
46
30
|
}
|
|
47
31
|
}));
|
|
48
32
|
}
|
|
49
33
|
with(callback) {
|
|
50
34
|
return __awaiter(this, void 0, void 0, function* () {
|
|
51
35
|
let attempt = 0;
|
|
52
|
-
const
|
|
53
|
-
|
|
36
|
+
const maxAttempts = 4;
|
|
37
|
+
const baseDelay = 10;
|
|
38
|
+
while (attempt < maxAttempts) {
|
|
54
39
|
try {
|
|
55
40
|
const client = yield this.createClient();
|
|
56
41
|
try {
|
|
@@ -67,14 +52,14 @@ class ConnectionFactory {
|
|
|
67
52
|
}
|
|
68
53
|
jinaga_1.Trace.warn("Postgres transient error: " + describeError(e));
|
|
69
54
|
attempt++;
|
|
70
|
-
if (attempt ===
|
|
55
|
+
if (attempt === maxAttempts) {
|
|
56
|
+
jinaga_1.Trace.error("Postgres error after max attempts: " + describeError(e));
|
|
71
57
|
throw e;
|
|
72
58
|
}
|
|
73
59
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
60
|
+
const delayTime = baseDelay * Math.pow(2, attempt - 1);
|
|
61
|
+
jinaga_1.Trace.warn("Attempt number " + attempt + ". Postgres retrying in " + delayTime + "ms");
|
|
62
|
+
yield (0, promise_1.delay)(delayTime);
|
|
78
63
|
}
|
|
79
64
|
throw new Error("Number of attempts exceeded");
|
|
80
65
|
});
|
|
@@ -85,7 +70,7 @@ class ConnectionFactory {
|
|
|
85
70
|
}
|
|
86
71
|
exports.ConnectionFactory = ConnectionFactory;
|
|
87
72
|
function isTransientError(e) {
|
|
88
|
-
if (e.code === 'ECONNREFUSED') {
|
|
73
|
+
if (e.code === 'ECONNREFUSED' || e.code === '23505') { // 23505 is the code for unique_violation
|
|
89
74
|
return true;
|
|
90
75
|
}
|
|
91
76
|
return false;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.js","sourceRoot":"","sources":["../../src/postgres/connection.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mCAA+B;
|
|
1
|
+
{"version":3,"file":"connection.js","s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"pg": "^8.13.1"
|
|
19
19
|
}
|
|
@@ -2980,9 +2980,9 @@
|
|
|
2980
2980
|
}
|
|
2981
2981
|
},
|
|
2982
2982
|
"node_modules/jinaga": {
|
|
2983
|
-
"version": "6.5.
|
|
2984
|
-
"resolved": "https://registry.npmjs.org/jinaga/-/jinaga-6.5.
|
|
2985
|
-
"integrity": "sha512-
|
|
2983
|
+
"version": "6.5.3",
|
|
2984
|
+
"resolved": "https://registry.npmjs.org/jinaga/-/jinaga-6.5.3.tgz",
|
|
2985
|
+
"integrity": "sha512-y3yInu8z4+I5ZwjFBeXxkJPN7hPo1jwFsjEJmiIKd7yLShsAFwYkd2WWSZMjCvqfU/LuziFjsJw5XDjWK3CzFQ==",
|
|
2986
2986
|
"license": "MIT",
|
|
2987
2987
|
"dependencies": {
|
|
2988
2988
|
"@stablelib/base64": "^1.0.1",
|
package/package.json
CHANGED
|
@@ -10,38 +10,24 @@ export class ConnectionFactory {
|
|
|
10
10
|
|
|
11
11
|
withTransaction<T>(callback: (connection: PoolClient) => Promise<T>) {
|
|
12
12
|
return this.with(async connection => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const result = await callback(connection);
|
|
19
|
-
await connection.query('COMMIT');
|
|
20
|
-
return result;
|
|
21
|
-
}
|
|
22
|
-
catch (e) {
|
|
23
|
-
Trace.warn("Postgres transaction error: " + describeError(e));
|
|
24
|
-
await connection.query('ROLLBACK');
|
|
25
|
-
if (e instanceof DatabaseError && e.code === '23505') {
|
|
26
|
-
Trace.warn("Postgres duplicate key: retrying");
|
|
27
|
-
attempts--;
|
|
28
|
-
if (attempts === 0) {
|
|
29
|
-
throw e;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
Trace.error(e);
|
|
34
|
-
throw e;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
13
|
+
try {
|
|
14
|
+
await connection.query('BEGIN');
|
|
15
|
+
const result = await callback(connection);
|
|
16
|
+
await connection.query('COMMIT');
|
|
17
|
+
return result;
|
|
37
18
|
}
|
|
38
|
-
|
|
19
|
+
catch (e) {
|
|
20
|
+
await connection.query('ROLLBACK');
|
|
21
|
+
throw e;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
39
24
|
}
|
|
40
25
|
|
|
41
26
|
async with<T>(callback: (connection: PoolClient) => Promise<T>) : Promise<T> {
|
|
42
27
|
let attempt = 0;
|
|
43
|
-
const
|
|
44
|
-
|
|
28
|
+
const maxAttempts = 4;
|
|
29
|
+
const baseDelay = 10;
|
|
30
|
+
while (attempt < maxAttempts) {
|
|
45
31
|
try {
|
|
46
32
|
const client = await this.createClient();
|
|
47
33
|
try {
|
|
@@ -58,14 +44,14 @@ export class ConnectionFactory {
|
|
|
58
44
|
}
|
|
59
45
|
Trace.warn("Postgres transient error: " + describeError(e));
|
|
60
46
|
attempt++;
|
|
61
|
-
if (attempt ===
|
|
47
|
+
if (attempt === maxAttempts) {
|
|
48
|
+
Trace.error("Postgres error after max attempts: " + describeError(e));
|
|
62
49
|
throw e;
|
|
63
50
|
}
|
|
64
51
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
52
|
+
const delayTime = baseDelay * Math.pow(2, attempt-1);
|
|
53
|
+
Trace.warn("Attempt number " + attempt + ". Postgres retrying in " + delayTime + "ms");
|
|
54
|
+
await delay(delayTime);
|
|
69
55
|
}
|
|
70
56
|
throw new Error("Number of attempts exceeded");
|
|
71
57
|
}
|
|
@@ -76,7 +62,7 @@ export class ConnectionFactory {
|
|
|
76
62
|
}
|
|
77
63
|
|
|
78
64
|
function isTransientError(e: any) {
|
|
79
|
-
if (e.code === 'ECONNREFUSED') {
|
|
65
|
+
if (e.code === 'ECONNREFUSED' || e.code === '23505') { // 23505 is the code for unique_violation
|
|
80
66
|
return true;
|
|
81
67
|
}
|
|
82
68
|
return false;
|