@@ -47,16 +47,21 @@ impl Default for GcState {
47
47
}
48
48
}
49
49
50
- #[ derive( Debug , Clone ) ]
51
- pub struct Blobs < S > {
50
+ #[ derive( Debug ) ]
51
+ struct BlobsInner < S > {
52
52
rt : LocalPoolHandle ,
53
53
pub ( crate ) store : S ,
54
54
events : EventSender ,
55
55
downloader : Downloader ,
56
- #[ cfg( feature = "rpc" ) ]
57
- batches : Arc < tokio:: sync:: Mutex < BlobBatches > > ,
58
56
endpoint : Endpoint ,
59
- gc_state : Arc < std:: sync:: Mutex < GcState > > ,
57
+ gc_state : std:: sync:: Mutex < GcState > ,
58
+ #[ cfg( feature = "rpc" ) ]
59
+ batches : tokio:: sync:: Mutex < BlobBatches > ,
60
+ }
61
+
62
+ #[ derive( Debug , Clone ) ]
63
+ pub struct Blobs < S > {
64
+ inner : Arc < BlobsInner < S > > ,
60
65
#[ cfg( feature = "rpc" ) ]
61
66
pub ( crate ) rpc_handler : Arc < std:: sync:: OnceLock < crate :: rpc:: RpcHandler > > ,
62
67
}
@@ -178,40 +183,46 @@ impl<S: crate::store::Store> Blobs<S> {
178
183
endpoint : Endpoint ,
179
184
) -> Self {
180
185
Self {
181
- rt,
182
- store,
183
- events,
184
- downloader,
185
- endpoint,
186
- #[ cfg( feature = "rpc" ) ]
187
- batches : Default :: default ( ) ,
188
- gc_state : Default :: default ( ) ,
186
+ inner : Arc :: new ( BlobsInner {
187
+ rt,
188
+ store,
189
+ events,
190
+ downloader,
191
+ endpoint,
192
+ #[ cfg( feature = "rpc" ) ]
193
+ batches : Default :: default ( ) ,
194
+ gc_state : Default :: default ( ) ,
195
+ } ) ,
189
196
#[ cfg( feature = "rpc" ) ]
190
197
rpc_handler : Default :: default ( ) ,
191
198
}
192
199
}
193
200
194
201
pub fn store ( & self ) -> & S {
195
- & self . store
202
+ & self . inner . store
203
+ }
204
+
205
+ pub fn events ( & self ) -> & EventSender {
206
+ & self . inner . events
196
207
}
197
208
198
209
pub fn rt ( & self ) -> & LocalPoolHandle {
199
- & self . rt
210
+ & self . inner . rt
200
211
}
201
212
202
213
pub fn downloader ( & self ) -> & Downloader {
203
- & self . downloader
214
+ & self . inner . downloader
204
215
}
205
216
206
217
pub fn endpoint ( & self ) -> & Endpoint {
207
- & self . endpoint
218
+ & self . inner . endpoint
208
219
}
209
220
210
221
/// Add a callback that will be called before the garbage collector runs.
211
222
///
212
223
/// This can only be called before the garbage collector has started, otherwise it will return an error.
213
224
pub fn add_protected ( & self , cb : ProtectCb ) -> Result < ( ) > {
214
- let mut state = self . gc_state . lock ( ) . unwrap ( ) ;
225
+ let mut state = self . inner . gc_state . lock ( ) . unwrap ( ) ;
215
226
match & mut * state {
216
227
GcState :: Initial ( cbs) => {
217
228
cbs. push ( cb) ;
@@ -225,7 +236,7 @@ impl<S: crate::store::Store> Blobs<S> {
225
236
226
237
/// Start garbage collection with the given settings.
227
238
pub fn start_gc ( & self , config : GcConfig ) -> Result < ( ) > {
228
- let mut state = self . gc_state . lock ( ) . unwrap ( ) ;
239
+ let mut state = self . inner . gc_state . lock ( ) . unwrap ( ) ;
229
240
let protected = match state. deref_mut ( ) {
230
241
GcState :: Initial ( items) => std:: mem:: take ( items) ,
231
242
GcState :: Started ( _) => bail ! ( "gc already started" ) ,
@@ -241,17 +252,17 @@ impl<S: crate::store::Store> Blobs<S> {
241
252
set
242
253
}
243
254
} ;
244
- let store = self . store . clone ( ) ;
255
+ let store = self . store ( ) . clone ( ) ;
245
256
let run = self
246
- . rt
257
+ . rt ( )
247
258
. spawn ( move || async move { store. gc_run ( config, protected_cb) . await } ) ;
248
259
* state = GcState :: Started ( Some ( run) ) ;
249
260
Ok ( ( ) )
250
261
}
251
262
252
263
#[ cfg( feature = "rpc" ) ]
253
264
pub ( crate ) async fn batches ( & self ) -> tokio:: sync:: MutexGuard < ' _ , BlobBatches > {
254
- self . batches . lock ( ) . await
265
+ self . inner . batches . lock ( ) . await
255
266
}
256
267
257
268
pub ( crate ) async fn download (
@@ -268,7 +279,7 @@ impl<S: crate::store::Store> Blobs<S> {
268
279
mode,
269
280
} = req;
270
281
let hash_and_format = HashAndFormat { hash, format } ;
271
- let temp_tag = self . store . temp_tag ( hash_and_format) ;
282
+ let temp_tag = self . store ( ) . temp_tag ( hash_and_format) ;
272
283
let stats = match mode {
273
284
DownloadMode :: Queued => {
274
285
self . download_queued ( endpoint, hash_and_format, nodes, progress. clone ( ) )
@@ -283,10 +294,10 @@ impl<S: crate::store::Store> Blobs<S> {
283
294
progress. send ( DownloadProgress :: AllDone ( stats) ) . await . ok ( ) ;
284
295
match tag {
285
296
SetTagOption :: Named ( tag) => {
286
- self . store . set_tag ( tag, Some ( hash_and_format) ) . await ?;
297
+ self . store ( ) . set_tag ( tag, Some ( hash_and_format) ) . await ?;
287
298
}
288
299
SetTagOption :: Auto => {
289
- self . store . create_tag ( hash_and_format) . await ?;
300
+ self . store ( ) . create_tag ( hash_and_format) . await ?;
290
301
}
291
302
}
292
303
drop ( temp_tag) ;
@@ -316,7 +327,7 @@ impl<S: crate::store::Store> Blobs<S> {
316
327
let can_download = !node_ids. is_empty ( ) && ( any_added || endpoint. discovery ( ) . is_some ( ) ) ;
317
328
anyhow:: ensure!( can_download, "no way to reach a node for download" ) ;
318
329
let req = DownloadRequest :: new ( hash_and_format, node_ids) . progress_sender ( progress) ;
319
- let handle = self . downloader . queue ( req) . await ;
330
+ let handle = self . downloader ( ) . queue ( req) . await ;
320
331
let stats = handle. await ?;
321
332
Ok ( stats)
322
333
}
@@ -334,7 +345,7 @@ impl<S: crate::store::Store> Blobs<S> {
334
345
let mut nodes_iter = nodes. into_iter ( ) ;
335
346
' outer: loop {
336
347
match crate :: get:: db:: get_to_db_in_steps (
337
- self . store . clone ( ) ,
348
+ self . store ( ) . clone ( ) ,
338
349
hash_and_format,
339
350
progress. clone ( ) ,
340
351
)
@@ -393,9 +404,9 @@ impl<S: crate::store::Store> Blobs<S> {
393
404
394
405
impl < S : crate :: store:: Store > ProtocolHandler for Blobs < S > {
395
406
fn accept ( & self , conn : Connecting ) -> BoxedFuture < Result < ( ) > > {
396
- let db = self . store . clone ( ) ;
397
- let events = self . events . clone ( ) ;
398
- let rt = self . rt . clone ( ) ;
407
+ let db = self . store ( ) . clone ( ) ;
408
+ let events = self . events ( ) . clone ( ) ;
409
+ let rt = self . rt ( ) . clone ( ) ;
399
410
400
411
Box :: pin ( async move {
401
412
crate :: provider:: handle_connection ( conn. await ?, db, events, rt) . await ;
@@ -404,7 +415,7 @@ impl<S: crate::store::Store> ProtocolHandler for Blobs<S> {
404
415
}
405
416
406
417
fn shutdown ( & self ) -> BoxedFuture < ( ) > {
407
- let store = self . store . clone ( ) ;
418
+ let store = self . store ( ) . clone ( ) ;
408
419
Box :: pin ( async move {
409
420
store. shutdown ( ) . await ;
410
421
} )
0 commit comments