github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/communication/nats/sender.go (about)

     1  /*
     2   * Copyright (C) 2017 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package nats
    19  
    20  import (
    21  	"time"
    22  
    23  	"github.com/mysteriumnetwork/node/communication"
    24  	"github.com/pkg/errors"
    25  	"github.com/rs/zerolog/log"
    26  )
    27  
    28  // NewSender constructs new Sender's instance which works thru NATS connection.
    29  // Codec packs/unpacks messages to byte payloads.
    30  // Topic (optional) if need to send messages prefixed topic.
    31  func NewSender(connection Connection, codec communication.Codec) *senderNATS {
    32  	return &senderNATS{
    33  		connection:     connection,
    34  		codec:          codec,
    35  		timeoutRequest: 10 * time.Second,
    36  	}
    37  }
    38  
    39  type senderNATS struct {
    40  	connection     Connection
    41  	codec          communication.Codec
    42  	timeoutRequest time.Duration
    43  	messageTopic   string
    44  }
    45  
    46  func (sender *senderNATS) Send(producer communication.MessageProducer) error {
    47  	messageEndpoint, err := producer.GetMessageEndpoint()
    48  	if err != nil {
    49  		return err
    50  	}
    51  	messageTopic := string(messageEndpoint)
    52  
    53  	messageData, err := sender.codec.Pack(producer.Produce())
    54  	if err != nil {
    55  		err = errors.Wrapf(err, "failed to encode message '%s'", messageTopic)
    56  		return err
    57  	}
    58  
    59  	log.WithLevel(levelFor(messageTopic)).Msgf("Message %q sending: %s", messageTopic, messageData)
    60  	err = sender.connection.Publish(messageTopic, messageData)
    61  	if err != nil {
    62  		err = errors.Wrapf(err, "failed to send message '%s'", messageTopic)
    63  		return err
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  func (sender *senderNATS) Request(producer communication.RequestProducer) (responsePtr interface{}, err error) {
    70  	requestEndpoint, err := producer.GetRequestEndpoint()
    71  	if err != nil {
    72  		return
    73  	}
    74  	requestTopic := string(requestEndpoint)
    75  	responsePtr = producer.NewResponse()
    76  
    77  	requestData, err := sender.codec.Pack(producer.Produce())
    78  	if err != nil {
    79  		err = errors.Wrapf(err, "failed to pack request '%s'", requestTopic)
    80  		return
    81  	}
    82  
    83  	log.WithLevel(levelFor(requestTopic)).Msgf("Request %q sending: %s", requestTopic, requestData)
    84  	msg, err := sender.connection.Request(requestTopic, requestData, sender.timeoutRequest)
    85  	if err != nil {
    86  		err = errors.Wrapf(err, "failed to send request '%s'", requestTopic)
    87  		return
    88  	}
    89  
    90  	log.Debug().Msgf("Received response for %q: %s", requestTopic, msg.Data)
    91  	err = sender.codec.Unpack(msg.Data, responsePtr)
    92  	if err != nil {
    93  		err = errors.Wrapf(err, "failed to unpack response '%s'", requestTopic)
    94  		log.Error().Err(err).Msg("")
    95  		return
    96  	}
    97  
    98  	return responsePtr, nil
    99  }