blob: e7cb5dd1b8f91a359600e06e4ca5b1c9611dcad5 [file] [log] [blame]
William Kurkianea869482019-04-09 15:16:11 -04001package cluster
2
3import (
4 "fmt"
5 "sort"
6 "sync"
7)
8
9type none struct{}
10
11type topicPartition struct {
12 Topic string
13 Partition int32
14}
15
16func (tp *topicPartition) String() string {
17 return fmt.Sprintf("%s-%d", tp.Topic, tp.Partition)
18}
19
20type offsetInfo struct {
21 Offset int64
22 Metadata string
23}
24
25func (i offsetInfo) NextOffset(fallback int64) int64 {
26 if i.Offset > -1 {
27 return i.Offset
28 }
29 return fallback
30}
31
32type int32Slice []int32
33
34func (p int32Slice) Len() int { return len(p) }
35func (p int32Slice) Less(i, j int) bool { return p[i] < p[j] }
36func (p int32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
37
38func (p int32Slice) Diff(o int32Slice) (res []int32) {
39 on := len(o)
40 for _, x := range p {
41 n := sort.Search(on, func(i int) bool { return o[i] >= x })
42 if n < on && o[n] == x {
43 continue
44 }
45 res = append(res, x)
46 }
47 return
48}
49
50// --------------------------------------------------------------------
51
52type loopTomb struct {
53 c chan none
54 o sync.Once
55 w sync.WaitGroup
56}
57
58func newLoopTomb() *loopTomb {
59 return &loopTomb{c: make(chan none)}
60}
61
62func (t *loopTomb) stop() { t.o.Do(func() { close(t.c) }) }
63func (t *loopTomb) Close() { t.stop(); t.w.Wait() }
64
65func (t *loopTomb) Dying() <-chan none { return t.c }
66func (t *loopTomb) Go(f func(<-chan none)) {
67 t.w.Add(1)
68
69 go func() {
70 defer t.stop()
71 defer t.w.Done()
72
73 f(t.c)
74 }()
75}