Restructure go-micro layout and plugins
This commit is contained in:
@@ -27,7 +27,7 @@ type Subscriber interface {
|
||||
|
||||
type options struct{}
|
||||
|
||||
type Options func(*options)
|
||||
type Option func(*options)
|
||||
|
||||
var (
|
||||
Address string
|
||||
@@ -35,13 +35,17 @@ var (
|
||||
DefaultBroker Broker
|
||||
)
|
||||
|
||||
func NewBroker(addrs []string, opt ...Option) Broker {
|
||||
return newHttpBroker([]string{Address}, opt...)
|
||||
}
|
||||
|
||||
func Init() error {
|
||||
if len(Id) == 0 {
|
||||
Id = "broker-" + uuid.NewUUID().String()
|
||||
}
|
||||
|
||||
if DefaultBroker == nil {
|
||||
DefaultBroker = NewHttpBroker([]string{Address})
|
||||
DefaultBroker = newHttpBroker([]string{Address})
|
||||
}
|
||||
|
||||
return DefaultBroker.Init()
|
||||
|
11
broker/http/http.go
Normal file
11
broker/http/http.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package http
|
||||
|
||||
// This is a hack
|
||||
|
||||
import (
|
||||
"github.com/myodc/go-micro/broker"
|
||||
)
|
||||
|
||||
func NewBroker(addrs []string, opt ...broker.Option) broker.Broker {
|
||||
return broker.NewBroker(addrs, opt...)
|
||||
}
|
@@ -21,39 +21,54 @@ import (
|
||||
"github.com/myodc/go-micro/registry"
|
||||
)
|
||||
|
||||
type HttpBroker struct {
|
||||
type httpBroker struct {
|
||||
id string
|
||||
address string
|
||||
unsubscribe chan *HttpSubscriber
|
||||
unsubscribe chan *httpSubscriber
|
||||
|
||||
sync.RWMutex
|
||||
subscribers map[string][]*HttpSubscriber
|
||||
subscribers map[string][]*httpSubscriber
|
||||
running bool
|
||||
exit chan chan error
|
||||
}
|
||||
|
||||
type HttpSubscriber struct {
|
||||
type httpSubscriber struct {
|
||||
id string
|
||||
topic string
|
||||
ch chan *HttpSubscriber
|
||||
ch chan *httpSubscriber
|
||||
fn func(*Message)
|
||||
svc registry.Service
|
||||
}
|
||||
|
||||
var (
|
||||
SubPath = "/_sub"
|
||||
DefaultSubPath = "/_sub"
|
||||
)
|
||||
|
||||
func (h *HttpSubscriber) Topic() string {
|
||||
func newHttpBroker(addrs []string, opt ...Option) Broker {
|
||||
addr := ":0"
|
||||
if len(addrs) > 0 {
|
||||
addr = addrs[0]
|
||||
}
|
||||
|
||||
return &httpBroker{
|
||||
id: Id,
|
||||
address: addr,
|
||||
subscribers: make(map[string][]*httpSubscriber),
|
||||
unsubscribe: make(chan *httpSubscriber),
|
||||
exit: make(chan chan error),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *httpSubscriber) Topic() string {
|
||||
return h.topic
|
||||
}
|
||||
|
||||
func (h *HttpSubscriber) Unsubscribe() error {
|
||||
func (h *httpSubscriber) Unsubscribe() error {
|
||||
h.ch <- h
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *HttpBroker) start() error {
|
||||
func (h *httpBroker) start() error {
|
||||
h.Lock()
|
||||
defer h.Unlock()
|
||||
|
||||
@@ -87,7 +102,7 @@ func (h *HttpBroker) start() error {
|
||||
h.stop()
|
||||
case subscriber := <-h.unsubscribe:
|
||||
h.Lock()
|
||||
var subscribers []*HttpSubscriber
|
||||
var subscribers []*httpSubscriber
|
||||
for _, sub := range h.subscribers[subscriber.topic] {
|
||||
if sub.id == subscriber.id {
|
||||
registry.Deregister(sub.svc)
|
||||
@@ -104,13 +119,13 @@ func (h *HttpBroker) start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *HttpBroker) stop() error {
|
||||
func (h *httpBroker) stop() error {
|
||||
ch := make(chan error)
|
||||
h.exit <- ch
|
||||
return <-ch
|
||||
}
|
||||
|
||||
func (h *HttpBroker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
func (h *httpBroker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != "POST" {
|
||||
err := errors.BadRequest("go.micro.broker", "Method not allowed")
|
||||
http.Error(w, err.Error(), http.StatusMethodNotAllowed)
|
||||
@@ -148,28 +163,28 @@ func (h *HttpBroker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
h.RUnlock()
|
||||
}
|
||||
|
||||
func (h *HttpBroker) Address() string {
|
||||
func (h *httpBroker) Address() string {
|
||||
return h.address
|
||||
}
|
||||
|
||||
func (h *HttpBroker) Connect() error {
|
||||
func (h *httpBroker) Connect() error {
|
||||
return h.start()
|
||||
}
|
||||
|
||||
func (h *HttpBroker) Disconnect() error {
|
||||
func (h *httpBroker) Disconnect() error {
|
||||
return h.stop()
|
||||
}
|
||||
|
||||
func (h *HttpBroker) Init() error {
|
||||
func (h *httpBroker) Init() error {
|
||||
if len(h.id) == 0 {
|
||||
h.id = "broker-" + uuid.NewUUID().String()
|
||||
}
|
||||
|
||||
http.Handle(SubPath, h)
|
||||
http.Handle(DefaultSubPath, h)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *HttpBroker) Publish(topic string, data []byte) error {
|
||||
func (h *httpBroker) Publish(topic string, data []byte) error {
|
||||
s, err := registry.GetService("topic:" + topic)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -186,7 +201,7 @@ func (h *HttpBroker) Publish(topic string, data []byte) error {
|
||||
}
|
||||
|
||||
for _, node := range s.Nodes() {
|
||||
r, err := http.Post(fmt.Sprintf("http://%s:%d%s", node.Address(), node.Port(), SubPath), "application/json", bytes.NewBuffer(b))
|
||||
r, err := http.Post(fmt.Sprintf("http://%s:%d%s", node.Address(), node.Port(), DefaultSubPath), "application/json", bytes.NewBuffer(b))
|
||||
if err == nil {
|
||||
r.Body.Close()
|
||||
}
|
||||
@@ -195,7 +210,7 @@ func (h *HttpBroker) Publish(topic string, data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *HttpBroker) Subscribe(topic string, function func(*Message)) (Subscriber, error) {
|
||||
func (h *httpBroker) Subscribe(topic string, function func(*Message)) (Subscriber, error) {
|
||||
// parse address for host, port
|
||||
parts := strings.Split(h.Address(), ":")
|
||||
host := strings.Join(parts[:len(parts)-1], ":")
|
||||
@@ -205,7 +220,7 @@ func (h *HttpBroker) Subscribe(topic string, function func(*Message)) (Subscribe
|
||||
node := registry.NewNode(h.id, host, port)
|
||||
service := registry.NewService("topic:"+topic, node)
|
||||
|
||||
subscriber := &HttpSubscriber{
|
||||
subscriber := &httpSubscriber{
|
||||
id: uuid.NewUUID().String(),
|
||||
topic: topic,
|
||||
ch: h.unsubscribe,
|
||||
@@ -224,18 +239,3 @@ func (h *HttpBroker) Subscribe(topic string, function func(*Message)) (Subscribe
|
||||
|
||||
return subscriber, nil
|
||||
}
|
||||
|
||||
func NewHttpBroker(addrs []string, opts ...Options) Broker {
|
||||
addr := ":0"
|
||||
if len(addrs) > 0 {
|
||||
addr = addrs[0]
|
||||
}
|
||||
|
||||
return &HttpBroker{
|
||||
id: Id,
|
||||
address: addr,
|
||||
subscribers: make(map[string][]*HttpSubscriber),
|
||||
unsubscribe: make(chan *HttpSubscriber),
|
||||
exit: make(chan chan error),
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package broker
|
||||
package nats
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -7,33 +7,34 @@ import (
|
||||
|
||||
"code.google.com/p/go-uuid/uuid"
|
||||
"github.com/apcera/nats"
|
||||
"github.com/myodc/go-micro/broker"
|
||||
)
|
||||
|
||||
type NatsBroker struct {
|
||||
type nbroker struct {
|
||||
addrs []string
|
||||
conn *nats.Conn
|
||||
}
|
||||
|
||||
type NatsSubscriber struct {
|
||||
type subscriber struct {
|
||||
s *nats.Subscription
|
||||
}
|
||||
|
||||
func (n *NatsSubscriber) Topic() string {
|
||||
func (n *subscriber) Topic() string {
|
||||
return n.s.Subject
|
||||
}
|
||||
|
||||
func (n *NatsSubscriber) Unsubscribe() error {
|
||||
func (n *subscriber) Unsubscribe() error {
|
||||
return n.s.Unsubscribe()
|
||||
}
|
||||
|
||||
func (n *NatsBroker) Address() string {
|
||||
func (n *nbroker) Address() string {
|
||||
if len(n.addrs) > 0 {
|
||||
return n.addrs[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (n *NatsBroker) Connect() error {
|
||||
func (n *nbroker) Connect() error {
|
||||
if n.conn != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -48,17 +49,17 @@ func (n *NatsBroker) Connect() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *NatsBroker) Disconnect() error {
|
||||
func (n *nbroker) Disconnect() error {
|
||||
n.conn.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *NatsBroker) Init() error {
|
||||
func (n *nbroker) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *NatsBroker) Publish(topic string, data []byte) error {
|
||||
b, err := json.Marshal(&Message{
|
||||
func (n *nbroker) Publish(topic string, data []byte) error {
|
||||
b, err := json.Marshal(&broker.Message{
|
||||
Id: uuid.NewUUID().String(),
|
||||
Timestamp: time.Now().Unix(),
|
||||
Topic: topic,
|
||||
@@ -70,9 +71,9 @@ func (n *NatsBroker) Publish(topic string, data []byte) error {
|
||||
return n.conn.Publish(topic, b)
|
||||
}
|
||||
|
||||
func (n *NatsBroker) Subscribe(topic string, function func(*Message)) (Subscriber, error) {
|
||||
subscriber, err := n.conn.Subscribe(topic, func(msg *nats.Msg) {
|
||||
var data *Message
|
||||
func (n *nbroker) Subscribe(topic string, function func(*broker.Message)) (broker.Subscriber, error) {
|
||||
sub, err := n.conn.Subscribe(topic, func(msg *nats.Msg) {
|
||||
var data *broker.Message
|
||||
if err := json.Unmarshal(msg.Data, &data); err != nil {
|
||||
return
|
||||
}
|
||||
@@ -81,10 +82,10 @@ func (n *NatsBroker) Subscribe(topic string, function func(*Message)) (Subscribe
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &NatsSubscriber{s: subscriber}, nil
|
||||
return &subscriber{s: sub}, nil
|
||||
}
|
||||
|
||||
func NewNatsBroker(addrs []string, opts ...Options) Broker {
|
||||
func NewBroker(addrs []string, opt ...broker.Option) broker.Broker {
|
||||
var cAddrs []string
|
||||
for _, addr := range addrs {
|
||||
if len(addr) == 0 {
|
||||
@@ -98,7 +99,7 @@ func NewNatsBroker(addrs []string, opts ...Options) Broker {
|
||||
if len(cAddrs) == 0 {
|
||||
cAddrs = []string{nats.DefaultURL}
|
||||
}
|
||||
return &NatsBroker{
|
||||
return &nbroker{
|
||||
addrs: cAddrs,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user