-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmemcached.ts
282 lines (239 loc) · 5.98 KB
/
memcached.ts
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
import net from 'net';
import { parseMemCommand } from './command';
interface IMemCachedServer {
/**
* Port on which the server will start listening to connections.
*
* @type {number}
*/
port: number;
/**
* Host on which the server will start listening to connections.
*
* @type {string}
*/
host: string;
/**
* Status of the server when starts listening to connections.
*
* @type {('listening' | 'closed' | 'error')}
*/
status: 'listening' | 'closed' | 'error';
/**
* Function to start the server.
*
* @returns {Promise<void>}
*/
startServer(): Promise<void>;
/**
* Function to stop the server.
*
* @returns {Promise<void>}
*/
stopServer(): Promise<void>;
/**
* Return the total number of client connected to the server.
*
* @returns {number}
*/
getConnectedClientsCount(): number;
}
/**
* The data stored in the Memcached server storage.
*
* @interface MemData
*/
interface MemData {
/**
* The key of the entry.
*
* @type {string}
*/
key: string;
/**
* The value of the entry.
*
* @type {string}
*/
value: string;
/**
* Flags passed by the user
*
* @type {number}
*/
flags: number;
/**
* Total number of bytes corresponding to value
*
* @type {number}
*/
byteCount: number;
/**
* Datetime milliseconds since EPOCH.
*
* @type {number}
*/
addedAt: number;
/**
* The expTime in milliseconds
*
* @type {number}
*/
expTime: number;
}
export default class MemCachedServer implements IMemCachedServer {
port;
host;
status: 'listening' | 'closed' | 'error';
private server: net.Server;
private storage: Map<string, MemData>;
private clients: Map<string, net.Socket>;
constructor(port: number = 11211, host: string = '127.0.0.1') {
this.port = port;
this.host = host;
this.server = new net.Server();
this.storage = new Map<string, MemData>();
this.status = 'closed';
this.clients = new Map<string, net.Socket>();
}
getConnectedClientsCount(): number {
return this.clients.size;
}
startServer(): Promise<void> {
return new Promise<void>((res, rej) => {
this.server.listen(this.port, this.host, () => {
this.status = 'listening';
res();
console.log('Started listening to connection on port ' + this.port);
});
this.server.on('connection', (socket) => {
const key = `${socket.remoteAddress}:${socket.remotePort}`;
console.log(`Client connected ${key}`);
// add client to the map
this.clients.set(key, socket);
socket.on('data', (data) => this.handleDataFromClient(socket, data));
socket.on('close', () => {
// Remove client from the map
this.clients.delete(key);
});
});
this.server.on('close', () => {
this.status = 'closed';
});
this.server.on('error', () => {
this.status = 'error';
rej();
});
});
}
stopServer(): Promise<void> {
return new Promise<void>((res) => {
this.server.close();
this.server.on('close', () => {
res();
});
});
}
private handleDataFromClient(socket: net.Socket, data: Buffer) {
const str = data.toString();
const arr = str.split('\r\n');
for (let i = 0; i < arr.length; i++) {
const command = parseMemCommand(arr[i]);
const name = command.name;
if (name === 'set' || name === 'add' || name === 'replace') {
// We need to read the data line by line,
// since it can be distributed between multiple lines.
let bytes = command.byteCount!;
let finalValue = '';
while (bytes > 0) {
// Go to next line
i++;
// Read the data, CRLF is already removed
finalValue += arr[i];
// Decrease the total number of bytes left to read
bytes -= finalValue.length;
}
this.handleSetAddReplaceCommand(
socket,
name,
command.key,
finalValue,
command.flags!,
command.byteCount!,
command.expTime!,
command.noReply
);
} else if (name === 'get') {
this.handleGetCommand(socket, command.key);
}
}
}
private handleSetAddReplaceCommand(
socket: net.Socket,
name: 'add' | 'set' | 'replace',
key: string,
value: string,
flags: number,
byteCount: number,
expTime: number,
noreply: boolean = false
) {
// add command: when storage already has a data with the given key
if (name === 'add' && this.storage.has(key)) {
if (noreply) {
return;
}
socket.write('NOT_STORED\r\n');
return;
}
// replace command: when storage doesn't have a data with the given key
else if (name === 'replace' && !this.storage.has(key)) {
if (noreply) {
return;
}
socket.write('NOT_STORED\r\n');
return;
}
if (expTime >= 0) {
this.storage.set(key, {
key,
value,
flags,
byteCount,
addedAt: new Date().getTime(),
expTime: expTime * 1000
});
}
if (noreply) {
return;
}
socket.write('STORED\r\n');
}
private handleGetCommand(socket: net.Socket, key: string) {
const obj = this.storage.get(key);
// Object is not present
if (obj === undefined) {
socket.write('END\r\n');
return;
}
// Object is present and expTime is zero
if (obj.expTime === 0) {
socket.write(
`VALUE ${key} ${obj.flags} ${obj.byteCount}\r\n${obj.value}\r\nEND\r\n`
);
}
// Object is present and expTime > 0
const diff = new Date().getTime() - obj.addedAt;
// Object is expired
if (diff >= obj.expTime) {
socket.write('END\r\n');
// Lazy delete
this.storage.delete(key);
return;
}
// Object is not expired
socket.write(
`VALUE ${key} ${obj.flags} ${obj.byteCount}\r\n${obj.value}\r\nEND\r\n`
);
}
}