2015-04-26 21:33:35 +03:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.google.com/p/go-uuid/uuid"
|
|
|
|
log "github.com/golang/glog"
|
2015-05-24 01:16:26 +03:00
|
|
|
c "github.com/myodc/go-micro/context"
|
2015-05-05 21:05:06 +03:00
|
|
|
"github.com/myodc/go-micro/errors"
|
|
|
|
"github.com/myodc/go-micro/registry"
|
2015-05-24 01:16:26 +03:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2015-04-26 21:33:35 +03:00
|
|
|
)
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
type httpBroker struct {
|
2015-04-26 21:33:35 +03:00
|
|
|
id string
|
|
|
|
address string
|
2015-05-23 22:04:16 +03:00
|
|
|
unsubscribe chan *httpSubscriber
|
2015-04-26 21:33:35 +03:00
|
|
|
|
|
|
|
sync.RWMutex
|
2015-05-23 22:04:16 +03:00
|
|
|
subscribers map[string][]*httpSubscriber
|
2015-04-26 21:33:35 +03:00
|
|
|
running bool
|
|
|
|
exit chan chan error
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
type httpSubscriber struct {
|
2015-04-26 21:33:35 +03:00
|
|
|
id string
|
|
|
|
topic string
|
2015-05-23 22:04:16 +03:00
|
|
|
ch chan *httpSubscriber
|
2015-05-24 01:16:26 +03:00
|
|
|
fn func(context.Context, *Message)
|
2015-05-26 00:14:28 +03:00
|
|
|
svc *registry.Service
|
2015-04-26 21:33:35 +03:00
|
|
|
}
|
|
|
|
|
2015-05-24 01:16:26 +03:00
|
|
|
// used in brokers where there is no support for headers
|
|
|
|
type envelope struct {
|
|
|
|
Header map[string]string
|
|
|
|
Message *Message
|
|
|
|
}
|
|
|
|
|
2015-04-26 21:33:35 +03:00
|
|
|
var (
|
2015-05-23 22:04:16 +03:00
|
|
|
DefaultSubPath = "/_sub"
|
2015-04-26 21:33:35 +03:00
|
|
|
)
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
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 {
|
2015-04-26 21:33:35 +03:00
|
|
|
return h.topic
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpSubscriber) Unsubscribe() error {
|
2015-04-26 21:33:35 +03:00
|
|
|
h.ch <- h
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpBroker) start() error {
|
2015-04-26 21:33:35 +03:00
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
if h.running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := net.Listen("tcp", h.address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Broker Listening on %s", l.Addr().String())
|
|
|
|
h.address = l.Addr().String()
|
|
|
|
|
|
|
|
go http.Serve(l, h)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
ce := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ce, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ch := <-h.exit:
|
|
|
|
ch <- l.Close()
|
|
|
|
h.Lock()
|
|
|
|
h.running = false
|
|
|
|
h.Unlock()
|
|
|
|
return
|
|
|
|
case <-ce:
|
|
|
|
h.stop()
|
|
|
|
case subscriber := <-h.unsubscribe:
|
|
|
|
h.Lock()
|
2015-05-23 22:04:16 +03:00
|
|
|
var subscribers []*httpSubscriber
|
2015-04-26 21:33:35 +03:00
|
|
|
for _, sub := range h.subscribers[subscriber.topic] {
|
|
|
|
if sub.id == subscriber.id {
|
|
|
|
registry.Deregister(sub.svc)
|
|
|
|
}
|
|
|
|
subscribers = append(subscribers, sub)
|
|
|
|
}
|
|
|
|
h.subscribers[subscriber.topic] = subscribers
|
|
|
|
h.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
h.running = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpBroker) stop() error {
|
2015-04-26 21:33:35 +03:00
|
|
|
ch := make(chan error)
|
|
|
|
h.exit <- ch
|
|
|
|
return <-ch
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpBroker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2015-04-26 21:33:35 +03:00
|
|
|
if req.Method != "POST" {
|
|
|
|
err := errors.BadRequest("go.micro.broker", "Method not allowed")
|
|
|
|
http.Error(w, err.Error(), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer req.Body.Close()
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
errr := errors.InternalServerError("go.micro.broker", fmt.Sprintf("Error reading request body: %v", err))
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(errr.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-24 01:16:26 +03:00
|
|
|
var e *envelope
|
|
|
|
if err = json.Unmarshal(b, &e); err != nil {
|
2015-04-26 21:33:35 +03:00
|
|
|
errr := errors.InternalServerError("go.micro.broker", fmt.Sprintf("Error parsing request body: %v", err))
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(errr.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-24 01:16:26 +03:00
|
|
|
if len(e.Message.Topic) == 0 {
|
2015-04-26 21:33:35 +03:00
|
|
|
errr := errors.InternalServerError("go.micro.broker", "Topic not found")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(errr.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-27 00:39:48 +03:00
|
|
|
ctx := c.WithMetadata(context.Background(), e.Header)
|
2015-05-24 01:16:26 +03:00
|
|
|
|
2015-04-26 21:33:35 +03:00
|
|
|
h.RLock()
|
2015-05-24 01:16:26 +03:00
|
|
|
for _, subscriber := range h.subscribers[e.Message.Topic] {
|
|
|
|
subscriber.fn(ctx, e.Message)
|
2015-04-26 21:33:35 +03:00
|
|
|
}
|
|
|
|
h.RUnlock()
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpBroker) Address() string {
|
2015-04-26 21:33:35 +03:00
|
|
|
return h.address
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpBroker) Connect() error {
|
2015-04-26 21:33:35 +03:00
|
|
|
return h.start()
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpBroker) Disconnect() error {
|
2015-04-26 21:33:35 +03:00
|
|
|
return h.stop()
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpBroker) Init() error {
|
2015-04-26 21:33:35 +03:00
|
|
|
if len(h.id) == 0 {
|
|
|
|
h.id = "broker-" + uuid.NewUUID().String()
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
http.Handle(DefaultSubPath, h)
|
2015-04-26 21:33:35 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-24 01:16:26 +03:00
|
|
|
func (h *httpBroker) Publish(ctx context.Context, topic string, body []byte) error {
|
2015-04-26 21:33:35 +03:00
|
|
|
s, err := registry.GetService("topic:" + topic)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-24 01:16:26 +03:00
|
|
|
message := &Message{
|
2015-04-26 21:33:35 +03:00
|
|
|
Id: uuid.NewUUID().String(),
|
|
|
|
Timestamp: time.Now().Unix(),
|
|
|
|
Topic: topic,
|
2015-05-24 01:16:26 +03:00
|
|
|
Body: body,
|
|
|
|
}
|
|
|
|
|
2015-05-27 00:39:48 +03:00
|
|
|
header, _ := c.GetMetadata(ctx)
|
2015-05-24 01:16:26 +03:00
|
|
|
|
|
|
|
b, err := json.Marshal(&envelope{
|
|
|
|
header,
|
|
|
|
message,
|
2015-04-26 21:33:35 +03:00
|
|
|
})
|
2015-05-24 01:16:26 +03:00
|
|
|
|
2015-04-26 21:33:35 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-26 00:14:28 +03:00
|
|
|
for _, node := range s.Nodes {
|
|
|
|
r, err := http.Post(fmt.Sprintf("http://%s:%d%s", node.Address, node.Port, DefaultSubPath), "application/json", bytes.NewBuffer(b))
|
2015-04-26 21:33:35 +03:00
|
|
|
if err == nil {
|
|
|
|
r.Body.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-24 01:16:26 +03:00
|
|
|
func (h *httpBroker) Subscribe(topic string, function func(context.Context, *Message)) (Subscriber, error) {
|
2015-04-26 21:33:35 +03:00
|
|
|
// parse address for host, port
|
|
|
|
parts := strings.Split(h.Address(), ":")
|
|
|
|
host := strings.Join(parts[:len(parts)-1], ":")
|
|
|
|
port, _ := strconv.Atoi(parts[len(parts)-1])
|
|
|
|
|
|
|
|
// register service
|
2015-05-26 00:14:28 +03:00
|
|
|
node := ®istry.Node{
|
|
|
|
Id: h.id,
|
|
|
|
Address: host,
|
|
|
|
Port: port,
|
|
|
|
}
|
|
|
|
|
|
|
|
service := ®istry.Service{
|
|
|
|
Name: "topic:" + topic,
|
|
|
|
Nodes: []*registry.Node{node},
|
|
|
|
}
|
2015-04-26 21:33:35 +03:00
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
subscriber := &httpSubscriber{
|
2015-04-26 21:33:35 +03:00
|
|
|
id: uuid.NewUUID().String(),
|
|
|
|
topic: topic,
|
|
|
|
ch: h.unsubscribe,
|
|
|
|
fn: function,
|
|
|
|
svc: service,
|
|
|
|
}
|
|
|
|
|
2015-05-26 00:14:28 +03:00
|
|
|
log.Infof("Registering subscriber %s", node.Id)
|
2015-04-26 21:33:35 +03:00
|
|
|
if err := registry.Register(service); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Lock()
|
|
|
|
h.subscribers[topic] = append(h.subscribers[topic], subscriber)
|
|
|
|
h.Unlock()
|
|
|
|
|
|
|
|
return subscriber, nil
|
|
|
|
}
|