forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modes.go
512 lines (393 loc) · 10.3 KB
/
modes.go
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// Copyright 2018 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"fmt"
"math"
"time"
"github.com/keybase/client/go/protocol/keybase1"
)
// NewInitModeFromType returns an InitMode object corresponding to the
// given type.
func NewInitModeFromType(t InitModeType) InitMode {
switch t {
case InitDefault:
return modeDefault{}
case InitMinimal:
return modeMinimal{}
case InitSingleOp:
return modeSingleOp{modeDefault{}}
case InitConstrained:
return modeConstrained{modeDefault{}}
case InitMemoryLimited:
return modeMemoryLimited{modeConstrained{modeDefault{}}}
default:
panic(fmt.Sprintf("Unknown mode: %s", t))
}
}
const (
defaultQRPeriod = 1 * time.Hour
defaultQRMinUnrefAge = 2 * 7 * 24 * time.Hour // 2 weeks
)
// Default mode:
type modeDefault struct {
}
func (md modeDefault) Type() InitModeType {
return InitDefault
}
func (md modeDefault) BlockWorkers() int {
return defaultBlockRetrievalWorkerQueueSize
}
func (md modeDefault) PrefetchWorkers() int {
return defaultPrefetchWorkerQueueSize
}
func (md modeDefault) ThrottledPrefetchPeriod() time.Duration {
return defaultThrottledPrefetchPeriod
}
func (md modeDefault) DefaultBlockRequestAction() BlockRequestAction {
return BlockRequestWithPrefetch
}
func (md modeDefault) RekeyWorkers() int {
return 16
}
func (md modeDefault) RekeyQueueSize() int {
return 2048 // 48 KB
}
func (md modeDefault) IsTestMode() bool {
return false
}
func (md modeDefault) DirtyBlockCacheEnabled() bool {
return true
}
func (md modeDefault) BackgroundFlushesEnabled() bool {
return true
}
func (md modeDefault) MetricsEnabled() bool {
return true
}
func (md modeDefault) ConflictResolutionEnabled() bool {
return true
}
func (md modeDefault) BlockManagementEnabled() bool {
return true
}
func (md modeDefault) QuotaReclamationEnabled() bool {
return true
}
func (md modeDefault) QuotaReclamationPeriod() time.Duration {
return defaultQRPeriod
}
func (md modeDefault) QuotaReclamationMinUnrefAge() time.Duration {
return defaultQRMinUnrefAge
}
func (md modeDefault) QuotaReclamationMinHeadAge() time.Duration {
// How old must the most recent TLF revision be before another
// device can run QR on that TLF? This is large, to avoid
// unnecessary conflicts on the TLF between devices.
return defaultQRMinUnrefAge + 24*time.Hour
}
func (md modeDefault) NodeCacheEnabled() bool {
return true
}
func (md modeDefault) TLFUpdatesEnabled() bool {
return true
}
func (md modeDefault) KBFSServiceEnabled() bool {
return true
}
func (md modeDefault) JournalEnabled() bool {
return true
}
func (md modeDefault) UnmergedTLFsEnabled() bool {
return true
}
func (md modeDefault) ServiceKeepaliveEnabled() bool {
return true
}
func (md modeDefault) TLFEditHistoryEnabled() bool {
return true
}
func (md modeDefault) SendEditNotificationsEnabled() bool {
return true
}
func (md modeDefault) ClientType() keybase1.ClientType {
return keybase1.ClientType_KBFS
}
func (md modeDefault) LocalHTTPServerEnabled() bool {
return true
}
func (md modeDefault) MaxCleanBlockCacheCapacity() uint64 {
return math.MaxUint64
}
// Minimal mode:
type modeMinimal struct {
}
func (mm modeMinimal) Type() InitModeType {
return InitMinimal
}
func (mm modeMinimal) BlockWorkers() int {
// In minimal mode, block re-embedding is not required, so we
// don't fetch the unembedded blocks..
return 0
}
func (mm modeMinimal) PrefetchWorkers() int {
return 0
}
func (mm modeMinimal) ThrottledPrefetchPeriod() time.Duration {
return 0
}
func (mm modeMinimal) DefaultBlockRequestAction() BlockRequestAction {
return BlockRequestSolo
}
func (mm modeMinimal) RekeyWorkers() int {
return 4
}
func (mm modeMinimal) RekeyQueueSize() int {
return 512 // 12 KB
}
func (mm modeMinimal) IsTestMode() bool {
return false
}
func (mm modeMinimal) DirtyBlockCacheEnabled() bool {
// No blocks will be dirtied in minimal mode, so don't bother with
// the dirty block cache.
return false
}
func (mm modeMinimal) BackgroundFlushesEnabled() bool {
// Don't do background flushes when in minimal mode, since there
// shouldn't be any data writes.
return false
}
func (mm modeMinimal) MetricsEnabled() bool {
return false
}
func (mm modeMinimal) ConflictResolutionEnabled() bool {
// No need to run CR if there won't be any data writes on this
// device. (There may still be rekey writes, but we don't allow
// conflicts to happen in that case.)
return false
}
func (mm modeMinimal) BlockManagementEnabled() bool {
// If this device is in minimal mode and won't be doing any data
// writes, no need deal with block-level cleanup operations.
// TODO: in the future it might still be useful to have
// e.g. mobile devices doing QR.
return false
}
func (mm modeMinimal) QuotaReclamationEnabled() bool {
return false
}
func (mm modeMinimal) QuotaReclamationPeriod() time.Duration {
return 0
}
func (mm modeMinimal) QuotaReclamationMinUnrefAge() time.Duration {
return 0
}
func (mm modeMinimal) QuotaReclamationMinHeadAge() time.Duration {
return 0
}
func (mm modeMinimal) NodeCacheEnabled() bool {
// If we're in minimal mode, let the node cache remain nil to
// ensure that the user doesn't try any data reads or writes.
return false
}
func (mm modeMinimal) TLFUpdatesEnabled() bool {
return true
}
func (mm modeMinimal) KBFSServiceEnabled() bool {
return false
}
func (mm modeMinimal) JournalEnabled() bool {
return false
}
func (mm modeMinimal) UnmergedTLFsEnabled() bool {
// Writes aren't allowed, so unmerged TLFs on this device
// shouldn't be possible.
return false
}
func (mm modeMinimal) ServiceKeepaliveEnabled() bool {
return false
}
func (mm modeMinimal) TLFEditHistoryEnabled() bool {
return false
}
func (mm modeMinimal) SendEditNotificationsEnabled() bool {
// Writes aren't allowed, so we shouldn't need to send any.
return false
}
func (mm modeMinimal) ClientType() keybase1.ClientType {
return keybase1.ClientType_KBFS
}
func (mm modeMinimal) LocalHTTPServerEnabled() bool {
return false
}
func (mm modeMinimal) MaxCleanBlockCacheCapacity() uint64 {
return math.MaxUint64
}
// Single op mode:
type modeSingleOp struct {
InitMode
}
func (mso modeSingleOp) Type() InitModeType {
return InitSingleOp
}
func (mso modeSingleOp) RekeyWorkers() int {
// Just block all rekeys and don't bother cleaning up requests
// since the process is short lived anyway.
return 0
}
func (mso modeSingleOp) RekeyQueueSize() int {
return 0
}
func (mso modeSingleOp) QuotaReclamationEnabled() bool {
return false
}
func (mso modeSingleOp) QuotaReclamationPeriod() time.Duration {
return 0
}
func (mso modeSingleOp) QuotaReclamationMinUnrefAge() time.Duration {
return 0
}
func (mso modeSingleOp) QuotaReclamationMinHeadAge() time.Duration {
return 0
}
func (mso modeSingleOp) TLFUpdatesEnabled() bool {
return false
}
func (mso modeSingleOp) KBFSServiceEnabled() bool {
return false
}
func (mso modeSingleOp) UnmergedTLFsEnabled() bool {
// There's basically no way for a TLF to start off as unmerged
// since single-ops should be using a fresh journal.
return false
}
func (mso modeSingleOp) TLFEditHistoryEnabled() bool {
return false
}
func (mso modeSingleOp) SendEditNotificationsEnabled() bool {
// We don't want git, or other single op writes, showing up in the
// notification history.
return false
}
func (mso modeSingleOp) ClientType() keybase1.ClientType {
return keybase1.ClientType_NONE
}
func (mso modeSingleOp) LocalHTTPServerEnabled() bool {
return false
}
// Constrained mode:
type modeConstrained struct {
InitMode
}
func (mc modeConstrained) Type() InitModeType {
return InitConstrained
}
func (mc modeConstrained) BlockWorkers() int {
return 1
}
func (mc modeConstrained) PrefetchWorkers() int {
return 1
}
func (mc modeConstrained) DefaultBlockRequestAction() BlockRequestAction {
return BlockRequestSolo
}
func (mc modeConstrained) RekeyWorkers() int {
return 4
}
func (mc modeConstrained) RekeyQueueSize() int {
return 1024 // 24 KB
}
func (mc modeConstrained) BackgroundFlushesEnabled() bool {
return true
}
func (mc modeConstrained) ConflictResolutionEnabled() bool {
return true
}
func (mc modeConstrained) QuotaReclamationEnabled() bool {
return true
}
func (mc modeConstrained) QuotaReclamationPeriod() time.Duration {
return defaultQRPeriod
}
func (mc modeConstrained) QuotaReclamationMinUnrefAge() time.Duration {
return defaultQRMinUnrefAge
}
func (mc modeConstrained) QuotaReclamationMinHeadAge() time.Duration {
// Don't ever run QR in constrained mode unless this device was
// the most recent writer.
return 0
}
func (mc modeConstrained) KBFSServiceEnabled() bool {
return false
}
func (mc modeConstrained) JournalEnabled() bool {
return true
}
func (mc modeConstrained) UnmergedTLFsEnabled() bool {
return true
}
func (mc modeConstrained) ServiceKeepaliveEnabled() bool {
return false
}
func (mc modeConstrained) TLFEditHistoryEnabled() bool {
return false
}
func (mc modeConstrained) SendEditNotificationsEnabled() bool {
return true
}
func (mc modeConstrained) LocalHTTPServerEnabled() bool {
return true
}
// Memory limited mode
type modeMemoryLimited struct {
InitMode
}
func (mml modeMemoryLimited) Type() InitModeType {
return InitMemoryLimited
}
func (mml modeMemoryLimited) RekeyWorkers() int {
return 0
}
func (mml modeMemoryLimited) RekeyQueueSize() int {
return 0
}
func (mml modeMemoryLimited) ConflictResolutionEnabled() bool {
return false
}
func (mml modeMemoryLimited) QuotaReclamationEnabled() bool {
return false
}
func (mml modeMemoryLimited) UnmergedTLFsEnabled() bool {
return false
}
func (mml modeMemoryLimited) SendEditNotificationsEnabled() bool {
return false
}
func (mml modeMemoryLimited) LocalHTTPServerEnabled() bool {
return false
}
func (mml modeMemoryLimited) MaxCleanBlockCacheCapacity() uint64 {
return 1 * (1 << 20) // 1 MB
}
// Wrapper for tests.
type modeTest struct {
InitMode
}
func (mt modeTest) IsTestMode() bool {
return true
}
func (mt modeTest) QuotaReclamationPeriod() time.Duration {
// No auto-reclamation during testing.
return 0
}
func (mt modeTest) QuotaReclamationMinUnrefAge() time.Duration {
// Smaller archival window by default during testing, for
// backwards compatibility with old tests.
return 1 * time.Minute
}
func (mt modeTest) QuotaReclamationMinHeadAge() time.Duration {
// No min head age during testing.
return 0
}