forked from jdextraze/go-gesclient
/
operation.go
189 lines (176 loc) · 6.07 KB
/
operation.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
183
184
185
186
187
188
189
package operations
import (
"errors"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/jdextraze/go-gesclient/client"
"github.com/jdextraze/go-gesclient/messages"
"github.com/jdextraze/go-gesclient/tasks"
"github.com/satori/go.uuid"
"net"
"reflect"
"sync/atomic"
)
type CreateRequestDtoHandler func() proto.Message
type InspectResponseHandler func(message proto.Message) (*client.InspectionResult, error)
type TransformResponseHandler func(message proto.Message) (interface{}, error)
type CreateResponseHandler func() proto.Message
type baseOperation struct {
requestCommand client.Command
responseCommand client.Command
userCredentials *client.UserCredentials
source *tasks.CompletionSource
response proto.Message
completed int32
createRequestDto CreateRequestDtoHandler
inspectResponse InspectResponseHandler
transformResponse TransformResponseHandler
createResponse CreateResponseHandler
}
func newBaseOperation(
requestCommand client.Command,
responseCommand client.Command,
userCredentials *client.UserCredentials,
source *tasks.CompletionSource,
createRequestDto CreateRequestDtoHandler,
inspectResponse InspectResponseHandler,
transformResponse TransformResponseHandler,
createResponse CreateResponseHandler,
) *baseOperation {
if source == nil {
panic("source is nil")
}
return &baseOperation{
source: source,
requestCommand: requestCommand,
responseCommand: responseCommand,
userCredentials: userCredentials,
createRequestDto: createRequestDto,
inspectResponse: inspectResponse,
transformResponse: transformResponse,
createResponse: createResponse,
}
}
func (o *baseOperation) CreateNetworkPackage(correlationId uuid.UUID) (*client.Package, error) {
var flags client.TcpFlag
if o.userCredentials != nil {
flags = client.FlagsAuthenticated
}
data, err := proto.Marshal(o.createRequestDto())
if err != nil {
return nil, err
}
return client.NewTcpPackage(o.requestCommand, flags, correlationId, data, o.userCredentials), err
}
func (o *baseOperation) InspectPackage(p *client.Package) (result *client.InspectionResult, err error) {
if p.Command() == o.responseCommand {
o.response = o.createResponse()
if err = proto.Unmarshal(p.Data(), o.response); err == nil {
result, err = o.inspectResponse(o.response)
}
} else {
switch p.Command() {
case client.Command_NotAuthenticated:
result, err = o.inspectNotAuthenticated(p)
case client.Command_BadRequest:
result, err = o.inspectBadRequest(p)
case client.Command_NotHandled:
result, err = o.inspectNotHandled(p)
default:
result, err = o.inspectUnexpectedCommand(p, o.responseCommand)
}
}
if err != nil {
err = o.Fail(err)
result = client.NewInspectionResult(client.InspectionDecision_EndOperation, err.Error(), nil, nil)
}
return
}
func (o *baseOperation) succeed() error {
if atomic.CompareAndSwapInt32(&o.completed, 0, 1) {
if o.response != nil {
if result, err := o.transformResponse(o.response); err != nil {
return err
} else {
return o.source.SetResult(result)
}
} else {
return o.source.SetError(errors.New("No result"))
}
}
return nil
}
func (o *baseOperation) Fail(err error) error {
if atomic.CompareAndSwapInt32(&o.completed, 0, 1) {
return o.source.SetError(err)
}
return nil
}
func (o *baseOperation) inspectNotAuthenticated(p *client.Package) (*client.InspectionResult, error) {
msg := string(p.Data())
if msg == "" {
msg = "Authentication error"
}
return client.NewInspectionResult(client.InspectionDecision_EndOperation, "NotAuthenticated", nil, nil),
o.Fail(errors.New(msg))
}
func (o *baseOperation) inspectBadRequest(p *client.Package) (*client.InspectionResult, error) {
msg := string(p.Data())
if msg == "" {
msg = "<no message>"
}
return client.NewInspectionResult(client.InspectionDecision_EndOperation, fmt.Sprintf("BadRequest - %s", msg), nil,
nil), o.Fail(errors.New(msg))
}
func (o *baseOperation) inspectNotHandled(p *client.Package) (*client.InspectionResult, error) {
var err error
dto := &messages.NotHandled{}
if err := proto.Unmarshal(p.Data(), dto); err != nil {
return nil, fmt.Errorf("Invalid payload for NotHandled: %v", err)
}
switch dto.GetReason() {
case messages.NotHandled_NotReady:
return client.NewInspectionResult(client.InspectionDecision_Retry, "NotHandled - NotReady", nil, nil), nil
case messages.NotHandled_TooBusy:
return client.NewInspectionResult(client.InspectionDecision_Retry, "NotHandled - TooBusy", nil, nil), nil
case messages.NotHandled_NotMaster:
masterInfo := &messages.NotHandled_MasterInfo{}
if err = proto.Unmarshal(dto.AdditionalInfo, masterInfo); err != nil {
break
}
var tcpEndpoint *net.TCPAddr
var secureTcpEndpoint *net.TCPAddr
tcpEndpoint, err = net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", masterInfo.GetExternalTcpAddress(),
masterInfo.GetExternalTcpPort()))
if err != nil {
break
}
secureTcpEndpoint, err = net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d",
masterInfo.GetExternalSecureTcpAddress(), masterInfo.GetExternalSecureTcpPort()))
if err != nil {
break
}
return client.NewInspectionResult(client.InspectionDecision_Reconnect, "NotHandled - NotMaster",
tcpEndpoint, secureTcpEndpoint), nil
default:
log.Errorf("Unknown NotHandledReason: %s", dto.Reason)
return client.NewInspectionResult(client.InspectionDecision_Retry, "NotHandled - <unknown>", nil, nil), nil
}
return nil, err
}
func (o *baseOperation) inspectUnexpectedCommand(
p *client.Package,
expectedCommand client.Command,
) (*client.InspectionResult, error) {
if p.Command() == expectedCommand {
return nil, fmt.Errorf("Command should not be %s", p.Command())
}
log.Errorf(`Unexpected TcpCommand received.
Expected: %v, Actual: %v, CorrelationId: %v
Operation (%s): %v,
TcpPackage data dump: %v`,
expectedCommand, p.Command, p.CorrelationId, reflect.TypeOf(o).Name(), o, p.Data,
)
return client.NewInspectionResult(client.InspectionDecision_EndOperation, fmt.Sprintf("Unexpected command - %s",
p.Command()), nil, nil), o.Fail(fmt.Errorf("Command not expected: %s. Expected: %s.", p.Command(), expectedCommand))
}