Add pub/sub to client/server and make broker more low level

This commit is contained in:
Asim
2015-06-12 19:52:27 +01:00
parent cdf2f2cbcd
commit b91af916f9
24 changed files with 542 additions and 193 deletions

View File

@@ -1,19 +1,32 @@
package client
import (
"github.com/myodc/go-micro/registry"
"github.com/myodc/go-micro/transport"
"golang.org/x/net/context"
)
type Client interface {
NewRequest(string, string, interface{}) Request
NewProtoRequest(string, string, interface{}) Request
NewJsonRequest(string, string, interface{}) Request
Call(context.Context, Request, interface{}) error
CallRemote(context.Context, string, Request, interface{}) error
Stream(context.Context, Request, interface{}) (Streamer, error)
StreamRemote(context.Context, string, Request, interface{}) (Streamer, error)
NewPublication(topic string, msg interface{}) Publication
NewRequest(service, method string, req interface{}) Request
NewProtoRequest(service, method string, req interface{}) Request
NewJsonRequest(service, method string, req interface{}) Request
Call(ctx context.Context, req Request, rsp interface{}) error
CallRemote(ctx context.Context, addr string, req Request, rsp interface{}) error
Stream(ctx context.Context, req Request, rspChan interface{}) (Streamer, error)
StreamRemote(ctx context.Context, addr string, req Request, rspChan interface{}) (Streamer, error)
Publish(ctx context.Context, p Publication) error
}
type Publication interface {
Topic() string
Message() interface{}
ContentType() string
}
type Request interface {
Service() string
Method() string
ContentType() string
Request() interface{}
}
type Streamer interface {
@@ -22,29 +35,12 @@ type Streamer interface {
Close() error
}
type options struct {
registry registry.Registry
transport transport.Transport
}
type Option func(*options)
var (
DefaultClient Client = newRpcClient()
)
func Registry(r registry.Registry) Option {
return func(o *options) {
o.registry = r
}
}
func Transport(t transport.Transport) Option {
return func(o *options) {
o.transport = t
}
}
func Call(ctx context.Context, request Request, response interface{}) error {
return DefaultClient.Call(ctx, request, response)
}
@@ -61,10 +57,18 @@ func StreamRemote(ctx context.Context, address string, request Request, response
return DefaultClient.StreamRemote(ctx, address, request, responseChan)
}
func Publish(ctx context.Context, p Publication) error {
return DefaultClient.Publish(ctx, p)
}
func NewClient(opt ...Option) Client {
return newRpcClient(opt...)
}
func NewPublication(topic string, message interface{}) Publication {
return DefaultClient.NewPublication(topic, message)
}
func NewRequest(service, method string, request interface{}) Request {
return DefaultClient.NewRequest(service, method, request)
}

31
client/options.go Normal file
View File

@@ -0,0 +1,31 @@
package client
import (
"github.com/myodc/go-micro/broker"
"github.com/myodc/go-micro/registry"
"github.com/myodc/go-micro/transport"
)
type options struct {
broker broker.Broker
registry registry.Registry
transport transport.Transport
}
func Broker(b broker.Broker) Option {
return func(o *options) {
o.broker = b
}
}
func Registry(r registry.Registry) Option {
return func(o *options) {
o.registry = r
}
}
func Transport(t transport.Transport) Option {
return func(o *options) {
o.transport = t
}
}

View File

@@ -1,8 +0,0 @@
package client
type Request interface {
Service() string
Method() string
ContentType() string
Request() interface{}
}

View File

@@ -1,11 +1,14 @@
package client
import (
"encoding/json"
"fmt"
"math/rand"
"net/http"
"sync"
"time"
"github.com/myodc/go-micro/broker"
c "github.com/myodc/go-micro/context"
"github.com/myodc/go-micro/errors"
"github.com/myodc/go-micro/registry"
@@ -13,6 +16,7 @@ import (
rpc "github.com/youtube/vitess/go/rpcplus"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
)
@@ -21,6 +25,7 @@ type headerRoundTripper struct {
}
type rpcClient struct {
once sync.Once
opts options
}
@@ -39,7 +44,14 @@ func newRpcClient(opt ...Option) Client {
opts.transport = transport.DefaultTransport
}
if opts.broker == nil {
opts.broker = broker.DefaultBroker
}
var once sync.Once
return &rpcClient{
once: once,
opts: opts,
}
}
@@ -152,6 +164,48 @@ func (r *rpcClient) Stream(ctx context.Context, request Request, responseChan in
return r.stream(ctx, address, request, responseChan)
}
func (r *rpcClient) Publish(ctx context.Context, p Publication) error {
md, ok := c.GetMetadata(ctx)
if !ok {
md = make(map[string]string)
}
md["Content-Type"] = p.ContentType()
// encode message body
var body []byte
switch p.ContentType() {
case "application/octet-stream":
b, err := proto.Marshal(p.Message().(proto.Message))
if err != nil {
return err
}
body = b
case "application/json":
b, err := json.Marshal(p.Message())
if err != nil {
return err
}
body = b
}
r.once.Do(func() {
r.opts.broker.Connect()
})
return r.opts.broker.Publish(p.Topic(), &broker.Message{
Header: md,
Body: body,
})
}
func (r *rpcClient) NewPublication(topic string, message interface{}) Publication {
return r.NewProtoPublication(topic, message)
}
func (r *rpcClient) NewProtoPublication(topic string, message interface{}) Publication {
return newRpcPublication(topic, message, "application/octet-stream")
}
func (r *rpcClient) NewRequest(service, method string, request interface{}) Request {
return r.NewProtoRequest(service, method, request)
}

27
client/rpc_publication.go Normal file
View File

@@ -0,0 +1,27 @@
package client
type rpcPublication struct {
topic string
contentType string
message interface{}
}
func newRpcPublication(topic string, message interface{}, contentType string) Publication {
return &rpcPublication{
message: message,
topic: topic,
contentType: contentType,
}
}
func (r *rpcPublication) ContentType() string {
return r.contentType
}
func (r *rpcPublication) Topic() string {
return r.topic
}
func (r *rpcPublication) Message() interface{} {
return r.message
}