forked from keybase/client
/
convloader.go
176 lines (144 loc) · 4.17 KB
/
convloader.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
package chat
import (
"errors"
"sync"
"time"
"golang.org/x/net/context"
"github.com/keybase/client/go/chat/globals"
"github.com/keybase/client/go/chat/types"
"github.com/keybase/client/go/chat/utils"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/client/go/protocol/gregor1"
"github.com/keybase/client/go/protocol/keybase1"
)
const (
bgLoaderMaxAttempts = 4
bgLoaderInitDelay = 100 * time.Millisecond
bgLoaderErrDelay = 300 * time.Millisecond
)
type clTask struct {
convID chat1.ConversationID
attempt int
lastAttemptAt time.Time
}
type BackgroundConvLoader struct {
globals.Contextified
utils.DebugLabeler
sync.Mutex
started bool
queue chan clTask
stop chan bool
identNotifier *IdentifyNotifier
// for testing, make this and can check conv load successes
loads chan chat1.ConversationID
testingNameInfoSource types.NameInfoSource
}
var _ types.ConvLoader = (*BackgroundConvLoader)(nil)
func NewBackgroundConvLoader(g *globals.Context) *BackgroundConvLoader {
b := &BackgroundConvLoader{
Contextified: globals.NewContextified(g),
DebugLabeler: utils.NewDebugLabeler(g.GetLog(), "BackgroundConvLoader", false),
stop: make(chan bool),
identNotifier: NewIdentifyNotifier(g),
}
b.newQueue()
return b
}
func (b *BackgroundConvLoader) Start(ctx context.Context, uid gregor1.UID) {
b.Lock()
defer b.Unlock()
if b.started {
b.stop <- true
}
b.newQueue()
b.started = true
go b.loop(uid)
}
func (b *BackgroundConvLoader) Stop(ctx context.Context) chan struct{} {
b.Lock()
defer b.Unlock()
if b.started {
b.stop <- true
b.started = false
}
ch := make(chan struct{})
close(ch)
return ch
}
func (b *BackgroundConvLoader) Queue(ctx context.Context, convID chat1.ConversationID) error {
return b.enqueue(ctx, clTask{convID: convID})
}
func (b *BackgroundConvLoader) enqueue(ctx context.Context, task clTask) error {
b.Lock()
defer b.Unlock()
select {
case b.queue <- task:
b.Debug(ctx, "added %s to queue", task.convID)
default:
b.Debug(ctx, "queue is full, not adding %s", task.convID)
return errors.New("queue is full")
}
return nil
}
func (b *BackgroundConvLoader) setTestingNameInfoSource(ni types.NameInfoSource) {
b.testingNameInfoSource = ni
}
func (b *BackgroundConvLoader) loop(uid gregor1.UID) {
bgctx := context.Background()
b.Debug(bgctx, "starting conv loader loop for %s", uid)
for {
// get a convID from queue, or stop
select {
case task := <-b.queue:
// always wait a short amount of time to avoid
// flood of conversation loads
duration := bgLoaderInitDelay
if task.attempt > 0 {
duration = bgLoaderErrDelay - time.Since(task.lastAttemptAt)
if duration < bgLoaderInitDelay {
duration = bgLoaderInitDelay
}
}
time.Sleep(duration)
b.load(bgctx, task, uid)
case <-b.stop:
b.Debug(bgctx, "shutting down conv loader loop for %s", uid)
return
}
}
}
func (b *BackgroundConvLoader) newQueue() {
if b.queue != nil {
close(b.queue)
}
b.queue = make(chan clTask, 200)
}
func (b *BackgroundConvLoader) load(ctx context.Context, task clTask, uid gregor1.UID) {
b.Debug(ctx, "loading conversation %s", task.convID)
var breaks []keybase1.TLFIdentifyFailure
ctx = Context(ctx, b.G(), keybase1.TLFIdentifyBehavior_CHAT_GUI, &breaks, b.identNotifier)
if b.testingNameInfoSource != nil {
CtxKeyFinder(ctx, b.G()).SetNameInfoSourceOverride(b.testingNameInfoSource)
}
query := &chat1.GetThreadQuery{MarkAsRead: false}
pagination := &chat1.Pagination{Num: 50}
_, _, err := b.G().ConvSource.Pull(ctx, task.convID, uid, query, pagination)
if err != nil {
b.Debug(ctx, "ConvSource.Pull error: %s (%T)", err, err)
if _, ok := err.(*TransientUnboxingError); ok && task.attempt+1 < bgLoaderMaxAttempts {
b.Debug(ctx, "transient error, retrying")
task.attempt++
task.lastAttemptAt = time.Now()
b.enqueue(ctx, task)
return
}
b.Debug(ctx, "failed to load conv %s", task.convID)
return
}
b.Debug(ctx, "loaded conversation %s", task.convID)
// if testing, put the convID on the loads channel
if b.loads != nil {
b.Debug(ctx, "putting convID %s on loads chan", task.convID)
b.loads <- task.convID
}
}