-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer.go
79 lines (63 loc) · 1.71 KB
/
producer.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
// author: wsfuyibing <websearch@163.com>
// date: 2023-02-03
package rabbitmq
import (
"context"
"fmt"
"github.com/fuyibing/gmd/app/md/base"
"github.com/fuyibing/log/v8"
"github.com/fuyibing/util/v8/process"
)
type (
// Producer
// 消息生产者.
Producer struct {
name string
processor process.Processor
}
)
func NewProducer() *Producer {
return (&Producer{}).init()
}
func (o *Producer) Processor() process.Processor {
return o.processor
}
func (o *Producer) Publish(payload *base.Payload) (string, error) {
return "", fmt.Errorf("not support")
}
// /////////////////////////////////////////////////////////////
// 生产者构造
// /////////////////////////////////////////////////////////////
func (o *Producer) init() *Producer {
o.name = "rabbitmq-producer"
o.processor = process.New(o.name).
After(o.onAfter).
Before(o.onBefore).
Callback(o.onCallerBefore, o.onCaller, o.onCallerAfter).
Panic(o.onPanic)
return o
}
// /////////////////////////////////////////////////////////////
// 生产者事件
// /////////////////////////////////////////////////////////////
func (o *Producer) onAfter(ctx context.Context) (ignored bool) {
log.Infof("[%s] producer finish", o.name)
return
}
func (o *Producer) onBefore(ctx context.Context) (ignored bool) {
log.Infof("[%s] producer begin", o.name)
return
}
func (o *Producer) onCaller(ctx context.Context) (ignored bool) {
for {
select {
case <-ctx.Done():
return
}
}
}
func (o *Producer) onCallerAfter(ctx context.Context) (ignored bool) { return }
func (o *Producer) onCallerBefore(ctx context.Context) (ignored bool) { return }
func (o *Producer) onPanic(ctx context.Context, v interface{}) {
log.Panicfc(ctx, "[%s] %v", o.name, v)
}