-
Notifications
You must be signed in to change notification settings - Fork 3
/
broker_helper.go
95 lines (81 loc) · 2.13 KB
/
broker_helper.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
package broker
import (
"bytes"
"context"
"fmt"
"git.golaxy.org/core"
"git.golaxy.org/core/util/generic"
"git.golaxy.org/framework/util/binaryutil"
)
type (
ErrorHandler = generic.DelegateAction1[error] // 错误处理器
)
// MakeWriteChan creates a new channel for publishing data to a specific topic.
func MakeWriteChan(broker IBroker, topic string, size int, errorHandler ...ErrorHandler) chan<- binaryutil.RecycleBytes {
if broker == nil {
panic(fmt.Errorf("%w: broker is nil", core.ErrArgs))
}
var _errorHandler ErrorHandler
if len(errorHandler) > 0 {
_errorHandler = errorHandler[0]
}
ch := make(chan binaryutil.RecycleBytes, size)
go func() {
defer func() {
for bs := range ch {
bs.Release()
}
}()
for bs := range ch {
err := broker.Publish(context.Background(), topic, bs.Data())
bs.Release()
if err != nil {
_errorHandler.Invoke(nil, err)
}
}
}()
return ch
}
// MakeReadChan creates a new channel for receiving data from a specific pattern.
func MakeReadChan(broker IBroker, ctx context.Context, pattern, queue string, size int, recyclable ...bool) (<-chan binaryutil.RecycleBytes, error) {
if broker == nil {
panic(fmt.Errorf("%w: broker is nil", core.ErrArgs))
}
if ctx == nil {
ctx = context.Background()
}
var _recyclable bool
if len(recyclable) > 0 {
_recyclable = recyclable[0]
}
ch := make(chan binaryutil.RecycleBytes, size)
_, err := broker.Subscribe(ctx, pattern,
With.Queue(queue),
With.EventHandler(generic.MakeDelegateFunc1(func(e IEvent) error {
bs := func() binaryutil.RecycleBytes {
if _recyclable {
return binaryutil.MakeRecycleBytes(binaryutil.BytesPool.Clone(e.Message()))
} else {
return binaryutil.MakeNonRecycleBytes(bytes.Clone(e.Message()))
}
}()
select {
case ch <- bs:
return nil
default:
bs.Release()
var nakErr error
if e.Queue() != "" {
nakErr = e.Nak(context.Background())
}
return fmt.Errorf("read chan is full, nak: %v", nakErr)
}
})),
With.UnsubscribedCB(generic.MakeDelegateAction1(func(sub ISubscriber) {
close(ch)
})))
if err != nil {
return nil, err
}
return ch, nil
}