forked from datastax/nodejs-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriters.js
310 lines (277 loc) · 8.01 KB
/
writers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const events = require('events');
const types = require('./types');
const utils = require('./utils.js');
const FrameHeader = types.FrameHeader;
/**
* Contains the logic to write all the different types to the frame.
*/
class FrameWriter {
/**
* Creates a new instance of FrameWriter.
* @param {Number} opcode
*/
constructor(opcode) {
if (!opcode) {
throw new Error('Opcode not provided');
}
this.buffers = [];
this.opcode = opcode;
this.bodyLength = 0;
}
add(buf) {
this.buffers.push(buf);
this.bodyLength += buf.length;
}
writeShort(num) {
const buf = utils.allocBufferUnsafe(2);
buf.writeUInt16BE(num, 0);
this.add(buf);
}
writeInt(num) {
const buf = utils.allocBufferUnsafe(4);
buf.writeInt32BE(num, 0);
this.add(buf);
}
/** @param {Long} num */
writeLong(num) {
this.add(types.Long.toBuffer(num));
}
/**
* Writes bytes according to Cassandra <int byteLength><bytes>
* @param {Buffer|null|types.unset} bytes
*/
writeBytes(bytes) {
if (bytes === null) {
//Only the length buffer containing -1
this.writeInt(-1);
return;
}
if (bytes === types.unset) {
this.writeInt(-2);
return;
}
//Add the length buffer
this.writeInt(bytes.length);
//Add the actual buffer
this.add(bytes);
}
/**
* Writes a buffer according to Cassandra protocol: bytes.length (2) + bytes
* @param {Buffer} bytes
*/
writeShortBytes(bytes) {
if(bytes === null) {
//Only the length buffer containing -1
this.writeShort(-1);
return;
}
//Add the length buffer
this.writeShort(bytes.length);
//Add the actual buffer
this.add(bytes);
}
/**
* Writes a single byte
* @param {Number} num Value of the byte, a number between 0 and 255.
*/
writeByte(num) {
this.add(utils.allocBufferFromArray([num]));
}
writeString(str) {
if (typeof str === "undefined") {
throw new Error("can not write undefined");
}
const len = Buffer.byteLength(str, 'utf8');
const buf = utils.allocBufferUnsafe(2 + len);
buf.writeUInt16BE(len, 0);
buf.write(str, 2, buf.length-2, 'utf8');
this.add(buf);
}
writeLString(str) {
const len = Buffer.byteLength(str, 'utf8');
const buf = utils.allocBufferUnsafe(4 + len);
buf.writeInt32BE(len, 0);
buf.write(str, 4, buf.length-4, 'utf8');
this.add(buf);
}
writeStringList(values) {
this.writeShort(values.length);
values.forEach(this.writeString, this);
}
writeCustomPayload(payload) {
const keys = Object.keys(payload);
this.writeShort(keys.length);
keys.forEach(k => {
this.writeString(k);
this.writeBytes(payload[k]);
});
}
writeStringMap(map) {
const keys = [];
for (const k in map) {
if (map.hasOwnProperty(k)) {
keys.push(k);
}
}
this.writeShort(keys.length);
for(let i = 0; i < keys.length; i++) {
const key = keys[i];
this.writeString(key);
this.writeString(map[key]);
}
}
/**
* @param {Number} version
* @param {Number} streamId
* @param {Number} [flags] Header flags
* @returns {Buffer}
* @throws {TypeError}
*/
write(version, streamId, flags) {
const header = new FrameHeader(version, flags || 0, streamId, this.opcode, this.bodyLength);
const headerBuffer = header.toBuffer();
this.buffers.unshift(headerBuffer);
return Buffer.concat(this.buffers, headerBuffer.length + this.bodyLength);
}
}
/**
* Represents a queue that process one write at a time (FIFO).
* @extends {EventEmitter}
*/
class WriteQueue extends events.EventEmitter {
/**
* Creates a new WriteQueue instance.
* @param {Socket} netClient
* @param {Encoder} encoder
* @param {ClientOptions} options
*/
constructor(netClient, encoder, options) {
super();
this.netClient = netClient;
this.encoder = encoder;
this.isRunning = false;
/** @type {Array<{operation: OperationState, callback: Function}>} */
this.queue = [];
this.coalescingThreshold = options.socketOptions.coalescingThreshold;
this.error = null;
this.canWrite = true;
// Listen to drain event that is going to be fired once
// the underlying buffer is empty
netClient.on('drain', () => {
this.canWrite = true;
this.run();
});
}
/**
* Enqueues a new request
* @param {OperationState} operation
* @param {Function} callback The write callback.
*/
push(operation, callback) {
const self = this;
if (this.error) {
// There was a write error, there is no point in further trying to write to the socket.
return process.nextTick(function writePushError() {
callback(self.error);
});
}
this.queue.push({ operation: operation, callback: callback});
this.run();
}
run() {
if (!this.isRunning && this.canWrite) {
this.isRunning = true;
// Use nextTick to allow the queue to build up on the current phase
process.nextTick(() => this.process());
}
}
process() {
if (this.error) {
return;
}
const buffers = [];
const callbacks = [];
let totalLength = 0;
while (this.queue.length > 0 && totalLength < this.coalescingThreshold) {
const writeItem = this.queue.shift();
if (!writeItem.operation.canBeWritten()) {
// Invoke the write callback with an error that is not going to be yielded to user
// as the operation has timed out or was cancelled.
writeItem.callback(new Error('The operation was already cancelled or timeout elapsed'));
continue;
}
let data;
try {
data = writeItem.operation.request.write(this.encoder, writeItem.operation.streamId);
}
catch (err) {
writeItem.callback(err);
continue;
}
totalLength += data.length;
buffers.push(data);
callbacks.push(writeItem.callback);
}
if (totalLength === 0) {
this.isRunning = false;
return;
}
// We have to invoke the callbacks to avoid race conditions.
// There is a performance benefit from executing all of them in a loop
for (let i = 0; i < callbacks.length; i++) {
callbacks[i]();
}
// Concatenate buffers and write it to the socket
// Further writes will be throttled until flushed
this.canWrite = this.netClient.write(Buffer.concat(buffers, totalLength), err => {
if (err) {
this.setWriteError(err);
return;
}
if (this.queue.length === 0 || !this.canWrite) {
// It will start running once we get the next message or has drained
this.isRunning = false;
return;
}
// Allow IO between writes
setImmediate(() => this.process());
});
}
/**
* Emits the 'error' event and callbacks items that haven't been written and clears them from the queue.
* @param err
*/
setWriteError(err) {
err.isSocketError = true;
this.error = new types.DriverError('Socket was closed');
this.error.isSocketError = true;
// Use an special flag for items that haven't been written
this.error.requestNotWritten = true;
this.error.innerError = err;
const q = this.queue;
// Not more items can be added to the queue.
this.queue = utils.emptyArray;
for (let i = 0; i < q.length; i++) {
const item = q[i];
// Use the error marking that it was not written
item.callback(this.error);
}
}
}
module.exports = { FrameWriter, WriteQueue };