NATS transport#680
Conversation
|
Oh, cool.. ! I would want to also see Publisher type (corresponding to Client), can that be added? |
|
yes, sure! I am adding |
examples/stringsvc4/main.go
Outdated
| "flag" | ||
|
|
||
| "github.com/go-kit/kit/endpoint" | ||
| natstransport "github.com/kirooha/kit/transport/nats" |
There was a problem hiding this comment.
github.com/kirooha/kit/transport/nats - maybe need to change to github.com/go-kit/kit/transport/nats
transport/nats/publisher.go
Outdated
| } | ||
|
|
||
| if !p.async { | ||
| resp, err := p.publisher.Request(p.subject, msg.Data, p.timeout) |
There was a problem hiding this comment.
Since you already have a context you can use RequestWithContext instead and remove the timeout field.
transport/nats/publisher.go
Outdated
|
|
||
| } | ||
|
|
||
| if p.reply == "" { |
There was a problem hiding this comment.
You can just use PublishMsg and than remove the rest of the method.
transport/nats/encode_decode.go
Outdated
| type DecodeRequestFunc func(context.Context, *nats.Msg) (request interface{}, err error) | ||
|
|
||
|
|
||
| // EncodeRequestFunc encodes the passed request object into the HTTP request |
There was a problem hiding this comment.
This comment still talks about HTTP
There was a problem hiding this comment.
oh, great! thanks a lot! I didn't hope to so fast review) Actually, it was a draft and I supposed to modify comments and test code. One more time, thanks a lot! I will fix your remarks as soon as possible.
| // subscribers, after invoking the endpoint but prior to publishing a reply. | ||
| type SubscriberResponseFunc func(context.Context, *nats.Conn) context.Context | ||
|
|
||
| // ClientResponseFunc may take information from an HTTP request and make the |
transport/nats/subscriber.go
Outdated
| // ServeMsg provides nats.MsgHandler. | ||
| func (s Subscriber) ServeMsg(nc *nats.Conn) func(msg *nats.Msg) { | ||
| return func(msg *nats.Msg) { | ||
| ctx := context.Background() |
There was a problem hiding this comment.
The context is never cancelled
transport/nats/publisher.go
Outdated
| ctx = f(ctx, &msg) | ||
| } | ||
|
|
||
| if !p.async { |
There was a problem hiding this comment.
The endpoint tries to handle both simple publish operations and requests which feels kinda odd. Also the !async case ignores the p.reply topic.
I suggest to remove the async case and the custom reply topic for know and only do a simple Request(WithContext). We can always add support for messages without reply later through options or a different type.
transport/nats/publisher.go
Outdated
| reply: reply, | ||
| enc: enc, | ||
| dec: dec, | ||
| before: []RequestFunc{}, |
There was a problem hiding this comment.
No need to initialize this, Go already handles nil correct for all the cases needed.
transport/nats/subscriber.go
Outdated
| // ServeMsg provides nats.MsgHandler. | ||
| func (s Subscriber) ServeMsg(nc *nats.Conn) func(msg *nats.Msg) { | ||
| return func(msg *nats.Msg) { | ||
| ctx := context.Background() |
There was a problem hiding this comment.
ctx should be cancelled when the handler returns
transport/nats/publisher.go
Outdated
| return nil, err | ||
| } | ||
|
|
||
| return nil, nil |
There was a problem hiding this comment.
The funcs in p.after should probably still be called here and below with a nil *nats.Msg
|
@nussjustin all remarks are fixed |
nussjustin
left a comment
There was a problem hiding this comment.
1 small comment, but other than that the API looks good to me.
This needs some tests though ;-)
For testing publishers and subscribers you can start real NATS servers via the gnatsd/server package (see Server.Start and Server.ReadyForConnections).
| // SubscriberRequestFunc may take information from a publisher request and put it into a | ||
| // request context. In Subscribers, RequestFuncs are executed prior to invoking the | ||
| // endpoint. | ||
| type SubscriberRequestFunc func(context.Context, *nats.Msg) context.Context |
There was a problem hiding this comment.
Sorry, you were right. This should be just RequestFunc. I forgot that this is used for both Publishers and Subscribers. That's what I get for not reviewing my comments before requesting changes...
|
there's some weird behavior of ci in this pr. I did 3 force push without any changes (amend commit that added blank lines). 2 builds were failed and the latest is succeeded. I mean ci/circleci and continuous-integration/travis-ci/pr |
|
@nussjustin tests have been added |
transport/nats/publisher_test.go
Outdated
| "strings" | ||
|
|
||
| "github.com/nats-io/go-nats" | ||
| natstransport "github.com/kirooha/kit/transport/nats" |
transport/nats/subscriber_test.go
Outdated
| "github.com/nats-io/go-nats" | ||
| "github.com/nats-io/gnatsd/server" | ||
|
|
||
| natstransport "github.com/kirooha/kit/transport/nats" |
|
@nussjustin fixed wrong paths, squashed commits to the only one with a more detailed comment. do you have any notes? |
|
Could you please add a test for PublisherTimeout? From what I see it's the only publisher option that's not tested right now (as reported by Otherwise LGTM so far. |
|
done |
transport/nats/publisher_test.go
Outdated
| defer nc.Close() | ||
|
|
||
| sub, err := nc.QueueSubscribe("natstransport.test", "natstransport", func(msg *nats.Msg) { | ||
| ch := make(chan struct{}) |
There was a problem hiding this comment.
You're leaking the goroutine here. Even if this is just a test it shouldn't leak goroutines. Just create the channel before the QueueSubscribe and close it after the test, like this:
ch := make(chan struct{})
defer close(ch)
sub, err := nc.QueueSubscribe("natstransport.test", "natstransport", func(msg *nats.Msg) {
<-ch
})NATS is an open-source, cloud-native messaging system (https://www.nats.io). The functional provides API that lets one works with NATS in a similar way as HTTP. - nats.MsgHandler could be used in queue or simple subscriber - Sync publisher
|
thank you! awesome note, fixed |
nussjustin
left a comment
There was a problem hiding this comment.
I don't see anything obviously wrong / bad right now sooo... passing on the review torch to @peterbourgon and co.
|
Great, thank you. Give me a week or so; I'm on a work trip and have a vacation immediately afterwards. |
|
Yeah I really don't have any nits to pick here, this LGTM! Thanks very much for the hard work! |
NATS is an open-source, cloud-native messaging system (https://www.nats.io). The functional provides API that lets one works with NATS in a similar way as HTTP. - nats.MsgHandler could be used in queue or simple subscriber - Sync publisher
The PR contains transport for NATS (messaging system, https://nats.io)
I saw a request with the similar functional #623
Since the PR is still open, I want to suggest my PR.