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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
package protocol
import (
"github.com/fatih/color"
j "github.com/json-iterator/go"
"github.com/spiral/errors"
"github.com/spiral/roadrunner/v2/pkg/payload"
"github.com/spiral/roadrunner/v2/plugins/logger"
"go.temporal.io/api/common/v1"
"go.temporal.io/api/failure/v1"
)
var json = j.ConfigCompatibleWithStandardLibrary
// JSONCodec can be used for debugging and log capturing reasons.
type JSONCodec struct {
// level enables verbose logging or all incoming and outcoming messages.
level DebugLevel
// logger renders messages when debug enabled.
logger logger.Logger
}
// jsonFrame contains message command in binary form.
type jsonFrame struct {
// ID contains ID of the command, response or error.
ID uint64 `json:"id"`
// Command name. Optional.
Command string `json:"command,omitempty"`
// Options to be unmarshalled to body (raw payload).
Options j.RawMessage `json:"options,omitempty"`
// Failure associated with command id.
Failure []byte `json:"failure,omitempty"`
// Payloads specific to the command or result.
Payloads []byte `json:"payloads,omitempty"`
}
// NewJSONCodec creates new Json communication codec.
func NewJSONCodec(level DebugLevel, logger logger.Logger) Codec {
return &JSONCodec{
level: level,
logger: logger,
}
}
// WithLogger creates new codes instance with attached logger.
func (c *JSONCodec) WithLogger(logger logger.Logger) Codec {
return &JSONCodec{
level: c.level,
logger: logger,
}
}
// GetName returns codec name.
func (c *JSONCodec) GetName() string {
return "json"
}
// Execute exchanges commands with worker.
func (c *JSONCodec) Execute(e Endpoint, ctx Context, msg ...Message) ([]Message, error) {
const op = errors.Op("json_codec_execute")
if len(msg) == 0 {
return nil, nil
}
var response = make([]jsonFrame, 0, 5)
var result = make([]Message, 0, 5)
var err error
frames := make([]jsonFrame, 0, len(msg))
for _, m := range msg {
frame, err := c.packFrame(m)
if err != nil {
return nil, errors.E(op, err)
}
frames = append(frames, frame)
}
p := payload.Payload{}
if ctx.IsEmpty() {
p.Context = []byte("null")
}
p.Context, err = json.Marshal(ctx)
if err != nil {
return nil, errors.E(op, err)
}
p.Body, err = json.Marshal(frames)
if err != nil {
return nil, errors.E(op, err)
}
if c.level >= DebugNormal {
logMessage := string(p.Body) + " " + string(p.Context)
if c.level >= DebugHumanized {
logMessage = color.GreenString(logMessage)
}
c.logger.Debug(logMessage)
}
out, err := e.Exec(p)
if err != nil {
return nil, errors.E(op, err)
}
if len(out.Body) == 0 {
// worker inactive or closed
return nil, nil
}
if c.level >= DebugNormal {
logMessage := string(out.Body)
if c.level >= DebugHumanized {
logMessage = color.HiYellowString(logMessage)
}
c.logger.Debug(logMessage, "receive", true)
}
err = json.Unmarshal(out.Body, &response)
if err != nil {
return nil, errors.E(op, err)
}
for _, f := range response {
msg, err := c.parseFrame(f)
if err != nil {
return nil, errors.E(op, err)
}
result = append(result, msg)
}
return result, nil
}
func (c *JSONCodec) packFrame(msg Message) (jsonFrame, error) {
var (
err error
frame jsonFrame
)
frame.ID = msg.ID
if msg.Payloads != nil {
frame.Payloads, err = msg.Payloads.Marshal()
if err != nil {
return jsonFrame{}, err
}
}
if msg.Failure != nil {
frame.Failure, err = msg.Failure.Marshal()
if err != nil {
return jsonFrame{}, err
}
}
if msg.Command == nil {
return frame, nil
}
frame.Command, err = commandName(msg.Command)
if err != nil {
return jsonFrame{}, err
}
frame.Options, err = json.Marshal(msg.Command)
if err != nil {
return jsonFrame{}, err
}
return frame, nil
}
func (c *JSONCodec) parseFrame(frame jsonFrame) (Message, error) {
var (
err error
msg Message
)
msg.ID = frame.ID
if frame.Payloads != nil {
msg.Payloads = &common.Payloads{}
err = msg.Payloads.Unmarshal(frame.Payloads)
if err != nil {
return Message{}, err
}
}
if frame.Failure != nil {
msg.Failure = &failure.Failure{}
err = msg.Failure.Unmarshal(frame.Failure)
if err != nil {
return Message{}, err
}
}
if frame.Command != "" {
cmd, err := initCommand(frame.Command)
if err != nil {
return Message{}, err
}
err = json.Unmarshal(frame.Options, &cmd)
if err != nil {
return Message{}, err
}
msg.Command = cmd
}
return msg, nil
}
|