-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription.go
85 lines (70 loc) · 1.99 KB
/
subscription.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
package gopubsub
import (
"context"
"github.com/google/uuid"
"gocloud.dev/gcerrors"
"gocloud.dev/pubsub/driver"
)
const defaultPublishChannelSize = 10000
type subscription struct {
ID uuid.UUID
C chan *driver.Message
unsubscribe func()
}
func OpenSubscription(ctx context.Context, topicName string) (*subscription, error) {
t, _ := openTopic(ctx, topicName)
s := subscription{
ID: uuid.New(),
C: make(chan *driver.Message, defaultPublishChannelSize),
}
s.unsubscribe = t.add(s)
return &s, nil
}
func (m subscription) ReceiveBatch(ctx context.Context, n int) ([]*driver.Message, error) {
// c, cancel := context.WithTimeout(ctx, 10*time.Millisecond)
// defer cancel()
for {
select {
// case <-c.Done():
// return nil, errors.New("receive message timeout")
case dm := <-m.C:
return []*driver.Message{dm}, nil
}
}
}
// As implements driver.Subscription.As.
func (s *subscription) As(i interface{}) bool {
return true
}
// CanNack implements driver.CanNack.
func (s *subscription) CanNack() bool {
// Nacking a single message doesn't make sense with the way Kafka maintains
// offsets.
return false
}
// SendNacks implements driver.Subscription.SendNacks.
func (s *subscription) SendNacks(ctx context.Context, ids []driver.AckID) error {
panic("unreachable")
}
// Close implements io.Closer.
func (s *subscription) Close() error {
// Cancel the ctx for the background goroutine and wait until it's done.
s.unsubscribe()
return nil
}
// ErrorAs implements driver.Subscription.ErrorAs.
func (s *subscription) ErrorAs(err error, i interface{}) bool {
return true
}
// ErrorCode implements driver.Subscription.ErrorCode.
func (*subscription) ErrorCode(err error) gcerrors.ErrorCode {
return gcerrors.Internal
}
// IsRetryable implements driver.Subscription.IsRetryable.
func (*subscription) IsRetryable(error) bool {
return false
}
// SendAcks implements driver.Subscription.SendAcks.
func (s *subscription) SendAcks(ctx context.Context, ids []driver.AckID) error {
return nil
}