forked from lovoo/goka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
122 lines (102 loc) · 2.03 KB
/
proxy.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
package goka
import (
"fmt"
"sync"
"time"
"github.com/lovoo/goka/kafka"
"github.com/lovoo/goka/storage"
)
const (
delayProxyInterval = 1 * time.Second
)
type proxy struct {
partition int32
consumer kafka.Consumer
}
func (p *proxy) Add(topic string, offset int64) error {
if err := p.consumer.AddPartition(topic, p.partition, offset); err != nil {
return fmt.Errorf("error adding %s/%d: %v", topic, p.partition, err)
}
return nil
}
func (p *proxy) Remove(topic string) error {
if err := p.consumer.RemovePartition(topic, p.partition); err != nil {
return fmt.Errorf("error removing %s/%d: %v", topic, p.partition, err)
}
return nil
}
func (p *proxy) AddGroup() {
p.consumer.AddGroupPartition(p.partition)
}
func (p *proxy) Stop() {}
type delayProxy struct {
proxy
stop bool
m sync.Mutex
wait []func() bool
}
func (p *delayProxy) waitersDone() bool {
for _, r := range p.wait {
if !r() {
return false
}
}
return true
}
func (p *delayProxy) AddGroup() {
if len(p.wait) == 0 {
p.consumer.AddGroupPartition(p.partition)
return
}
go func() {
ticker := time.NewTicker(delayProxyInterval)
defer ticker.Stop()
for range ticker.C {
p.m.Lock()
if p.stop {
p.m.Unlock()
return
}
if p.waitersDone() {
p.consumer.AddGroupPartition(p.partition)
p.m.Unlock()
return
}
p.m.Unlock()
}
}()
}
func (p *delayProxy) Stop() {
p.m.Lock()
p.stop = true
p.m.Unlock()
}
type storageProxy struct {
storage.Storage
partition int32
stateless bool
update UpdateCallback
openedOnce once
closedOnce once
}
func (s *storageProxy) Open() error {
if s == nil {
return nil
}
return s.openedOnce.Do(s.Storage.Open)
}
func (s *storageProxy) Close() error {
if s == nil {
return nil
}
return s.closedOnce.Do(s.Storage.Close)
}
func (s *storageProxy) Update(k string, v []byte) error {
return s.update(s.Storage, s.partition, k, v)
}
func (s *storageProxy) Stateless() bool {
return s.stateless
}
func (s *storageProxy) MarkRecovered() error {
return s.Storage.MarkRecovered()
}