forked from sebastienhatton/riak-pb
/
client.js
185 lines (155 loc) · 3.88 KB
/
client.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
var EventEmitter = require('events').EventEmitter;
var assert = require('assert');
var extend = require('util')._extend;
var Options = require('./options');
var DumbClient = require('./dumb_client');
var log = require('./log')('client');
exports =
module.exports =
function RiakClient(options) {
options = Options(options);
var c = new EventEmitter();
var ending = false;
var expectMultiple;
var callback;
var stream;
var isDone = false;
c.busy = false;
c.queue = [];
var client = DumbClient(options);
client.on('readable', clientOnReadable);
client.on('error', clientOnError);
client.on('warning', clientOnWarning);
c.request =
function request(type, data, expectMultiple, callback, stream) {
var payload = {type: type};
if (data) payload.data = data;
var req = {
payload: payload,
expectMultiple: expectMultiple,
stream: stream,
callback: callback};
c.queue.push(req);
flush();
}
function flush() {
if (!c.busy) {
if (c.queue.length) {
actuallyDoRequest();
} else {
if (ending) {
// no more jobs in the queue
// and we're ending
// this is the time to say goodbye...
client.destroy();
process.nextTick(function() {
c.emit('close');
c.emit('end');
});
} else {
c.emit('drain');
}
}
}
}
function actuallyDoRequest() {
isDone = false;
c.busy = true;
var args = c.queue.shift();
expectMultiple = args.expectMultiple;
callback = args.callback;
var s = stream = args.stream;
if (s) {
var ended = false;
var resulted = false;
client.removeListener('readable', clientOnReadable);
// streaming
client.pipe(stream);
client.once('done', function() {
if (! ended) s.emit('end');
});
client.once('interrupted', clientInterrupted);
stream.once('end', function() {
if (! ended) {
ended = true;
finish(null, s.results);
}
});
stream.once('error', function(err) {
cleanup();
done(err);
});
stream.once('results', function(results) {
finish(null, results);
});
var cleanedup = false;
function cleanup() {
if (! cleanedup) {
cleanedup = true;
client.unpipe(stream);
client.removeListener('interrupted', clientInterrupted);
client.addListener('readable', clientOnReadable);
}
}
function finish(err, result) {
if (! resulted) {
cleanup();
resulted = true;
done(err, result);
}
}
function clientInterrupted(err) {
s.emit('error', err);
s.emit('end');
}
}
client.write(args);
}
function clientOnReadable() {
log('dumb client is readable');
if (! expectMultiple && c.busy) {
log('not expecting multiple and busy');
var response;
while (response = client.read()) {
log('read from the dumb client:', response);
if (! expectMultiple) done(null, response);
}
}
}
function clientOnError(err) {
if (c.busy) done(err);
else c.emit('error', err);
}
function clientOnWarning(warn) {
c.emit('warning', warn);
}
/// Done
function done(err, result) {
if (! isDone) {
log('done. setting busy to false');
isDone = true;
c.busy = false;
if (err) {
if (callback) callback(err);
else client.emit('error', err);
} else {
if (callback) {
log('calling back with result', result);
callback(null, result);
}
}
flush();
}
}
/// Disconnect
c.disconnect =
function disconnect() {
ending = true;
flush();
};
return c;
};
exports.createClient =
function createClient(options) {
return exports(options);
};