-
Notifications
You must be signed in to change notification settings - Fork 3
/
longpoll.go
59 lines (50 loc) · 1.06 KB
/
longpoll.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
package dropbox
import (
"context"
"fmt"
"os"
"time"
)
// Longpoll signals changes in dir by sending on ch.
func (db Client) Longpoll(ctx context.Context, dir string, ch chan<- []Metadata) {
for {
res, err := db.ListFolder(ListFolderParams{
Path: dir,
})
if err != nil {
fmt.Fprintln(os.Stderr, "ListFolder:", err)
continue
}
for res.HasMore {
res, err = db.ListFolderContinue(res.Cursor)
if err != nil {
fmt.Fprintln(os.Stderr, "ListFolderContinue:", err)
continue
}
}
cu := res.Cursor
changed, backoff, err := db.ListFolderLongPoll(ctx, cu, 480)
if err != nil {
fmt.Fprintln(os.Stderr, "ListFolderLongPoll:", err)
continue
}
if backoff != 0 {
time.Sleep(time.Second * time.Duration(backoff))
}
if !changed {
continue
}
res = ListFolderResult{HasMore: true, Cursor: cu}
for res.HasMore {
res, err = db.ListFolderContinue(res.Cursor)
if err != nil {
fmt.Fprintln(os.Stderr, "ListFolderContinue:", err)
continue
}
if len(res.Entries) > 0 {
ch <- res.Entries
break
}
}
}
}