-
Notifications
You must be signed in to change notification settings - Fork 2
/
topic.go
63 lines (53 loc) · 1.73 KB
/
topic.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
package broker
import (
"context"
"fmt"
"sync"
"github.com/alitto/pond"
"github.com/ispiroglu/mercurius/internal/logger"
"github.com/ispiroglu/mercurius/proto"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type Topic struct {
sync.RWMutex
logger *zap.Logger
Name string
SubscriberRepository *SubscriberRepository
EventChan chan *proto.Event
workerPool *pond.WorkerPool
}
func (t *Topic) PublishEvent(event *proto.Event) {
if t.SubscriberRepository.poolCount.Load() == 0 {
select {
case t.EventChan <- event:
default:
t.logger.Warn("Event channel is full, event is dropped", zap.String("Topic", t.Name), zap.String("Event", event.String()))
}
} else {
t.SubscriberRepository.StreamPools.Range(func(k any, v interface{}) bool {
c := *v.(*StreamPool).Ch
c <- event
return true
})
}
}
func (t *Topic) AddSubscriber(ctx context.Context, id string, name string) (*Subscriber, error) {
s, err := t.SubscriberRepository.addSubscriber(ctx, id, name, t.Name)
if err != nil {
t.logger.Error("Could not add already existing subscriber to topic", zap.String("Topic", t.Name), zap.String("SubscriberID", id), zap.String("Subscriber name", name))
errorMessage := fmt.Sprintf("This subscriber: %s is alreay added to this topic: %s\n", id, t.Name)
return nil, status.Error(codes.AlreadyExists, errorMessage)
}
return s, nil
}
func newTopic(name string) *Topic {
return &Topic{
logger: logger.NewLogger(),
Name: name,
SubscriberRepository: NewSubscriberRepository(),
EventChan: make(chan *proto.Event), // If channel is full, there will be waiting goroutines.
workerPool: nil,
}
}