blob: aa77ffbdc1f924f316b8d53b5171c18e80199fa1 [file] [log] [blame]
William Kurkianea869482019-04-09 15:16:11 -04001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package kafka
17
18import (
19 "context"
20 "errors"
21 "fmt"
William Kurkianea869482019-04-09 15:16:11 -040022 "reflect"
23 "strings"
24 "sync"
25 "time"
William Kurkianea869482019-04-09 15:16:11 -040026
Esin Karamanccb714b2019-11-29 15:02:06 +000027 "github.com/golang/protobuf/proto"
28 "github.com/golang/protobuf/ptypes"
29 "github.com/golang/protobuf/ptypes/any"
30 "github.com/google/uuid"
31 "github.com/opencord/voltha-lib-go/v3/pkg/log"
32 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
33)
William Kurkianea869482019-04-09 15:16:11 -040034
35const (
36 DefaultMaxRetries = 3
37 DefaultRequestTimeout = 10000 // 10000 milliseconds - to handle a wider latency range
38)
39
40const (
41 TransactionKey = "transactionID"
42 FromTopic = "fromTopic"
43)
44
kdarapub26b4502019-10-05 03:02:33 +053045var ErrorTransactionNotAcquired = errors.New("transaction-not-acquired")
46var ErrorTransactionInvalidId = errors.New("transaction-invalid-id")
47
William Kurkianea869482019-04-09 15:16:11 -040048// requestHandlerChannel represents an interface associated with a channel. Whenever, an event is
49// obtained from that channel, this interface is invoked. This is used to handle
50// async requests into the Core via the kafka messaging bus
51type requestHandlerChannel struct {
52 requesthandlerInterface interface{}
53 ch <-chan *ic.InterContainerMessage
54}
55
56// transactionChannel represents a combination of a topic and a channel onto which a response received
57// on the kafka bus will be sent to
58type transactionChannel struct {
59 topic *Topic
60 ch chan *ic.InterContainerMessage
61}
62
npujarec5762e2020-01-01 14:08:48 +053063type InterContainerProxy interface {
64 Start() error
65 Stop()
66 GetDefaultTopic() *Topic
67 DeviceDiscovered(deviceId string, deviceType string, parentId string, publisher string) error
68 InvokeRPC(ctx context.Context, rpc string, toTopic *Topic, replyToTopic *Topic, waitForResponse bool, key string, kvArgs ...*KVArg) (bool, *any.Any)
69 SubscribeWithRequestHandlerInterface(topic Topic, handler interface{}) error
70 SubscribeWithDefaultRequestHandler(topic Topic, initialOffset int64) error
71 UnSubscribeFromRequestHandler(topic Topic) error
72 DeleteTopic(topic Topic) error
73 EnableLivenessChannel(enable bool) chan bool
74 SendLiveness() error
75}
76
77// interContainerProxy represents the messaging proxy
78type interContainerProxy struct {
William Kurkianea869482019-04-09 15:16:11 -040079 kafkaHost string
80 kafkaPort int
npujarec5762e2020-01-01 14:08:48 +053081 defaultTopic *Topic
William Kurkianea869482019-04-09 15:16:11 -040082 defaultRequestHandlerInterface interface{}
83 deviceDiscoveryTopic *Topic
84 kafkaClient Client
npujarec5762e2020-01-01 14:08:48 +053085 doneCh chan struct{}
86 doneOnce sync.Once
William Kurkianea869482019-04-09 15:16:11 -040087
88 // This map is used to map a topic to an interface and channel. When a request is received
89 // on that channel (registered to the topic) then that interface is invoked.
90 topicToRequestHandlerChannelMap map[string]*requestHandlerChannel
91 lockTopicRequestHandlerChannelMap sync.RWMutex
92
93 // This map is used to map a channel to a response topic. This channel handles all responses on that
94 // channel for that topic and forward them to the appropriate consumers channel, using the
95 // transactionIdToChannelMap.
96 topicToResponseChannelMap map[string]<-chan *ic.InterContainerMessage
97 lockTopicResponseChannelMap sync.RWMutex
98
99 // This map is used to map a transaction to a consumers channel. This is used whenever a request has been
100 // sent out and we are waiting for a response.
101 transactionIdToChannelMap map[string]*transactionChannel
102 lockTransactionIdToChannelMap sync.RWMutex
103}
104
npujarec5762e2020-01-01 14:08:48 +0530105type InterContainerProxyOption func(*interContainerProxy)
William Kurkianea869482019-04-09 15:16:11 -0400106
107func InterContainerHost(host string) InterContainerProxyOption {
npujarec5762e2020-01-01 14:08:48 +0530108 return func(args *interContainerProxy) {
William Kurkianea869482019-04-09 15:16:11 -0400109 args.kafkaHost = host
110 }
111}
112
113func InterContainerPort(port int) InterContainerProxyOption {
npujarec5762e2020-01-01 14:08:48 +0530114 return func(args *interContainerProxy) {
William Kurkianea869482019-04-09 15:16:11 -0400115 args.kafkaPort = port
116 }
117}
118
119func DefaultTopic(topic *Topic) InterContainerProxyOption {
npujarec5762e2020-01-01 14:08:48 +0530120 return func(args *interContainerProxy) {
121 args.defaultTopic = topic
William Kurkianea869482019-04-09 15:16:11 -0400122 }
123}
124
125func DeviceDiscoveryTopic(topic *Topic) InterContainerProxyOption {
npujarec5762e2020-01-01 14:08:48 +0530126 return func(args *interContainerProxy) {
William Kurkianea869482019-04-09 15:16:11 -0400127 args.deviceDiscoveryTopic = topic
128 }
129}
130
131func RequestHandlerInterface(handler interface{}) InterContainerProxyOption {
npujarec5762e2020-01-01 14:08:48 +0530132 return func(args *interContainerProxy) {
William Kurkianea869482019-04-09 15:16:11 -0400133 args.defaultRequestHandlerInterface = handler
134 }
135}
136
137func MsgClient(client Client) InterContainerProxyOption {
npujarec5762e2020-01-01 14:08:48 +0530138 return func(args *interContainerProxy) {
William Kurkianea869482019-04-09 15:16:11 -0400139 args.kafkaClient = client
140 }
141}
142
npujarec5762e2020-01-01 14:08:48 +0530143func newInterContainerProxy(opts ...InterContainerProxyOption) *interContainerProxy {
144 proxy := &interContainerProxy{
William Kurkianea869482019-04-09 15:16:11 -0400145 kafkaHost: DefaultKafkaHost,
146 kafkaPort: DefaultKafkaPort,
npujarec5762e2020-01-01 14:08:48 +0530147 doneCh: make(chan struct{}),
William Kurkianea869482019-04-09 15:16:11 -0400148 }
149
150 for _, option := range opts {
151 option(proxy)
152 }
153
npujarec5762e2020-01-01 14:08:48 +0530154 return proxy
William Kurkianea869482019-04-09 15:16:11 -0400155}
156
npujarec5762e2020-01-01 14:08:48 +0530157func NewInterContainerProxy(opts ...InterContainerProxyOption) InterContainerProxy {
158 return newInterContainerProxy(opts...)
159}
160
161func (kp *interContainerProxy) Start() error {
Esin Karamanccb714b2019-11-29 15:02:06 +0000162 logger.Info("Starting-Proxy")
William Kurkianea869482019-04-09 15:16:11 -0400163
164 // Kafka MsgClient should already have been created. If not, output fatal error
165 if kp.kafkaClient == nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000166 logger.Fatal("kafka-client-not-set")
William Kurkianea869482019-04-09 15:16:11 -0400167 }
168
William Kurkianea869482019-04-09 15:16:11 -0400169 // Start the kafka client
170 if err := kp.kafkaClient.Start(); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000171 logger.Errorw("Cannot-create-kafka-proxy", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400172 return err
173 }
174
175 // Create the topic to response channel map
176 kp.topicToResponseChannelMap = make(map[string]<-chan *ic.InterContainerMessage)
177 //
178 // Create the transactionId to Channel Map
179 kp.transactionIdToChannelMap = make(map[string]*transactionChannel)
180
181 // Create the topic to request channel map
182 kp.topicToRequestHandlerChannelMap = make(map[string]*requestHandlerChannel)
183
184 return nil
185}
186
npujarec5762e2020-01-01 14:08:48 +0530187func (kp *interContainerProxy) Stop() {
Esin Karamanccb714b2019-11-29 15:02:06 +0000188 logger.Info("stopping-intercontainer-proxy")
npujarec5762e2020-01-01 14:08:48 +0530189 kp.doneOnce.Do(func() { close(kp.doneCh) })
William Kurkianea869482019-04-09 15:16:11 -0400190 // TODO : Perform cleanup
191 kp.kafkaClient.Stop()
192 //kp.deleteAllTopicRequestHandlerChannelMap()
193 //kp.deleteAllTopicResponseChannelMap()
194 //kp.deleteAllTransactionIdToChannelMap()
195}
196
npujarec5762e2020-01-01 14:08:48 +0530197func (kp *interContainerProxy) GetDefaultTopic() *Topic {
198 return kp.defaultTopic
199}
200
William Kurkianea869482019-04-09 15:16:11 -0400201// DeviceDiscovered publish the discovered device onto the kafka messaging bus
npujarec5762e2020-01-01 14:08:48 +0530202func (kp *interContainerProxy) DeviceDiscovered(deviceId string, deviceType string, parentId string, publisher string) error {
Esin Karamanccb714b2019-11-29 15:02:06 +0000203 logger.Debugw("sending-device-discovery-msg", log.Fields{"deviceId": deviceId})
William Kurkianea869482019-04-09 15:16:11 -0400204 // Simple validation
205 if deviceId == "" || deviceType == "" {
Esin Karamanccb714b2019-11-29 15:02:06 +0000206 logger.Errorw("invalid-parameters", log.Fields{"id": deviceId, "type": deviceType})
William Kurkianea869482019-04-09 15:16:11 -0400207 return errors.New("invalid-parameters")
208 }
209 // Create the device discovery message
210 header := &ic.Header{
211 Id: uuid.New().String(),
212 Type: ic.MessageType_DEVICE_DISCOVERED,
npujarec5762e2020-01-01 14:08:48 +0530213 FromTopic: kp.defaultTopic.Name,
William Kurkianea869482019-04-09 15:16:11 -0400214 ToTopic: kp.deviceDiscoveryTopic.Name,
215 Timestamp: time.Now().UnixNano(),
216 }
217 body := &ic.DeviceDiscovered{
218 Id: deviceId,
219 DeviceType: deviceType,
220 ParentId: parentId,
221 Publisher: publisher,
222 }
223
224 var marshalledData *any.Any
225 var err error
226 if marshalledData, err = ptypes.MarshalAny(body); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000227 logger.Errorw("cannot-marshal-request", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400228 return err
229 }
230 msg := &ic.InterContainerMessage{
231 Header: header,
232 Body: marshalledData,
233 }
234
235 // Send the message
236 if err := kp.kafkaClient.Send(msg, kp.deviceDiscoveryTopic); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000237 logger.Errorw("cannot-send-device-discovery-message", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400238 return err
239 }
240 return nil
241}
242
243// InvokeRPC is used to send a request to a given topic
npujarec5762e2020-01-01 14:08:48 +0530244func (kp *interContainerProxy) InvokeRPC(ctx context.Context, rpc string, toTopic *Topic, replyToTopic *Topic,
William Kurkianea869482019-04-09 15:16:11 -0400245 waitForResponse bool, key string, kvArgs ...*KVArg) (bool, *any.Any) {
246
247 // If a replyToTopic is provided then we use it, otherwise just use the default toTopic. The replyToTopic is
248 // typically the device ID.
249 responseTopic := replyToTopic
250 if responseTopic == nil {
npujarec5762e2020-01-01 14:08:48 +0530251 responseTopic = kp.defaultTopic
William Kurkianea869482019-04-09 15:16:11 -0400252 }
253
254 // Encode the request
255 protoRequest, err := encodeRequest(rpc, toTopic, responseTopic, key, kvArgs...)
256 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000257 logger.Warnw("cannot-format-request", log.Fields{"rpc": rpc, "error": err})
William Kurkianea869482019-04-09 15:16:11 -0400258 return false, nil
259 }
260
261 // Subscribe for response, if needed, before sending request
262 var ch <-chan *ic.InterContainerMessage
263 if waitForResponse {
264 var err error
265 if ch, err = kp.subscribeForResponse(*responseTopic, protoRequest.Header.Id); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000266 logger.Errorw("failed-to-subscribe-for-response", log.Fields{"error": err, "toTopic": toTopic.Name})
William Kurkianea869482019-04-09 15:16:11 -0400267 }
268 }
269
270 // Send request - if the topic is formatted with a device Id then we will send the request using a
271 // specific key, hence ensuring a single partition is used to publish the request. This ensures that the
272 // subscriber on that topic will receive the request in the order it was sent. The key used is the deviceId.
273 //key := GetDeviceIdFromTopic(*toTopic)
Esin Karamanccb714b2019-11-29 15:02:06 +0000274 logger.Debugw("sending-msg", log.Fields{"rpc": rpc, "toTopic": toTopic, "replyTopic": responseTopic, "key": key, "xId": protoRequest.Header.Id})
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000275 go func() {
276 if err := kp.kafkaClient.Send(protoRequest, toTopic, key); err != nil {
277 logger.Errorw("send-failed", log.Fields{
278 "topic": toTopic,
279 "key": key,
280 "error": err})
281 }
282 }()
William Kurkianea869482019-04-09 15:16:11 -0400283
284 if waitForResponse {
285 // Create a child context based on the parent context, if any
286 var cancel context.CancelFunc
287 childCtx := context.Background()
288 if ctx == nil {
289 ctx, cancel = context.WithTimeout(context.Background(), DefaultRequestTimeout*time.Millisecond)
290 } else {
291 childCtx, cancel = context.WithTimeout(ctx, DefaultRequestTimeout*time.Millisecond)
292 }
293 defer cancel()
294
295 // Wait for response as well as timeout or cancellation
296 // Remove the subscription for a response on return
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000297 defer func() {
298 if err := kp.unSubscribeForResponse(protoRequest.Header.Id); err != nil {
299 logger.Errorw("response-unsubscribe-failed", log.Fields{
300 "id": protoRequest.Header.Id,
301 "error": err})
302 }
303 }()
William Kurkianea869482019-04-09 15:16:11 -0400304 select {
305 case msg, ok := <-ch:
306 if !ok {
Esin Karamanccb714b2019-11-29 15:02:06 +0000307 logger.Warnw("channel-closed", log.Fields{"rpc": rpc, "replyTopic": replyToTopic.Name})
William Kurkianea869482019-04-09 15:16:11 -0400308 protoError := &ic.Error{Reason: "channel-closed"}
309 var marshalledArg *any.Any
310 if marshalledArg, err = ptypes.MarshalAny(protoError); err != nil {
311 return false, nil // Should never happen
312 }
313 return false, marshalledArg
314 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000315 logger.Debugw("received-response", log.Fields{"rpc": rpc, "msgHeader": msg.Header})
William Kurkianea869482019-04-09 15:16:11 -0400316 var responseBody *ic.InterContainerResponseBody
317 var err error
318 if responseBody, err = decodeResponse(msg); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000319 logger.Errorw("decode-response-error", log.Fields{"error": err})
npujarec5762e2020-01-01 14:08:48 +0530320 // FIXME we should return something
William Kurkianea869482019-04-09 15:16:11 -0400321 }
322 return responseBody.Success, responseBody.Result
323 case <-ctx.Done():
Esin Karamanccb714b2019-11-29 15:02:06 +0000324 logger.Debugw("context-cancelled", log.Fields{"rpc": rpc, "ctx": ctx.Err()})
William Kurkianea869482019-04-09 15:16:11 -0400325 // pack the error as proto any type
npujarec5762e2020-01-01 14:08:48 +0530326 protoError := &ic.Error{Reason: ctx.Err().Error(), Code: ic.ErrorCode_DEADLINE_EXCEEDED}
327
William Kurkianea869482019-04-09 15:16:11 -0400328 var marshalledArg *any.Any
329 if marshalledArg, err = ptypes.MarshalAny(protoError); err != nil {
330 return false, nil // Should never happen
331 }
332 return false, marshalledArg
333 case <-childCtx.Done():
Esin Karamanccb714b2019-11-29 15:02:06 +0000334 logger.Debugw("context-cancelled", log.Fields{"rpc": rpc, "ctx": childCtx.Err()})
William Kurkianea869482019-04-09 15:16:11 -0400335 // pack the error as proto any type
npujarec5762e2020-01-01 14:08:48 +0530336 protoError := &ic.Error{Reason: childCtx.Err().Error(), Code: ic.ErrorCode_DEADLINE_EXCEEDED}
337
William Kurkianea869482019-04-09 15:16:11 -0400338 var marshalledArg *any.Any
339 if marshalledArg, err = ptypes.MarshalAny(protoError); err != nil {
340 return false, nil // Should never happen
341 }
342 return false, marshalledArg
343 case <-kp.doneCh:
Esin Karamanccb714b2019-11-29 15:02:06 +0000344 logger.Infow("received-exit-signal", log.Fields{"toTopic": toTopic.Name, "rpc": rpc})
William Kurkianea869482019-04-09 15:16:11 -0400345 return true, nil
346 }
347 }
348 return true, nil
349}
350
351// SubscribeWithRequestHandlerInterface allows a caller to assign a target object to be invoked automatically
352// when a message is received on a given topic
npujarec5762e2020-01-01 14:08:48 +0530353func (kp *interContainerProxy) SubscribeWithRequestHandlerInterface(topic Topic, handler interface{}) error {
William Kurkianea869482019-04-09 15:16:11 -0400354
355 // Subscribe to receive messages for that topic
356 var ch <-chan *ic.InterContainerMessage
357 var err error
358 if ch, err = kp.kafkaClient.Subscribe(&topic); err != nil {
359 //if ch, err = kp.Subscribe(topic); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000360 logger.Errorw("failed-to-subscribe", log.Fields{"error": err, "topic": topic.Name})
Matt Jeannereteb5059f2019-07-19 06:11:00 -0400361 return err
William Kurkianea869482019-04-09 15:16:11 -0400362 }
363
364 kp.defaultRequestHandlerInterface = handler
365 kp.addToTopicRequestHandlerChannelMap(topic.Name, &requestHandlerChannel{requesthandlerInterface: handler, ch: ch})
366 // Launch a go routine to receive and process kafka messages
367 go kp.waitForMessages(ch, topic, handler)
368
369 return nil
370}
371
372// SubscribeWithDefaultRequestHandler allows a caller to add a topic to an existing target object to be invoked automatically
373// when a message is received on a given topic. So far there is only 1 target registered per microservice
npujarec5762e2020-01-01 14:08:48 +0530374func (kp *interContainerProxy) SubscribeWithDefaultRequestHandler(topic Topic, initialOffset int64) error {
William Kurkianea869482019-04-09 15:16:11 -0400375 // Subscribe to receive messages for that topic
376 var ch <-chan *ic.InterContainerMessage
377 var err error
378 if ch, err = kp.kafkaClient.Subscribe(&topic, &KVArg{Key: Offset, Value: initialOffset}); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000379 logger.Errorw("failed-to-subscribe", log.Fields{"error": err, "topic": topic.Name})
William Kurkianea869482019-04-09 15:16:11 -0400380 return err
381 }
382 kp.addToTopicRequestHandlerChannelMap(topic.Name, &requestHandlerChannel{requesthandlerInterface: kp.defaultRequestHandlerInterface, ch: ch})
383
384 // Launch a go routine to receive and process kafka messages
385 go kp.waitForMessages(ch, topic, kp.defaultRequestHandlerInterface)
386
387 return nil
388}
389
npujarec5762e2020-01-01 14:08:48 +0530390func (kp *interContainerProxy) UnSubscribeFromRequestHandler(topic Topic) error {
William Kurkianea869482019-04-09 15:16:11 -0400391 return kp.deleteFromTopicRequestHandlerChannelMap(topic.Name)
392}
393
npujarec5762e2020-01-01 14:08:48 +0530394func (kp *interContainerProxy) deleteFromTopicResponseChannelMap(topic string) error {
William Kurkianea869482019-04-09 15:16:11 -0400395 kp.lockTopicResponseChannelMap.Lock()
396 defer kp.lockTopicResponseChannelMap.Unlock()
397 if _, exist := kp.topicToResponseChannelMap[topic]; exist {
398 // Unsubscribe to this topic first - this will close the subscribed channel
399 var err error
400 if err = kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToResponseChannelMap[topic]); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000401 logger.Errorw("unsubscribing-error", log.Fields{"topic": topic})
William Kurkianea869482019-04-09 15:16:11 -0400402 }
403 delete(kp.topicToResponseChannelMap, topic)
404 return err
405 } else {
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000406 return fmt.Errorf("%s-Topic-not-found", topic)
William Kurkianea869482019-04-09 15:16:11 -0400407 }
408}
409
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000410// nolint: unused
npujarec5762e2020-01-01 14:08:48 +0530411func (kp *interContainerProxy) deleteAllTopicResponseChannelMap() error {
William Kurkianea869482019-04-09 15:16:11 -0400412 kp.lockTopicResponseChannelMap.Lock()
413 defer kp.lockTopicResponseChannelMap.Unlock()
414 var err error
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000415 for topic := range kp.topicToResponseChannelMap {
William Kurkianea869482019-04-09 15:16:11 -0400416 // Unsubscribe to this topic first - this will close the subscribed channel
417 if err = kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToResponseChannelMap[topic]); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000418 logger.Errorw("unsubscribing-error", log.Fields{"topic": topic, "error": err})
William Kurkianea869482019-04-09 15:16:11 -0400419 }
420 delete(kp.topicToResponseChannelMap, topic)
421 }
422 return err
423}
424
npujarec5762e2020-01-01 14:08:48 +0530425func (kp *interContainerProxy) addToTopicRequestHandlerChannelMap(topic string, arg *requestHandlerChannel) {
William Kurkianea869482019-04-09 15:16:11 -0400426 kp.lockTopicRequestHandlerChannelMap.Lock()
427 defer kp.lockTopicRequestHandlerChannelMap.Unlock()
428 if _, exist := kp.topicToRequestHandlerChannelMap[topic]; !exist {
429 kp.topicToRequestHandlerChannelMap[topic] = arg
430 }
431}
432
npujarec5762e2020-01-01 14:08:48 +0530433func (kp *interContainerProxy) deleteFromTopicRequestHandlerChannelMap(topic string) error {
William Kurkianea869482019-04-09 15:16:11 -0400434 kp.lockTopicRequestHandlerChannelMap.Lock()
435 defer kp.lockTopicRequestHandlerChannelMap.Unlock()
436 if _, exist := kp.topicToRequestHandlerChannelMap[topic]; exist {
437 // Close the kafka client client first by unsubscribing to this topic
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000438 if err := kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToRequestHandlerChannelMap[topic].ch); err != nil {
439 return err
440 }
William Kurkianea869482019-04-09 15:16:11 -0400441 delete(kp.topicToRequestHandlerChannelMap, topic)
442 return nil
443 } else {
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000444 return fmt.Errorf("%s-Topic-not-found", topic)
William Kurkianea869482019-04-09 15:16:11 -0400445 }
446}
447
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000448// nolint: unused
npujarec5762e2020-01-01 14:08:48 +0530449func (kp *interContainerProxy) deleteAllTopicRequestHandlerChannelMap() error {
William Kurkianea869482019-04-09 15:16:11 -0400450 kp.lockTopicRequestHandlerChannelMap.Lock()
451 defer kp.lockTopicRequestHandlerChannelMap.Unlock()
452 var err error
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000453 for topic := range kp.topicToRequestHandlerChannelMap {
William Kurkianea869482019-04-09 15:16:11 -0400454 // Close the kafka client client first by unsubscribing to this topic
455 if err = kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToRequestHandlerChannelMap[topic].ch); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000456 logger.Errorw("unsubscribing-error", log.Fields{"topic": topic, "error": err})
William Kurkianea869482019-04-09 15:16:11 -0400457 }
458 delete(kp.topicToRequestHandlerChannelMap, topic)
459 }
460 return err
461}
462
npujarec5762e2020-01-01 14:08:48 +0530463func (kp *interContainerProxy) addToTransactionIdToChannelMap(id string, topic *Topic, arg chan *ic.InterContainerMessage) {
William Kurkianea869482019-04-09 15:16:11 -0400464 kp.lockTransactionIdToChannelMap.Lock()
465 defer kp.lockTransactionIdToChannelMap.Unlock()
466 if _, exist := kp.transactionIdToChannelMap[id]; !exist {
467 kp.transactionIdToChannelMap[id] = &transactionChannel{topic: topic, ch: arg}
468 }
469}
470
npujarec5762e2020-01-01 14:08:48 +0530471func (kp *interContainerProxy) deleteFromTransactionIdToChannelMap(id string) {
William Kurkianea869482019-04-09 15:16:11 -0400472 kp.lockTransactionIdToChannelMap.Lock()
473 defer kp.lockTransactionIdToChannelMap.Unlock()
474 if transChannel, exist := kp.transactionIdToChannelMap[id]; exist {
475 // Close the channel first
476 close(transChannel.ch)
477 delete(kp.transactionIdToChannelMap, id)
478 }
479}
480
npujarec5762e2020-01-01 14:08:48 +0530481func (kp *interContainerProxy) deleteTopicTransactionIdToChannelMap(id string) {
William Kurkianea869482019-04-09 15:16:11 -0400482 kp.lockTransactionIdToChannelMap.Lock()
483 defer kp.lockTransactionIdToChannelMap.Unlock()
484 for key, value := range kp.transactionIdToChannelMap {
485 if value.topic.Name == id {
486 close(value.ch)
487 delete(kp.transactionIdToChannelMap, key)
488 }
489 }
490}
491
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000492// nolint: unused
npujarec5762e2020-01-01 14:08:48 +0530493func (kp *interContainerProxy) deleteAllTransactionIdToChannelMap() {
William Kurkianea869482019-04-09 15:16:11 -0400494 kp.lockTransactionIdToChannelMap.Lock()
495 defer kp.lockTransactionIdToChannelMap.Unlock()
496 for key, value := range kp.transactionIdToChannelMap {
497 close(value.ch)
498 delete(kp.transactionIdToChannelMap, key)
499 }
500}
501
npujarec5762e2020-01-01 14:08:48 +0530502func (kp *interContainerProxy) DeleteTopic(topic Topic) error {
William Kurkianea869482019-04-09 15:16:11 -0400503 // If we have any consumers on that topic we need to close them
504 if err := kp.deleteFromTopicResponseChannelMap(topic.Name); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000505 logger.Errorw("delete-from-topic-responsechannelmap-failed", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400506 }
507 if err := kp.deleteFromTopicRequestHandlerChannelMap(topic.Name); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000508 logger.Errorw("delete-from-topic-requesthandlerchannelmap-failed", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400509 }
510 kp.deleteTopicTransactionIdToChannelMap(topic.Name)
511
512 return kp.kafkaClient.DeleteTopic(&topic)
513}
514
515func encodeReturnedValue(returnedVal interface{}) (*any.Any, error) {
516 // Encode the response argument - needs to be a proto message
517 if returnedVal == nil {
518 return nil, nil
519 }
520 protoValue, ok := returnedVal.(proto.Message)
521 if !ok {
Esin Karamanccb714b2019-11-29 15:02:06 +0000522 logger.Warnw("response-value-not-proto-message", log.Fields{"error": ok, "returnVal": returnedVal})
William Kurkianea869482019-04-09 15:16:11 -0400523 err := errors.New("response-value-not-proto-message")
524 return nil, err
525 }
526
527 // Marshal the returned value, if any
528 var marshalledReturnedVal *any.Any
529 var err error
530 if marshalledReturnedVal, err = ptypes.MarshalAny(protoValue); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000531 logger.Warnw("cannot-marshal-returned-val", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400532 return nil, err
533 }
534 return marshalledReturnedVal, nil
535}
536
537func encodeDefaultFailedResponse(request *ic.InterContainerMessage) *ic.InterContainerMessage {
538 responseHeader := &ic.Header{
539 Id: request.Header.Id,
540 Type: ic.MessageType_RESPONSE,
541 FromTopic: request.Header.ToTopic,
542 ToTopic: request.Header.FromTopic,
npujarec5762e2020-01-01 14:08:48 +0530543 Timestamp: time.Now().UnixNano(),
William Kurkianea869482019-04-09 15:16:11 -0400544 }
545 responseBody := &ic.InterContainerResponseBody{
546 Success: false,
547 Result: nil,
548 }
549 var marshalledResponseBody *any.Any
550 var err error
551 // Error should never happen here
552 if marshalledResponseBody, err = ptypes.MarshalAny(responseBody); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000553 logger.Warnw("cannot-marshal-failed-response-body", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400554 }
555
556 return &ic.InterContainerMessage{
557 Header: responseHeader,
558 Body: marshalledResponseBody,
559 }
560
561}
562
563//formatRequest formats a request to send over kafka and returns an InterContainerMessage message on success
564//or an error on failure
565func encodeResponse(request *ic.InterContainerMessage, success bool, returnedValues ...interface{}) (*ic.InterContainerMessage, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +0000566 //logger.Debugw("encodeResponse", log.Fields{"success": success, "returnedValues": returnedValues})
William Kurkianea869482019-04-09 15:16:11 -0400567 responseHeader := &ic.Header{
568 Id: request.Header.Id,
569 Type: ic.MessageType_RESPONSE,
570 FromTopic: request.Header.ToTopic,
571 ToTopic: request.Header.FromTopic,
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400572 KeyTopic: request.Header.KeyTopic,
William Kurkianea869482019-04-09 15:16:11 -0400573 Timestamp: time.Now().UnixNano(),
574 }
575
576 // Go over all returned values
577 var marshalledReturnedVal *any.Any
578 var err error
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000579
580 // for now we support only 1 returned value - (excluding the error)
581 if len(returnedValues) > 0 {
582 if marshalledReturnedVal, err = encodeReturnedValue(returnedValues[0]); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000583 logger.Warnw("cannot-marshal-response-body", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400584 }
William Kurkianea869482019-04-09 15:16:11 -0400585 }
586
587 responseBody := &ic.InterContainerResponseBody{
588 Success: success,
589 Result: marshalledReturnedVal,
590 }
591
592 // Marshal the response body
593 var marshalledResponseBody *any.Any
594 if marshalledResponseBody, err = ptypes.MarshalAny(responseBody); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000595 logger.Warnw("cannot-marshal-response-body", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400596 return nil, err
597 }
598
599 return &ic.InterContainerMessage{
600 Header: responseHeader,
601 Body: marshalledResponseBody,
602 }, nil
603}
604
605func CallFuncByName(myClass interface{}, funcName string, params ...interface{}) (out []reflect.Value, err error) {
606 myClassValue := reflect.ValueOf(myClass)
607 // Capitalize the first letter in the funcName to workaround the first capital letters required to
608 // invoke a function from a different package
609 funcName = strings.Title(funcName)
610 m := myClassValue.MethodByName(funcName)
611 if !m.IsValid() {
612 return make([]reflect.Value, 0), fmt.Errorf("method-not-found \"%s\"", funcName)
613 }
614 in := make([]reflect.Value, len(params))
615 for i, param := range params {
616 in[i] = reflect.ValueOf(param)
617 }
618 out = m.Call(in)
619 return
620}
621
npujarec5762e2020-01-01 14:08:48 +0530622func (kp *interContainerProxy) addTransactionId(transactionId string, currentArgs []*ic.Argument) []*ic.Argument {
William Kurkianea869482019-04-09 15:16:11 -0400623 arg := &KVArg{
624 Key: TransactionKey,
625 Value: &ic.StrType{Val: transactionId},
626 }
627
628 var marshalledArg *any.Any
629 var err error
630 if marshalledArg, err = ptypes.MarshalAny(&ic.StrType{Val: transactionId}); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000631 logger.Warnw("cannot-add-transactionId", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400632 return currentArgs
633 }
634 protoArg := &ic.Argument{
635 Key: arg.Key,
636 Value: marshalledArg,
637 }
638 return append(currentArgs, protoArg)
639}
640
npujarec5762e2020-01-01 14:08:48 +0530641func (kp *interContainerProxy) addFromTopic(fromTopic string, currentArgs []*ic.Argument) []*ic.Argument {
William Kurkianea869482019-04-09 15:16:11 -0400642 var marshalledArg *any.Any
643 var err error
644 if marshalledArg, err = ptypes.MarshalAny(&ic.StrType{Val: fromTopic}); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000645 logger.Warnw("cannot-add-transactionId", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400646 return currentArgs
647 }
648 protoArg := &ic.Argument{
649 Key: FromTopic,
650 Value: marshalledArg,
651 }
652 return append(currentArgs, protoArg)
653}
654
npujarec5762e2020-01-01 14:08:48 +0530655func (kp *interContainerProxy) handleMessage(msg *ic.InterContainerMessage, targetInterface interface{}) {
William Kurkianea869482019-04-09 15:16:11 -0400656
657 // First extract the header to know whether this is a request - responses are handled by a different handler
658 if msg.Header.Type == ic.MessageType_REQUEST {
659 var out []reflect.Value
660 var err error
661
662 // Get the request body
663 requestBody := &ic.InterContainerRequestBody{}
664 if err = ptypes.UnmarshalAny(msg.Body, requestBody); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000665 logger.Warnw("cannot-unmarshal-request", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400666 } else {
Esin Karamanccb714b2019-11-29 15:02:06 +0000667 logger.Debugw("received-request", log.Fields{"rpc": requestBody.Rpc, "header": msg.Header})
William Kurkianea869482019-04-09 15:16:11 -0400668 // let the callee unpack the arguments as its the only one that knows the real proto type
669 // Augment the requestBody with the message Id as it will be used in scenarios where cores
670 // are set in pairs and competing
671 requestBody.Args = kp.addTransactionId(msg.Header.Id, requestBody.Args)
672
673 // Augment the requestBody with the From topic name as it will be used in scenarios where a container
674 // needs to send an unsollicited message to the currently requested container
675 requestBody.Args = kp.addFromTopic(msg.Header.FromTopic, requestBody.Args)
676
677 out, err = CallFuncByName(targetInterface, requestBody.Rpc, requestBody.Args)
678 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000679 logger.Warn(err)
William Kurkianea869482019-04-09 15:16:11 -0400680 }
681 }
682 // Response required?
683 if requestBody.ResponseRequired {
684 // If we already have an error before then just return that
685 var returnError *ic.Error
686 var returnedValues []interface{}
687 var success bool
688 if err != nil {
689 returnError = &ic.Error{Reason: err.Error()}
690 returnedValues = make([]interface{}, 1)
691 returnedValues[0] = returnError
692 } else {
693 returnedValues = make([]interface{}, 0)
694 // Check for errors first
695 lastIndex := len(out) - 1
696 if out[lastIndex].Interface() != nil { // Error
kdarapub26b4502019-10-05 03:02:33 +0530697 if retError, ok := out[lastIndex].Interface().(error); ok {
698 if retError.Error() == ErrorTransactionNotAcquired.Error() {
Esin Karamanccb714b2019-11-29 15:02:06 +0000699 logger.Debugw("Ignoring request", log.Fields{"error": retError, "txId": msg.Header.Id})
kdarapub26b4502019-10-05 03:02:33 +0530700 return // Ignore - process is in competing mode and ignored transaction
701 }
702 returnError = &ic.Error{Reason: retError.Error()}
William Kurkianea869482019-04-09 15:16:11 -0400703 returnedValues = append(returnedValues, returnError)
704 } else { // Should never happen
705 returnError = &ic.Error{Reason: "incorrect-error-returns"}
706 returnedValues = append(returnedValues, returnError)
707 }
708 } else if len(out) == 2 && reflect.ValueOf(out[0].Interface()).IsValid() && reflect.ValueOf(out[0].Interface()).IsNil() {
Esin Karamanccb714b2019-11-29 15:02:06 +0000709 logger.Warnw("Unexpected response of (nil,nil)", log.Fields{"txId": msg.Header.Id})
kdarapub26b4502019-10-05 03:02:33 +0530710 return // Ignore - should not happen
William Kurkianea869482019-04-09 15:16:11 -0400711 } else { // Non-error case
712 success = true
713 for idx, val := range out {
Esin Karamanccb714b2019-11-29 15:02:06 +0000714 //logger.Debugw("returned-api-response-loop", log.Fields{"idx": idx, "val": val.Interface()})
William Kurkianea869482019-04-09 15:16:11 -0400715 if idx != lastIndex {
716 returnedValues = append(returnedValues, val.Interface())
717 }
718 }
719 }
720 }
721
722 var icm *ic.InterContainerMessage
723 if icm, err = encodeResponse(msg, success, returnedValues...); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000724 logger.Warnw("error-encoding-response-returning-failure-result", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400725 icm = encodeDefaultFailedResponse(msg)
726 }
727 // To preserve ordering of messages, all messages to a given topic are sent to the same partition
728 // by providing a message key. The key is encoded in the topic name. If the deviceId is not
729 // present then the key will be empty, hence all messages for a given topic will be sent to all
730 // partitions.
731 replyTopic := &Topic{Name: msg.Header.FromTopic}
732 key := msg.Header.KeyTopic
Esin Karamanccb714b2019-11-29 15:02:06 +0000733 logger.Debugw("sending-response-to-kafka", log.Fields{"rpc": requestBody.Rpc, "header": icm.Header, "key": key})
William Kurkianea869482019-04-09 15:16:11 -0400734 // TODO: handle error response.
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000735 go func() {
736 if err := kp.kafkaClient.Send(icm, replyTopic, key); err != nil {
737 logger.Errorw("send-reply-failed", log.Fields{
738 "topic": replyTopic,
739 "key": key,
740 "error": err})
741 }
742 }()
William Kurkianea869482019-04-09 15:16:11 -0400743 }
744 } else if msg.Header.Type == ic.MessageType_RESPONSE {
Esin Karamanccb714b2019-11-29 15:02:06 +0000745 logger.Debugw("response-received", log.Fields{"msg-header": msg.Header})
William Kurkianea869482019-04-09 15:16:11 -0400746 go kp.dispatchResponse(msg)
747 } else {
Esin Karamanccb714b2019-11-29 15:02:06 +0000748 logger.Warnw("unsupported-message-received", log.Fields{"msg-header": msg.Header})
William Kurkianea869482019-04-09 15:16:11 -0400749 }
750}
751
npujarec5762e2020-01-01 14:08:48 +0530752func (kp *interContainerProxy) waitForMessages(ch <-chan *ic.InterContainerMessage, topic Topic, targetInterface interface{}) {
William Kurkianea869482019-04-09 15:16:11 -0400753 // Wait for messages
754 for msg := range ch {
Esin Karamanccb714b2019-11-29 15:02:06 +0000755 //logger.Debugw("request-received", log.Fields{"msg": msg, "topic": topic.Name, "target": targetInterface})
William Kurkianea869482019-04-09 15:16:11 -0400756 go kp.handleMessage(msg, targetInterface)
757 }
758}
759
npujarec5762e2020-01-01 14:08:48 +0530760func (kp *interContainerProxy) dispatchResponse(msg *ic.InterContainerMessage) {
William Kurkianea869482019-04-09 15:16:11 -0400761 kp.lockTransactionIdToChannelMap.RLock()
762 defer kp.lockTransactionIdToChannelMap.RUnlock()
763 if _, exist := kp.transactionIdToChannelMap[msg.Header.Id]; !exist {
Esin Karamanccb714b2019-11-29 15:02:06 +0000764 logger.Debugw("no-waiting-channel", log.Fields{"transaction": msg.Header.Id})
William Kurkianea869482019-04-09 15:16:11 -0400765 return
766 }
767 kp.transactionIdToChannelMap[msg.Header.Id].ch <- msg
768}
769
770// subscribeForResponse allows a caller to subscribe to a given topic when waiting for a response.
771// This method is built to prevent all subscribers to receive all messages as is the case of the Subscribe
772// API. There is one response channel waiting for kafka messages before dispatching the message to the
773// corresponding waiting channel
npujarec5762e2020-01-01 14:08:48 +0530774func (kp *interContainerProxy) subscribeForResponse(topic Topic, trnsId string) (chan *ic.InterContainerMessage, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +0000775 logger.Debugw("subscribeForResponse", log.Fields{"topic": topic.Name, "trnsid": trnsId})
William Kurkianea869482019-04-09 15:16:11 -0400776
777 // Create a specific channel for this consumers. We cannot use the channel from the kafkaclient as it will
778 // broadcast any message for this topic to all channels waiting on it.
779 ch := make(chan *ic.InterContainerMessage)
780 kp.addToTransactionIdToChannelMap(trnsId, &topic, ch)
781
782 return ch, nil
783}
784
npujarec5762e2020-01-01 14:08:48 +0530785func (kp *interContainerProxy) unSubscribeForResponse(trnsId string) error {
Esin Karamanccb714b2019-11-29 15:02:06 +0000786 logger.Debugw("unsubscribe-for-response", log.Fields{"trnsId": trnsId})
William Kurkianea869482019-04-09 15:16:11 -0400787 kp.deleteFromTransactionIdToChannelMap(trnsId)
788 return nil
789}
790
npujarec5762e2020-01-01 14:08:48 +0530791func (kp *interContainerProxy) EnableLivenessChannel(enable bool) chan bool {
cbabu95f21522019-11-13 14:25:18 +0100792 return kp.kafkaClient.EnableLivenessChannel(enable)
793}
794
npujarec5762e2020-01-01 14:08:48 +0530795func (kp *interContainerProxy) EnableHealthinessChannel(enable bool) chan bool {
Scott Baker86fce9a2019-12-12 09:47:17 -0800796 return kp.kafkaClient.EnableHealthinessChannel(enable)
797}
798
npujarec5762e2020-01-01 14:08:48 +0530799func (kp *interContainerProxy) SendLiveness() error {
cbabu95f21522019-11-13 14:25:18 +0100800 return kp.kafkaClient.SendLiveness()
801}
802
William Kurkianea869482019-04-09 15:16:11 -0400803//formatRequest formats a request to send over kafka and returns an InterContainerMessage message on success
804//or an error on failure
805func encodeRequest(rpc string, toTopic *Topic, replyTopic *Topic, key string, kvArgs ...*KVArg) (*ic.InterContainerMessage, error) {
806 requestHeader := &ic.Header{
807 Id: uuid.New().String(),
808 Type: ic.MessageType_REQUEST,
809 FromTopic: replyTopic.Name,
810 ToTopic: toTopic.Name,
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400811 KeyTopic: key,
William Kurkianea869482019-04-09 15:16:11 -0400812 Timestamp: time.Now().UnixNano(),
813 }
814 requestBody := &ic.InterContainerRequestBody{
815 Rpc: rpc,
816 ResponseRequired: true,
817 ReplyToTopic: replyTopic.Name,
818 }
819
820 for _, arg := range kvArgs {
821 if arg == nil {
822 // In case the caller sends an array with empty args
823 continue
824 }
825 var marshalledArg *any.Any
826 var err error
827 // ascertain the value interface type is a proto.Message
828 protoValue, ok := arg.Value.(proto.Message)
829 if !ok {
Esin Karamanccb714b2019-11-29 15:02:06 +0000830 logger.Warnw("argument-value-not-proto-message", log.Fields{"error": ok, "Value": arg.Value})
William Kurkianea869482019-04-09 15:16:11 -0400831 err := errors.New("argument-value-not-proto-message")
832 return nil, err
833 }
834 if marshalledArg, err = ptypes.MarshalAny(protoValue); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000835 logger.Warnw("cannot-marshal-request", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400836 return nil, err
837 }
838 protoArg := &ic.Argument{
839 Key: arg.Key,
840 Value: marshalledArg,
841 }
842 requestBody.Args = append(requestBody.Args, protoArg)
843 }
844
845 var marshalledData *any.Any
846 var err error
847 if marshalledData, err = ptypes.MarshalAny(requestBody); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000848 logger.Warnw("cannot-marshal-request", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400849 return nil, err
850 }
851 request := &ic.InterContainerMessage{
852 Header: requestHeader,
853 Body: marshalledData,
854 }
855 return request, nil
856}
857
858func decodeResponse(response *ic.InterContainerMessage) (*ic.InterContainerResponseBody, error) {
859 // Extract the message body
860 responseBody := ic.InterContainerResponseBody{}
861 if err := ptypes.UnmarshalAny(response.Body, &responseBody); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000862 logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -0400863 return nil, err
864 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000865 //logger.Debugw("response-decoded-successfully", log.Fields{"response-status": &responseBody.Success})
William Kurkianea869482019-04-09 15:16:11 -0400866
867 return &responseBody, nil
868
869}