Skip to content

optimize NATS message sending and receiving logic by using the nats.Msg struct instead of the original #2783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions transport/nats/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/nats-io/nats.go"
"go-micro.dev/v5/codec/json"
"go-micro.dev/v5/server"
"go-micro.dev/v5/transport"
)
Expand Down Expand Up @@ -61,8 +60,6 @@ var (
DefaultTimeout = time.Minute
)



func configure(n *ntport, opts ...transport.Option) {
for _, o := range opts {
o(&n.opts)
Expand Down Expand Up @@ -121,21 +118,28 @@ func (n *ntportClient) Remote() string {
}

func (n *ntportClient) Send(m *transport.Message) error {
b, err := n.opts.Codec.Marshal(m)
if err != nil {
return err
header := nats.Header{}
for k, v := range m.Header {
header.Add(k, v)
}

msg := &nats.Msg{
Subject: n.addr,
Reply: n.id,
Header: header,
Data: m.Body,
}

// no deadline
if n.opts.Timeout == time.Duration(0) {
return n.conn.PublishRequest(n.addr, n.id, b)
return n.conn.PublishMsg(msg)
}

// use the deadline
ch := make(chan error, 1)

go func() {
ch <- n.conn.PublishRequest(n.addr, n.id, b)
ch <- n.conn.PublishMsg(msg)
}()

select {
Expand All @@ -157,12 +161,13 @@ func (n *ntportClient) Recv(m *transport.Message) error {
return err
}

var mr transport.Message
if err := n.opts.Codec.Unmarshal(rsp.Data, &mr); err != nil {
return err
m.Header = make(map[string]string)
for k, v := range rsp.Header {
m.Header[k] = v[0]
}

*m = mr
m.Body = rsp.Data

return nil
}

Expand Down Expand Up @@ -213,28 +218,37 @@ func (n *ntportSocket) Recv(m *transport.Message) error {
}
n.Unlock()

if err := n.opts.Codec.Unmarshal(r.Data, m); err != nil {
return err
m.Header = make(map[string]string)
for k, v := range r.Header {
m.Header[k] = v[0]
}
m.Body = r.Data

return nil
}

func (n *ntportSocket) Send(m *transport.Message) error {
b, err := n.opts.Codec.Marshal(m)
if err != nil {
return err
header := nats.Header{}
for k, v := range m.Header {
header.Add(k, v)
}

msg := &nats.Msg{
Subject: n.m.Reply,
Header: header,
Data: m.Body,
}

// no deadline
if n.opts.Timeout == time.Duration(0) {
return n.conn.Publish(n.m.Reply, b)
return n.conn.PublishMsg(msg)
}

// use the deadline
ch := make(chan error, 1)

go func() {
ch <- n.conn.Publish(n.m.Reply, b)
ch <- n.conn.PublishMsg(msg)
}()

select {
Expand Down Expand Up @@ -435,8 +449,6 @@ func (n *ntport) String() string {

func NewTransport(opts ...transport.Option) transport.Transport {
options := transport.Options{
// Default codec
Codec: json.Marshaler{},
Timeout: DefaultTimeout,
Context: context.Background(),
}
Expand Down