forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_retrieval_worker.go
90 lines (79 loc) · 2.09 KB
/
block_retrieval_worker.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
// Copyright 2016 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 (
"io"
)
// blockRetrievalWorker processes blockRetrievalQueue requests
type blockRetrievalWorker struct {
blockGetter
stopCh chan struct{}
queue *blockRetrievalQueue
workCh <-chan struct{}
}
// run runs the worker loop until Shutdown is called
func (brw *blockRetrievalWorker) run() {
for {
err := brw.HandleRequest()
// Only io.EOF is relevant to the loop; other errors are handled in
// FinalizeRequest
if err == io.EOF {
return
}
}
}
// newBlockRetrievalWorker returns a blockRetrievalWorker for a given
// blockRetrievalQueue, using the passed in blockGetter to obtain blocks for
// requests.
func newBlockRetrievalWorker(bg blockGetter, q *blockRetrievalQueue,
workCh <-chan struct{}) *blockRetrievalWorker {
brw := &blockRetrievalWorker{
blockGetter: bg,
stopCh: make(chan struct{}),
queue: q,
workCh: workCh,
}
go brw.run()
return brw
}
// HandleRequest is the main work method for the worker. It obtains a
// blockRetrieval from the queue, retrieves the block using
// blockGetter.getBlock, and responds to the subscribed requestors with the
// results.
func (brw *blockRetrievalWorker) HandleRequest() (err error) {
var retrieval *blockRetrieval
select {
case <-brw.workCh:
retrieval = brw.queue.popIfNotEmpty()
if retrieval == nil {
return nil
}
case <-brw.stopCh:
return io.EOF
}
var block Block
defer func() {
brw.queue.FinalizeRequest(retrieval, block, err)
}()
// Handle canceled contexts.
select {
case <-retrieval.ctx.Done():
return retrieval.ctx.Err()
default:
}
func() {
retrieval.reqMtx.RLock()
defer retrieval.reqMtx.RUnlock()
block = retrieval.requests[0].block.NewEmpty()
}()
return brw.getBlock(retrieval.ctx, retrieval.kmd, retrieval.blockPtr, block)
}
// Shutdown shuts down the blockRetrievalWorker once its current work is done.
func (brw *blockRetrievalWorker) Shutdown() {
select {
case <-brw.stopCh:
default:
close(brw.stopCh)
}
}