2015-04-26 19:33:35 +01:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-01-16 22:13:02 +00:00
|
|
|
"crypto/tls"
|
2015-04-26 19:33:35 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-12-23 20:05:47 +00:00
|
|
|
"io"
|
2015-04-26 19:33:35 +01:00
|
|
|
"io/ioutil"
|
2016-03-14 11:01:10 +00:00
|
|
|
"log"
|
2015-12-23 20:05:47 +00:00
|
|
|
"math/rand"
|
2015-04-26 19:33:35 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2016-01-16 22:13:02 +00:00
|
|
|
"net/url"
|
|
|
|
"runtime"
|
2015-04-26 19:33:35 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2015-12-23 20:05:47 +00:00
|
|
|
"time"
|
2015-04-26 19:33:35 +01:00
|
|
|
|
2015-11-20 16:17:33 +00:00
|
|
|
"github.com/micro/go-micro/errors"
|
|
|
|
"github.com/micro/go-micro/registry"
|
2016-01-16 23:05:11 +00:00
|
|
|
mls "github.com/micro/misc/lib/tls"
|
2015-08-26 12:15:37 +01:00
|
|
|
"github.com/pborman/uuid"
|
2016-01-20 15:22:44 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2015-04-26 19:33:35 +01:00
|
|
|
)
|
|
|
|
|
2015-12-23 19:07:26 +00:00
|
|
|
// HTTP Broker is a placeholder for actual message brokers.
|
|
|
|
// This should not really be used in production but useful
|
|
|
|
// in developer where you want zero dependencies.
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
type httpBroker struct {
|
2015-04-26 19:33:35 +01:00
|
|
|
id string
|
|
|
|
address string
|
2015-05-23 20:04:16 +01:00
|
|
|
unsubscribe chan *httpSubscriber
|
2015-12-31 18:11:46 +00:00
|
|
|
opts Options
|
2015-04-26 19:33:35 +01:00
|
|
|
|
2016-03-22 18:02:50 +00:00
|
|
|
mux *http.ServeMux
|
|
|
|
|
2016-01-16 22:13:02 +00:00
|
|
|
c *http.Client
|
2016-01-20 15:22:44 +00:00
|
|
|
r registry.Registry
|
2016-01-16 22:13:02 +00:00
|
|
|
|
2015-04-26 19:33:35 +01:00
|
|
|
sync.RWMutex
|
2015-05-23 20:04:16 +01:00
|
|
|
subscribers map[string][]*httpSubscriber
|
2015-04-26 19:33:35 +01:00
|
|
|
running bool
|
|
|
|
exit chan chan error
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
type httpSubscriber struct {
|
2015-12-23 19:07:26 +00:00
|
|
|
opts SubscribeOptions
|
2015-04-26 19:33:35 +01:00
|
|
|
id string
|
|
|
|
topic string
|
2015-05-23 20:04:16 +01:00
|
|
|
ch chan *httpSubscriber
|
2015-06-12 19:52:27 +01:00
|
|
|
fn Handler
|
2015-05-25 22:14:28 +01:00
|
|
|
svc *registry.Service
|
2015-04-26 19:33:35 +01:00
|
|
|
}
|
|
|
|
|
2015-12-23 19:07:26 +00:00
|
|
|
type httpPublication struct {
|
|
|
|
m *Message
|
|
|
|
t string
|
|
|
|
}
|
|
|
|
|
2015-04-26 19:33:35 +01:00
|
|
|
var (
|
2015-12-23 20:05:47 +00:00
|
|
|
DefaultSubPath = "/_sub"
|
|
|
|
broadcastVersion = "ff.http.broadcast"
|
2016-01-27 20:17:31 +00:00
|
|
|
registerTTL = time.Minute
|
|
|
|
registerInterval = time.Second * 30
|
2015-04-26 19:33:35 +01:00
|
|
|
)
|
|
|
|
|
2015-12-23 20:05:47 +00:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
2016-01-17 00:33:07 +00:00
|
|
|
func newTransport(config *tls.Config) *http.Transport {
|
|
|
|
if config == nil {
|
|
|
|
config = &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 22:13:02 +00:00
|
|
|
t := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
2016-01-18 00:10:04 +00:00
|
|
|
TLSClientConfig: config,
|
2016-01-16 22:13:02 +00:00
|
|
|
}
|
|
|
|
runtime.SetFinalizer(&t, func(tr **http.Transport) {
|
|
|
|
(*tr).CloseIdleConnections()
|
|
|
|
})
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-03-15 22:12:28 +00:00
|
|
|
func newHttpBroker(opts ...Option) Broker {
|
2016-01-20 15:22:44 +00:00
|
|
|
options := Options{
|
|
|
|
Context: context.TODO(),
|
|
|
|
}
|
|
|
|
|
2016-01-16 23:05:11 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
addr := ":0"
|
2016-03-15 22:12:28 +00:00
|
|
|
if len(options.Addrs) > 0 && len(options.Addrs[0]) > 0 {
|
|
|
|
addr = options.Addrs[0]
|
2015-05-23 20:04:16 +01:00
|
|
|
}
|
|
|
|
|
2016-01-20 15:22:44 +00:00
|
|
|
reg, ok := options.Context.Value(registryKey).(registry.Registry)
|
|
|
|
if !ok {
|
|
|
|
reg = registry.DefaultRegistry
|
|
|
|
}
|
|
|
|
|
2016-03-22 18:02:50 +00:00
|
|
|
h := &httpBroker{
|
2015-06-12 19:52:27 +01:00
|
|
|
id: "broker-" + uuid.NewUUID().String(),
|
2015-05-23 20:04:16 +01:00
|
|
|
address: addr,
|
2016-01-16 23:05:11 +00:00
|
|
|
opts: options,
|
2016-01-20 15:22:44 +00:00
|
|
|
r: reg,
|
2016-01-17 00:33:07 +00:00
|
|
|
c: &http.Client{Transport: newTransport(options.TLSConfig)},
|
2015-05-23 20:04:16 +01:00
|
|
|
subscribers: make(map[string][]*httpSubscriber),
|
|
|
|
unsubscribe: make(chan *httpSubscriber),
|
|
|
|
exit: make(chan chan error),
|
2016-03-22 18:02:50 +00:00
|
|
|
mux: http.NewServeMux(),
|
2015-05-23 20:04:16 +01:00
|
|
|
}
|
2016-03-22 18:02:50 +00:00
|
|
|
|
|
|
|
h.mux.Handle(DefaultSubPath, h)
|
|
|
|
return h
|
2015-05-23 20:04:16 +01:00
|
|
|
}
|
|
|
|
|
2015-12-23 19:07:26 +00:00
|
|
|
func (h *httpPublication) Ack() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpPublication) Message() *Message {
|
|
|
|
return h.m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpPublication) Topic() string {
|
|
|
|
return h.t
|
|
|
|
}
|
|
|
|
|
2015-12-31 18:11:46 +00:00
|
|
|
func (h *httpSubscriber) Options() SubscribeOptions {
|
2015-12-23 19:07:26 +00:00
|
|
|
return h.opts
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func (h *httpSubscriber) Topic() string {
|
2015-04-26 19:33:35 +01:00
|
|
|
return h.topic
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func (h *httpSubscriber) Unsubscribe() error {
|
2015-04-26 19:33:35 +01:00
|
|
|
h.ch <- h
|
2016-01-16 23:05:11 +00:00
|
|
|
// artificial delay
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
2015-04-26 19:33:35 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 20:17:31 +00:00
|
|
|
func (h *httpBroker) run(l net.Listener) {
|
|
|
|
t := time.NewTicker(registerInterval)
|
|
|
|
defer t.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// heartbeat for each subscriber
|
|
|
|
case <-t.C:
|
|
|
|
h.RLock()
|
|
|
|
for _, subs := range h.subscribers {
|
|
|
|
for _, sub := range subs {
|
|
|
|
h.r.Register(sub.svc, registry.RegisterTTL(registerTTL))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h.RUnlock()
|
|
|
|
// received exit signal
|
|
|
|
case ch := <-h.exit:
|
|
|
|
ch <- l.Close()
|
|
|
|
h.Lock()
|
|
|
|
h.running = false
|
|
|
|
h.Unlock()
|
|
|
|
return
|
|
|
|
// unsubscribe subscriber
|
|
|
|
case subscriber := <-h.unsubscribe:
|
|
|
|
h.Lock()
|
|
|
|
var subscribers []*httpSubscriber
|
|
|
|
for _, sub := range h.subscribers[subscriber.topic] {
|
|
|
|
if sub.id == subscriber.id {
|
|
|
|
h.r.Deregister(sub.svc)
|
|
|
|
}
|
|
|
|
subscribers = append(subscribers, sub)
|
|
|
|
}
|
|
|
|
h.subscribers[subscriber.topic] = subscribers
|
|
|
|
h.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func (h *httpBroker) start() error {
|
2015-04-26 19:33:35 +01:00
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
if h.running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-16 23:05:11 +00:00
|
|
|
var l net.Listener
|
|
|
|
var err error
|
|
|
|
|
2016-01-17 00:33:07 +00:00
|
|
|
if h.opts.Secure || h.opts.TLSConfig != nil {
|
2016-01-16 23:39:47 +00:00
|
|
|
config := h.opts.TLSConfig
|
|
|
|
if config == nil {
|
|
|
|
cert, err := mls.Certificate(h.address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config = &tls.Config{Certificates: []tls.Certificate{cert}}
|
2016-01-16 23:05:11 +00:00
|
|
|
}
|
2016-01-16 23:39:47 +00:00
|
|
|
l, err = tls.Listen("tcp", h.address, config)
|
2016-01-16 23:05:11 +00:00
|
|
|
} else {
|
|
|
|
l, err = net.Listen("tcp", h.address)
|
|
|
|
}
|
|
|
|
|
2015-04-26 19:33:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-14 11:01:10 +00:00
|
|
|
log.Printf("Broker Listening on %s", l.Addr().String())
|
2015-04-26 19:33:35 +01:00
|
|
|
h.address = l.Addr().String()
|
|
|
|
|
2016-03-22 18:02:50 +00:00
|
|
|
go http.Serve(l, h.mux)
|
2016-01-27 20:17:31 +00:00
|
|
|
go h.run(l)
|
2015-04-26 19:33:35 +01:00
|
|
|
|
|
|
|
h.running = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func (h *httpBroker) stop() error {
|
2016-01-27 20:17:31 +00:00
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
if !h.running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-26 19:33:35 +01:00
|
|
|
ch := make(chan error)
|
|
|
|
h.exit <- ch
|
2016-01-27 20:17:31 +00:00
|
|
|
err := <-ch
|
|
|
|
h.running = false
|
|
|
|
return err
|
2015-04-26 19:33:35 +01:00
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func (h *httpBroker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2015-04-26 19:33:35 +01: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()
|
|
|
|
|
2016-01-16 22:13:02 +00:00
|
|
|
req.ParseForm()
|
|
|
|
|
2015-04-26 19:33:35 +01:00
|
|
|
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-06-12 19:52:27 +01:00
|
|
|
var m *Message
|
|
|
|
if err = json.Unmarshal(b, &m); err != nil {
|
2015-04-26 19:33:35 +01: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-06-12 19:52:27 +01:00
|
|
|
topic := m.Header[":topic"]
|
|
|
|
delete(m.Header, ":topic")
|
|
|
|
|
|
|
|
if len(topic) == 0 {
|
2015-04-26 19:33:35 +01:00
|
|
|
errr := errors.InternalServerError("go.micro.broker", "Topic not found")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(errr.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-23 19:07:26 +00:00
|
|
|
p := &httpPublication{m: m, t: topic}
|
2016-01-16 22:13:02 +00:00
|
|
|
id := req.Form.Get("id")
|
|
|
|
|
2015-04-26 19:33:35 +01:00
|
|
|
h.RLock()
|
2016-04-28 00:16:11 +01:00
|
|
|
|
2015-06-12 19:52:27 +01:00
|
|
|
for _, subscriber := range h.subscribers[topic] {
|
2016-01-16 22:13:02 +00:00
|
|
|
if id == subscriber.id {
|
2016-04-28 00:16:11 +01:00
|
|
|
// sub is sync; crufty rate limiting
|
|
|
|
// so we don't hose the cpu
|
2016-01-16 22:13:02 +00:00
|
|
|
subscriber.fn(p)
|
|
|
|
}
|
2015-04-26 19:33:35 +01:00
|
|
|
}
|
|
|
|
h.RUnlock()
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func (h *httpBroker) Address() string {
|
2015-04-26 19:33:35 +01:00
|
|
|
return h.address
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func (h *httpBroker) Connect() error {
|
2015-04-26 19:33:35 +01:00
|
|
|
return h.start()
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
func (h *httpBroker) Disconnect() error {
|
2015-04-26 19:33:35 +01:00
|
|
|
return h.stop()
|
|
|
|
}
|
|
|
|
|
2015-12-23 19:07:26 +00:00
|
|
|
func (h *httpBroker) Init(opts ...Option) error {
|
2016-01-16 23:05:11 +00:00
|
|
|
for _, o := range opts {
|
2016-01-16 23:10:14 +00:00
|
|
|
o(&h.opts)
|
2016-01-16 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 19:33:35 +01:00
|
|
|
if len(h.id) == 0 {
|
|
|
|
h.id = "broker-" + uuid.NewUUID().String()
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:22:44 +00:00
|
|
|
reg, ok := h.opts.Context.Value(registryKey).(registry.Registry)
|
|
|
|
if !ok {
|
|
|
|
reg = registry.DefaultRegistry
|
|
|
|
}
|
|
|
|
|
|
|
|
h.r = reg
|
|
|
|
|
2015-04-26 19:33:35 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-31 18:11:46 +00:00
|
|
|
func (h *httpBroker) Options() Options {
|
|
|
|
return h.opts
|
|
|
|
}
|
|
|
|
|
2015-12-23 19:07:26 +00:00
|
|
|
func (h *httpBroker) Publish(topic string, msg *Message, opts ...PublishOption) error {
|
2016-01-20 15:22:44 +00:00
|
|
|
s, err := h.r.GetService("topic:" + topic)
|
2015-04-26 19:33:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-21 23:52:08 +00:00
|
|
|
if msg.Header == nil {
|
|
|
|
msg.Header = map[string]string{}
|
|
|
|
}
|
|
|
|
|
2015-06-12 19:52:27 +01:00
|
|
|
msg.Header[":topic"] = topic
|
|
|
|
b, err := json.Marshal(msg)
|
2015-04-26 19:33:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-28 00:16:11 +01:00
|
|
|
fn := func(node *registry.Node, b []byte) {
|
2016-01-16 22:13:02 +00:00
|
|
|
scheme := "http"
|
2016-01-16 23:05:11 +00:00
|
|
|
|
2016-01-16 22:13:02 +00:00
|
|
|
// check if secure is added in metadata
|
|
|
|
if node.Metadata["secure"] == "true" {
|
|
|
|
scheme = "https"
|
|
|
|
}
|
|
|
|
|
|
|
|
vals := url.Values{}
|
|
|
|
vals.Add("id", node.Id)
|
2016-04-28 00:16:11 +01:00
|
|
|
|
2016-01-16 22:13:02 +00:00
|
|
|
uri := fmt.Sprintf("%s://%s:%d%s?%s", scheme, node.Address, node.Port, DefaultSubPath, vals.Encode())
|
2016-04-28 00:16:11 +01:00
|
|
|
r, err := h.c.Post(uri, "application/json", bytes.NewReader(b))
|
2015-12-23 20:05:47 +00:00
|
|
|
if err == nil {
|
2016-04-28 00:16:11 +01:00
|
|
|
io.Copy(ioutil.Discard, r.Body)
|
2015-12-23 20:05:47 +00:00
|
|
|
r.Body.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-08 01:48:48 +00:00
|
|
|
for _, service := range s {
|
2015-12-23 20:05:47 +00:00
|
|
|
// broadcast version means broadcast to all nodes
|
|
|
|
if service.Version == broadcastVersion {
|
|
|
|
for _, node := range service.Nodes {
|
2016-04-28 00:16:11 +01:00
|
|
|
// publish async
|
|
|
|
go fn(node, b)
|
2015-11-08 01:48:48 +00:00
|
|
|
}
|
2015-12-23 20:05:47 +00:00
|
|
|
return nil
|
2015-04-26 19:33:35 +01:00
|
|
|
}
|
2015-12-23 20:05:47 +00:00
|
|
|
|
|
|
|
node := service.Nodes[rand.Int()%len(service.Nodes)]
|
2016-04-28 00:16:11 +01:00
|
|
|
// publish async
|
|
|
|
go fn(node, b)
|
2015-12-23 20:05:47 +00:00
|
|
|
return nil
|
2015-04-26 19:33:35 +01:00
|
|
|
}
|
2015-12-23 20:05:47 +00:00
|
|
|
|
2015-04-26 19:33:35 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-23 19:07:26 +00:00
|
|
|
func (h *httpBroker) Subscribe(topic string, handler Handler, opts ...SubscribeOption) (Subscriber, error) {
|
|
|
|
opt := newSubscribeOptions(opts...)
|
|
|
|
|
2015-04-26 19:33:35 +01: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])
|
|
|
|
|
2016-02-15 21:57:17 +00:00
|
|
|
addr, err := extractAddress(host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-27 01:17:53 +00:00
|
|
|
id := uuid.NewUUID().String()
|
|
|
|
|
2016-01-17 00:33:07 +00:00
|
|
|
var secure bool
|
|
|
|
|
|
|
|
if h.opts.Secure || h.opts.TLSConfig != nil {
|
|
|
|
secure = true
|
|
|
|
}
|
|
|
|
|
2015-04-26 19:33:35 +01:00
|
|
|
// register service
|
2015-05-25 22:14:28 +01:00
|
|
|
node := ®istry.Node{
|
2016-01-16 22:13:02 +00:00
|
|
|
Id: h.id + "." + id,
|
2016-02-15 21:57:17 +00:00
|
|
|
Address: addr,
|
2015-05-25 22:14:28 +01:00
|
|
|
Port: port,
|
2016-01-16 22:13:02 +00:00
|
|
|
Metadata: map[string]string{
|
2016-01-17 00:33:07 +00:00
|
|
|
"secure": fmt.Sprintf("%t", secure),
|
2016-01-16 22:13:02 +00:00
|
|
|
},
|
2015-05-25 22:14:28 +01:00
|
|
|
}
|
|
|
|
|
2015-12-23 20:05:47 +00:00
|
|
|
version := opt.Queue
|
|
|
|
if len(version) == 0 {
|
|
|
|
version = broadcastVersion
|
|
|
|
}
|
|
|
|
|
2015-05-25 22:14:28 +01:00
|
|
|
service := ®istry.Service{
|
2015-12-23 20:05:47 +00:00
|
|
|
Name: "topic:" + topic,
|
|
|
|
Version: version,
|
|
|
|
Nodes: []*registry.Node{node},
|
2015-05-25 22:14:28 +01:00
|
|
|
}
|
2015-04-26 19:33:35 +01:00
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
subscriber := &httpSubscriber{
|
2015-12-23 19:07:26 +00:00
|
|
|
opts: opt,
|
2016-01-16 22:13:02 +00:00
|
|
|
id: h.id + "." + id,
|
2015-04-26 19:33:35 +01:00
|
|
|
topic: topic,
|
|
|
|
ch: h.unsubscribe,
|
2015-06-12 19:52:27 +01:00
|
|
|
fn: handler,
|
2015-04-26 19:33:35 +01:00
|
|
|
svc: service,
|
|
|
|
}
|
|
|
|
|
2016-02-15 22:20:54 +00:00
|
|
|
if err := h.r.Register(service, registry.RegisterTTL(registerTTL)); err != nil {
|
2015-04-26 19:33:35 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Lock()
|
|
|
|
h.subscribers[topic] = append(h.subscribers[topic], subscriber)
|
|
|
|
h.Unlock()
|
|
|
|
return subscriber, nil
|
|
|
|
}
|
2015-12-19 21:56:14 +00:00
|
|
|
|
|
|
|
func (h *httpBroker) String() string {
|
|
|
|
return "http"
|
|
|
|
}
|