-
Notifications
You must be signed in to change notification settings - Fork 22
/
SharedResources.d
282 lines (200 loc) · 9.05 KB
/
SharedResources.d
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
/*******************************************************************************
Neo DHT client shared resources, available to all request handlers.
Copyright:
Copyright (c) 2017 sociomantic labs GmbH. All rights reserved.
License:
Boost Software License Version 1.0. See LICENSE.txt for details.
*******************************************************************************/
module dhtproto.client.internal.SharedResources;
import ocean.transition;
/// ditto
public final class SharedResources
{
import swarm.neo.util.AcquiredResources;
import dhtproto.client.internal.NodeHashRanges;
import swarm.neo.client.ConnectionSet;
import ocean.util.container.pool.FreeList;
import ocean.core.TypeConvert : downcast;
import ocean.core.Verify;
import ocean.io.compress.Lzo;
import swarm.neo.util.MessageFiber;
import swarm.neo.util.VoidBufferAsArrayOf;
import swarm.util.RecordBatcher;
/// Global NodeHashRanges instance
private NodeHashRanges node_hash_ranges_;
/// Free list of recycled buffers
private FreeList!(ubyte[]) buffers;
/// Free list of MessageFiber instances
private FreeList!(MessageFiber) fibers;
/// Free list of RecordBatch instances
private FreeList!(RecordBatch) record_batches;
/// Lzo instance shared by all record batches (newed on demand)
private Lzo lzo_;
/***************************************************************************
A SharedResources instance is stored in the ConnectionSet as an Object.
This helper function safely casts from this Object to a correctly-typed
instance.
Params:
obj = object to cast from
Returns:
obj cast to SharedResources
***************************************************************************/
public static SharedResources fromObject ( Object obj )
{
auto shared_resources = downcast!(SharedResources)(obj);
verify(shared_resources !is null);
return shared_resources;
}
/***************************************************************************
Constructor.
***************************************************************************/
this ( )
{
this.buffers = new FreeList!(ubyte[]);
this.fibers = new FreeList!(MessageFiber);
this.record_batches = new FreeList!(RecordBatch);
}
/***************************************************************************
Sets the shared NodeHashRanges instance. (This cannot be done in a ctor,
because the NodeHashRanges instance requires a ConnectionSet, which is
owned by the Neo object, which requires a SharedResources instance to be
passed to its ctor.)
Params:
node_hash_ranges = NodeHashRanges owned by the client
***************************************************************************/
public void setNodeHashRanges ( NodeHashRanges node_hash_ranges )
{
this.node_hash_ranges_ = node_hash_ranges;
}
/***************************************************************************
Returns:
shared NodeHashRanges instance
***************************************************************************/
public NodeHashRanges node_hash_ranges ( )
{
return this.node_hash_ranges_;
}
/***************************************************************************
Returns:
shared Lzo instance
***************************************************************************/
private Lzo lzo ( )
{
if ( this.lzo_ is null )
this.lzo_ = new Lzo;
return this.lzo_;
}
/***************************************************************************
Class to track the resources acquired by a request and relinquish them
(recylcing them into the shared resources pool) when the request
finishes. An instance should be newed as a request is started and
destroyed as it finishes. Newing an instance as `scope` is the most
convenient way.
***************************************************************************/
public class RequestResources
{
/// Set of acquired buffers
private AcquiredArraysOf!(void) acquired_buffers;
/// Set of acquired arrays of buffer slices
private AcquiredArraysOf!(void[]) acquired_buffer_lists;
/// Set of acquired buffers of NodeHashRange
private AcquiredArraysOf!(NodeHashRange) acquired_node_hash_range_buffers;
/// Set of acquired fibers
private Acquired!(MessageFiber) acquired_fibers;
/// Set of acquired record batches
private Acquired!(RecordBatch) acquired_record_batches;
/***********************************************************************
Constructor.
***********************************************************************/
this ( )
{
this.acquired_buffers.initialise(this.outer.buffers);
this.acquired_buffer_lists.initialise(this.outer.buffers);
this.acquired_node_hash_range_buffers.initialise(this.outer.buffers);
this.acquired_fibers.initialise(this.outer.buffers,
this.outer.fibers);
this.acquired_record_batches.initialise(this.outer.buffers,
this.outer.record_batches);
}
/***********************************************************************
Destructor. Relinquishes any acquired resources.
***********************************************************************/
~this ( )
{
this.acquired_buffers.relinquishAll();
this.acquired_buffer_lists.relinquishAll();
this.acquired_node_hash_range_buffers.relinquishAll();
this.acquired_fibers.relinquishAll();
this.acquired_record_batches.relinquishAll();
}
/***********************************************************************
Returns:
a shared LZO instance
***********************************************************************/
public Lzo lzo ( )
{
return this.outer.lzo;
}
/***********************************************************************
Returns:
a pointer to a new chunk of memory (a void[]) to use during the
request's lifetime
***********************************************************************/
public void[]* getVoidBuffer ( )
{
return this.acquired_buffers.acquire();
}
/***********************************************************************
Returns:
a void[] wrapped as an array of void[] (slices). The individual
slices that are added to the array should be acquired using
getVoidBuffer().
***********************************************************************/
public VoidBufferAsArrayOf!(void[]) getBufferList ( )
{
return this.acquired_buffer_lists.acquireWrapped();
}
/***********************************************************************
Returns:
a new NodeHashRange buffer acquired from the shared resources
pools
***********************************************************************/
public VoidBufferAsArrayOf!(NodeHashRange) getNodeHashRangeBuffer ( )
{
return this.acquired_node_hash_range_buffers.acquireWrapped();
}
/***********************************************************************
Gets a fiber from the shared resources pool and assigns the provided
delegate as its entry point.
Params:
fiber_method = entry point to assign to acquired fiber
Returns:
a new MessageFiber acquired from the shared resources pools
***********************************************************************/
public MessageFiber getFiber ( scope void delegate ( ) fiber_method )
{
bool new_fiber = false;
MessageFiber newFiber ( )
{
new_fiber = true;
return new MessageFiber(fiber_method, 64 * 1024);
}
auto fiber = this.acquired_fibers.acquire(newFiber());
if (!new_fiber)
fiber.reset(fiber_method);
return fiber;
}
/***********************************************************************
Gets a record batch from the shared resources pool.
Returns:
a new record batch acquired from the shared resources pools
***********************************************************************/
public RecordBatch getRecordBatch ( )
{
auto batch = this.acquired_record_batches.acquire(
new RecordBatch(this.outer.lzo()));
batch.clear();
return batch;
}
}
}