-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber.go
89 lines (76 loc) · 2.03 KB
/
subscriber.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
//
// @project geniusrabbit.com 2015 – 2016, 2020
// @author Dmitry Ponomarev <demdxx@gmail.com> 2015 – 2016, 2020
//
package notificationcenter
import (
"context"
"io"
"reflect"
"github.com/pkg/errors"
)
var (
errInvalidReceiver = errors.New("invalid receiver value")
errReceiverAlreadyRegistered = errors.New("receiver already registered")
)
// Subscriber provides methods of working with subscription
//go:generate mockgen -source $GOFILE -package mocks -destination mocks/subscriber.go
type Subscriber interface {
io.Closer
// Subscribe new receiver to receive messages from the subsribtion
Subscribe(ctx context.Context, receiver Receiver) error
// Start processing queue
Listen(ctx context.Context) error
}
// ModelSubscriber provedes subscibe functionality implementation
type ModelSubscriber struct {
receivers []Receiver
// Error handler pointer
ErrorHandler ErrorHandler
// Panic handler pointer
PanicHandler PanicHandler
}
// Subscribe new receiver to receive messages from the subsribtion
func (s *ModelSubscriber) Subscribe(ctx context.Context, receiver Receiver) error {
if receiver == nil {
return errInvalidReceiver
}
for _, current := range s.receivers {
if reflect.ValueOf(current).Pointer() == reflect.ValueOf(receiver).Pointer() {
return errReceiverAlreadyRegistered
}
}
s.receivers = append(s.receivers, receiver)
return nil
}
// ProcessMessage by all receivers
func (s *ModelSubscriber) ProcessMessage(msg Message) error {
defer func() {
if s.PanicHandler == nil {
return
}
if rec := recover(); rec != nil {
s.PanicHandler(msg, rec)
}
}()
for _, r := range s.receivers {
if err := r.Receive(msg); err != nil {
if s.ErrorHandler == nil {
return err
}
s.ErrorHandler(msg, err)
}
}
return nil
}
// Close all receivers if supports io.Closer interface
func (s *ModelSubscriber) Close() error {
for _, rec := range s.receivers {
if closer, _ := rec.(io.Closer); closer != nil {
if err := closer.Close(); err != nil {
return err
}
}
}
return nil
}