rads-db 3.0.76 → 3.0.78
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.
- package/drivers/azureCosmos.cjs +27 -5
- package/drivers/azureCosmos.mjs +31 -10
- package/package.json +1 -1
package/drivers/azureCosmos.cjs
CHANGED
|
@@ -124,14 +124,36 @@ var _default = options => (schema, entity) => {
|
|
|
124
124
|
...item
|
|
125
125
|
});
|
|
126
126
|
}
|
|
127
|
-
for (const chunk of _lodash.default.chunk(itemsToPut,
|
|
128
|
-
|
|
127
|
+
for (const chunk of _lodash.default.chunk(itemsToPut, 100)) {
|
|
128
|
+
let chunkToSend = chunk.map(x => ({
|
|
129
129
|
operationType: "Upsert",
|
|
130
130
|
resourceBody: x
|
|
131
|
-
}))
|
|
131
|
+
}));
|
|
132
|
+
const responses = [];
|
|
133
|
+
for (let i = 0; i < 20; i++) {
|
|
134
|
+
const response = await client.items.bulk(chunkToSend);
|
|
135
|
+
const newChunkToSend = [];
|
|
136
|
+
for (let i2 = 0; i2 < chunkToSend.length; i2++) {
|
|
137
|
+
if (response[i2].statusCode !== 429) {
|
|
138
|
+
responses.push(response[i2]);
|
|
139
|
+
} else {
|
|
140
|
+
newChunkToSend.push(chunkToSend[i2]);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
chunkToSend = newChunkToSend;
|
|
144
|
+
if (chunkToSend.length === 0) break;
|
|
145
|
+
const delay = 50 + i * 100;
|
|
146
|
+
console.warn(`Db overloaded. Retrying after ${delay}ms...`);
|
|
147
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
148
|
+
}
|
|
149
|
+
if (chunkToSend.length !== 0) {
|
|
150
|
+
throw new _cosmos.RestError("Db oveloaded. Too many retries. Consider increasing database RU setting.", {
|
|
151
|
+
code: "429"
|
|
152
|
+
});
|
|
153
|
+
}
|
|
132
154
|
ctx?.log?.({
|
|
133
|
-
charge: _lodash.default.sumBy(
|
|
134
|
-
request: `put#${
|
|
155
|
+
charge: _lodash.default.sumBy(responses, r => r.requestCharge),
|
|
156
|
+
request: `put#${chunk[0]._partition}[${chunk.length}]`
|
|
135
157
|
});
|
|
136
158
|
}
|
|
137
159
|
}
|
package/drivers/azureCosmos.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CosmosClient } from "@azure/cosmos";
|
|
1
|
+
import { CosmosClient, RestError } from "@azure/cosmos";
|
|
2
2
|
import _ from "lodash";
|
|
3
3
|
const cosmosClientCache = {};
|
|
4
4
|
const databaseClientCache = {};
|
|
@@ -80,16 +80,37 @@ export default (options) => (schema, entity) => {
|
|
|
80
80
|
throw new Error(`You must provide an id`);
|
|
81
81
|
itemsToPut.push({ _partition: entity, id, ...item });
|
|
82
82
|
}
|
|
83
|
-
for (const chunk of _.chunk(itemsToPut,
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
)
|
|
83
|
+
for (const chunk of _.chunk(itemsToPut, 100)) {
|
|
84
|
+
let chunkToSend = chunk.map((x) => ({
|
|
85
|
+
operationType: "Upsert",
|
|
86
|
+
resourceBody: x
|
|
87
|
+
}));
|
|
88
|
+
const responses = [];
|
|
89
|
+
for (let i = 0; i < 20; i++) {
|
|
90
|
+
const response = await client.items.bulk(chunkToSend);
|
|
91
|
+
const newChunkToSend = [];
|
|
92
|
+
for (let i2 = 0; i2 < chunkToSend.length; i2++) {
|
|
93
|
+
if (response[i2].statusCode !== 429) {
|
|
94
|
+
responses.push(response[i2]);
|
|
95
|
+
} else {
|
|
96
|
+
newChunkToSend.push(chunkToSend[i2]);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
chunkToSend = newChunkToSend;
|
|
100
|
+
if (chunkToSend.length === 0)
|
|
101
|
+
break;
|
|
102
|
+
const delay = 50 + i * 100;
|
|
103
|
+
console.warn(`Db overloaded. Retrying after ${delay}ms...`);
|
|
104
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
105
|
+
}
|
|
106
|
+
if (chunkToSend.length !== 0) {
|
|
107
|
+
throw new RestError("Db oveloaded. Too many retries. Consider increasing database RU setting.", {
|
|
108
|
+
code: "429"
|
|
109
|
+
});
|
|
110
|
+
}
|
|
90
111
|
ctx?.log?.({
|
|
91
|
-
charge: _.sumBy(
|
|
92
|
-
request: `put#${
|
|
112
|
+
charge: _.sumBy(responses, (r) => r.requestCharge),
|
|
113
|
+
request: `put#${chunk[0]._partition}[${chunk.length}]`
|
|
93
114
|
});
|
|
94
115
|
}
|
|
95
116
|
}
|