/
subscription.go
147 lines (125 loc) · 2.81 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
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
package gpubsub
import (
"context"
"github.com/google/uuid"
"golang.org/x/sync/semaphore"
"log"
"sync"
"time"
)
type Subscription[T any] struct {
mu sync.RWMutex
name string
topic *Topic[T]
ch chan string
messages map[string]*Message[T]
concurrency int64
interval time.Duration
ttl time.Duration
}
func (s *Subscription[T]) Name() string {
return s.name
}
func (s *Subscription[T]) Topic() *Topic[T] {
return s.topic
}
func (s *Subscription[T]) Subscribe(ctx context.Context, consumer func(*Message[T])) {
var wg sync.WaitGroup
sem := semaphore.NewWeighted(s.concurrency)
s.register()
go s.watch(ctx, s.interval, s.ttl)
for {
select {
case id := <-s.ch:
message := s.messages[id]
err := s.do(ctx, &wg, sem, consumer, message)
if err != nil && err != context.Canceled {
panic(err)
}
case <-ctx.Done():
s.unregister()
log.Printf("closing subscription: %s\n", s.Name())
cancelCtx := context.Background()
for {
select {
case id := <-s.ch:
message := s.messages[id]
err := s.do(cancelCtx, &wg, sem, consumer, message)
if err != nil {
panic(err)
}
default:
wg.Wait()
return
}
}
}
}
}
func (s *Subscription[T]) register() {
s.topic.register(s)
}
func (s *Subscription[T]) unregister() {
s.topic.unregister(s)
}
func (s *Subscription[T]) newMessage(body T) *Message[T] {
return &Message[T]{
id: uuid.New().String(),
body: body,
subscription: s,
createdAt: time.Now(),
}
}
func (s *Subscription[T]) publish(message *Message[T]) {
s.mu.Lock()
defer s.mu.Unlock()
s.ch <- message.id
s.messages[message.id] = message
}
func (s *Subscription[T]) ack(message *Message[T]) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.messages, message.id)
}
func (s *Subscription[T]) nack(message *Message[T]) {
}
func (s *Subscription[T]) remind(message *Message[T]) {
s.ch <- message.id
}
func (s *Subscription[T]) do(ctx context.Context, wg *sync.WaitGroup, sem *semaphore.Weighted, consumer func(*Message[T]), message *Message[T]) error {
wg.Add(1)
if err := sem.Acquire(ctx, 1); err == nil {
go func() {
defer wg.Done()
defer sem.Release(1)
defer message.touch()
consumer(message)
}()
return nil
} else {
s.remind(message)
wg.Done()
return err
}
}
func (s *Subscription[T]) salvage(interval time.Duration, ttl time.Duration) {
for _, message := range s.messages {
if time.Now().Sub(message.createdAt) > ttl {
s.ack(message)
}
if time.Now().Sub(message.lastViewedAt) > interval {
s.remind(message)
}
}
}
func (s *Subscription[T]) watch(ctx context.Context, interval time.Duration, ttl time.Duration) {
ticker := time.NewTicker(interval)
for {
select {
case <-ticker.C:
s.salvage(interval, ttl)
case <-ctx.Done():
return
}
}
}