-
Notifications
You must be signed in to change notification settings - Fork 1
/
broker.go
107 lines (87 loc) · 2.63 KB
/
broker.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
package broker
import (
"context"
"errors"
"sync"
"github.com/ispiroglu/mercurius/internal/logger"
pb "github.com/ispiroglu/mercurius/proto"
"go.uber.org/zap"
)
type Broker struct {
logger *zap.Logger
*TopicRepository
SubscriberRepository *SubscriberRepository
retriedEvents sync.Map
}
func NewBroker() *Broker {
return &Broker{
logger: logger.NewLogger(),
TopicRepository: NewTopicRepository(),
SubscriberRepository: NewSubscriberRepository(),
retriedEvents: sync.Map{},
}
}
func (b *Broker) Publish(event *pb.Event) (*pb.ACK, error) {
t, err := b.findOrInsertTopic(event.Topic)
if err != nil {
return nil, err
}
t.PublishEvent(event)
return &pb.ACK{}, nil
}
func (b *Broker) Unsubscribe(sub *Subscriber) {
b.TopicRepository.Unsubscribe(sub)
if err := b.SubscriberRepository.Unsubscribe(sub); err != nil {
b.logger.Warn("Failed to unsubscribe subscriber", zap.String("SubscriberID", sub.Id), zap.String("Subscriber Name", sub.Name), zap.Error(err))
}
}
func (b *Broker) Subscribe(ctx context.Context, topicName string, sId string, sName string) (*Subscriber, error) {
t, err := b.findOrInsertTopic(topicName)
if err != nil {
b.logger.Error("Broker could not find or insert topic", zap.String("Topic", topicName), zap.Error(err))
return nil, err
}
s, err := t.AddSubscriber(ctx, sId, sName)
if err != nil {
b.logger.Error("Broker could not add subscriber to topic", zap.String("Topic", topicName), zap.String("SubscriberID", sId), zap.Error(err))
return nil, err
}
b.SubscriberRepository.addSub(s)
return s, nil
}
func (b *Broker) Retry(_ context.Context, in *pb.RetryRequest) (*pb.ACK, error) {
retryCountInterface, ok := b.retriedEvents.Load(in.Event.Id)
retryCount := 0
if ok {
retryCount = retryCountInterface.(int)
}
retryCount++
if retryCount == 4 {
err := errors.New("exceeded retry limit")
b.retriedEvents.Delete(in.Event.Id)
return nil, err
}
streamPool, ok := b.SubscriberRepository.StreamPools.Load(in.SubscriberID)
if !ok {
return nil, errors.New("invalid retry request")
}
*streamPool.(*StreamPool).Ch <- in.Event
b.retriedEvents.Store(in.Event.Id, retryCount)
return &pb.ACK{}, nil
}
func (b *Broker) GetTopics() map[string]bool {
topics := make(map[string]bool, 0)
b.TopicRepository.Topics.Range(func(key, value interface{}) bool {
topics[key.(string)] = true
return true
})
return topics
}
func (b *Broker) findOrInsertTopic(topicName string) (*Topic, error) {
if topic, ok := b.TopicRepository.Topics.Load(topicName); ok {
return topic.(*Topic), nil
}
t := newTopic(topicName)
b.TopicRepository.Topics.Store(topicName, t)
return t, nil
}