-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
182 lines (149 loc) · 4.63 KB
/
config.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright © 2020 Dmitry Stoletov <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mt
import (
"encoding/json"
"fmt"
"time"
"github.com/google/uuid"
"github.com/streadway/amqp"
)
// ParseConfig read config from json.
func ParseConfig(jsonConf []byte) (Config, error) {
var conf Config
err := json.Unmarshal(jsonConf, &conf)
if err != nil {
return conf, fmt.Errorf("failed to unmarshal, %w", err)
}
return conf, nil
}
// Config ...
type Config struct {
Services map[ServiceName]Service `json:"services"`
}
type Service struct {
Exchange Exchange `json:"exchange"`
}
type Exchange struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Durable bool `json:"durable,omitempty"`
AutoDelete bool `json:"autodelete,omitempty"`
Internal bool `json:"internal,omitempty"`
NoWait bool `json:"nowait,omitempty"`
Arguments amqp.Table `json:"arguments,omitempty"`
Binding Binding `json:"binding,omitempty"`
Queue Queue `json:"queue,omitempty"`
ReplyQueue ReplyQueue `json:"replyqueue,omitempty"`
}
func (e *Exchange) UnmarshalJSON(b []byte) error {
type xExchange Exchange
xEx := xExchange(DefaultExchange())
if err := json.Unmarshal(b, &xEx); err != nil {
return fmt.Errorf("failed to unmarshal, %w", err)
}
*e = Exchange(xEx)
return nil
}
func DefaultExchange() Exchange {
return Exchange{
Name: "",
Type: "",
Durable: true,
AutoDelete: false,
Internal: false,
NoWait: false,
Arguments: amqp.Table{},
Binding: DefaultBinding(),
Queue: DefaultQueue(),
ReplyQueue: ReplyQueue{
Timeout: Duration(time.Minute),
},
}
}
type Binding struct {
Key string `json:"key,omitempty"`
NoWait bool `json:"nowait,omitempty"`
Arguments amqp.Table `json:"arguments,omitempty"`
}
func DefaultBinding() Binding {
return Binding{
Key: "",
NoWait: false,
Arguments: amqp.Table{},
}
}
type Queue struct {
Name string `json:"name,omitempty"`
Durable bool `json:"durable,omitempty"`
AutoDelete bool `json:"autodelete,omitempty"`
NoWait bool `json:"nowait,omitempty"`
Arguments amqp.Table `json:"arguments,omitempty"`
// @see http://www.rabbitmq.com/blog/2012/04/25/rabbitmq-performance-measurements-part-2/
// Консумер не получит следующие n сообщений, пока не подтвердит предыдущие.
PrefetchCount int `json:"prefetchCount,omitempty"`
Consumer Consume `json:"consume,omitempty"`
}
func DefaultQueue() Queue {
return Queue{
Name: "",
Durable: true,
AutoDelete: false,
NoWait: false,
Arguments: amqp.Table{},
PrefetchCount: 0,
Consumer: DefaultConsumer(),
}
}
type Consume struct {
Tag string `json:"tag,omitempty"`
// управление сообщением в случае невозможности обработки
// true - сообщение вновь ставится в очередь
// false - отбросить сообщение в отстойник, если он настроен. Иначе удалить сообщение
Requeue bool `json:"requeue,omitempty"`
NoAck bool `json:"noack,omitempty"`
Exclusive bool `json:"exclusive,omitempty"`
NoWait bool `json:"nowait,omitempty"`
Arguments amqp.Table `json:"arguments,omitempty"`
}
func DefaultConsumer() Consume {
return Consume{
Tag: uniqueConsumerTag(),
Requeue: true,
NoAck: false,
Exclusive: false,
NoWait: false,
Arguments: amqp.Table{},
}
}
func uniqueConsumerTag() string {
return "ctag-" + uuid.New().String()
}
type ReplyQueue struct {
BindToExchange bool `json:"bindToExchange,omitempty"`
Timeout Duration `json:"timeout,omitempty"`
}
type Duration time.Duration
func (e *Duration) UnmarshalJSON(b []byte) error {
val := ""
if err := json.Unmarshal(b, &val); err != nil {
return fmt.Errorf("failed to unmarshal, %w", err)
}
dur, err := time.ParseDuration(val)
if err != nil {
return fmt.Errorf("failed to parse, %w", err)
}
*e = Duration(dur)
return nil
}