-
Notifications
You must be signed in to change notification settings - Fork 0
/
standard.go
69 lines (57 loc) · 1.35 KB
/
standard.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
package queue
import (
"github.com/zhiyunliu/glue/config"
"github.com/zhiyunliu/glue/container"
)
const (
TypeNode = "queues"
_defaultName = "default"
)
type StandardQueue interface {
GetQueue(name string, opts ...Option) (q IQueue)
}
// xQueue queue
type xQueue struct {
c container.Container
}
// NewStandardQueue 创建queue
func NewStandardQueue(c container.Container) StandardQueue {
return &xQueue{c: c}
}
// GetQueue GetQueue
func (s *xQueue) GetQueue(name string, opts ...Option) (q IQueue) {
realName := name
if realName == "" {
realName = _defaultName
}
obj, err := s.c.GetOrCreate(TypeNode, realName, func(cfg config.Config) (interface{}, error) {
//{"proto":"redis","addr":"redis://localhost"}
cfgVal := cfg.Get(TypeNode).Get(realName)
protoType := cfgVal.Value("proto").String()
return newQueue(protoType, cfgVal, opts...)
}, s.getUniqueKey(opts...))
if err != nil {
panic(err)
}
return obj.(IQueue)
}
func (s *xQueue) getUniqueKey(opts ...Option) string {
if len(opts) == 0 {
return ""
}
tmpCfg := &Options{}
for i := range opts {
opts[i](tmpCfg)
}
return tmpCfg.getUniqueKey()
}
type xBuilder struct{}
func NewBuilder() container.StandardBuilder {
return &xBuilder{}
}
func (xBuilder) Name() string {
return TypeNode
}
func (xBuilder) Build(c container.Container) interface{} {
return NewStandardQueue(c)
}