micro/broker/http_broker.go

493 lines
9.4 KiB
Go
Raw Normal View History

package broker
import (
"bytes"
2016-01-17 01:13:02 +03:00
"crypto/tls"
"fmt"
2015-12-23 23:05:47 +03:00
"io"
"io/ioutil"
2016-03-14 14:01:10 +03:00
"log"
2015-12-23 23:05:47 +03:00
"math/rand"
"net"
"net/http"
2016-01-17 01:13:02 +03:00
"net/url"
"runtime"
"strconv"
"strings"
"sync"
2015-12-23 23:05:47 +03:00
"time"
"github.com/micro/go-micro/broker/codec/json"
2015-11-20 19:17:33 +03:00
"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/registry"
maddr "github.com/micro/misc/lib/addr"
mnet "github.com/micro/misc/lib/net"
mls "github.com/micro/misc/lib/tls"
2015-08-26 14:15:37 +03:00
"github.com/pborman/uuid"
2016-01-20 18:22:44 +03:00
"golang.org/x/net/context"
)
2015-12-23 22:07:26 +03: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.
type httpBroker struct {
id string
address string
unsubscribe chan *httpSubscriber
opts Options
mux *http.ServeMux
2016-01-17 01:13:02 +03:00
c *http.Client
2016-01-20 18:22:44 +03:00
r registry.Registry
2016-01-17 01:13:02 +03:00
sync.RWMutex
subscribers map[string][]*httpSubscriber
running bool
exit chan chan error
}
type httpSubscriber struct {
2015-12-23 22:07:26 +03:00
opts SubscribeOptions
id string
topic string
ch chan *httpSubscriber
fn Handler
svc *registry.Service
}
2015-12-23 22:07:26 +03:00
type httpPublication struct {
m *Message
t string
}
var (
2015-12-23 23:05:47 +03:00
DefaultSubPath = "/_sub"
broadcastVersion = "ff.http.broadcast"
registerTTL = time.Minute
registerInterval = time.Second * 30
)
2015-12-23 23:05:47 +03:00
func init() {
rand.Seed(time.Now().Unix())
}
func newTransport(config *tls.Config) *http.Transport {
if config == nil {
config = &tls.Config{
InsecureSkipVerify: true,
}
}
2016-01-17 01:13:02 +03: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 03:10:04 +03:00
TLSClientConfig: config,
2016-01-17 01:13:02 +03:00
}
runtime.SetFinalizer(&t, func(tr **http.Transport) {
(*tr).CloseIdleConnections()
})
return t
}
2016-03-16 01:12:28 +03:00
func newHttpBroker(opts ...Option) Broker {
2016-01-20 18:22:44 +03:00
options := Options{
Codec: json.NewCodec(),
2016-01-20 18:22:44 +03:00
Context: context.TODO(),
}
for _, o := range opts {
o(&options)
}
addr := ":0"
2016-03-16 01:12:28 +03:00
if len(options.Addrs) > 0 && len(options.Addrs[0]) > 0 {
addr = options.Addrs[0]
}
2016-01-20 18:22:44 +03:00
reg, ok := options.Context.Value(registryKey).(registry.Registry)
if !ok {
reg = registry.DefaultRegistry
}
h := &httpBroker{
id: "broker-" + uuid.NewUUID().String(),
address: addr,
opts: options,
2016-01-20 18:22:44 +03:00
r: reg,
c: &http.Client{Transport: newTransport(options.TLSConfig)},
subscribers: make(map[string][]*httpSubscriber),
unsubscribe: make(chan *httpSubscriber),
exit: make(chan chan error),
mux: http.NewServeMux(),
}
h.mux.Handle(DefaultSubPath, h)
return h
}
2015-12-23 22:07:26 +03:00
func (h *httpPublication) Ack() error {
return nil
}
func (h *httpPublication) Message() *Message {
return h.m
}
func (h *httpPublication) Topic() string {
return h.t
}
func (h *httpSubscriber) Options() SubscribeOptions {
2015-12-23 22:07:26 +03:00
return h.opts
}
func (h *httpSubscriber) Topic() string {
return h.topic
}
func (h *httpSubscriber) Unsubscribe() error {
h.ch <- h
// artificial delay
time.Sleep(time.Millisecond * 10)
return nil
}
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] {
2016-11-21 18:51:30 +03:00
// deregister and skip forward
if sub.id == subscriber.id {
h.r.Deregister(sub.svc)
2016-11-21 18:51:30 +03:00
continue
}
subscribers = append(subscribers, sub)
}
h.subscribers[subscriber.topic] = subscribers
h.Unlock()
}
}
}
func (h *httpBroker) start() error {
h.Lock()
defer h.Unlock()
if h.running {
return nil
}
var l net.Listener
var err error
if h.opts.Secure || h.opts.TLSConfig != nil {
2016-01-17 02:39:47 +03:00
config := h.opts.TLSConfig
fn := func(addr string) (net.Listener, error) {
if config == nil {
hosts := []string{addr}
// check if its a valid host:port
if host, _, err := net.SplitHostPort(addr); err == nil {
if len(host) == 0 {
hosts = maddr.IPs()
} else {
hosts = []string{host}
}
}
// generate a certificate
cert, err := mls.Certificate(hosts...)
if err != nil {
return nil, err
}
config = &tls.Config{Certificates: []tls.Certificate{cert}}
2016-01-17 02:39:47 +03:00
}
return tls.Listen("tcp", addr, config)
}
l, err = mnet.Listen(h.address, fn)
} else {
fn := func(addr string) (net.Listener, error) {
return net.Listen("tcp", addr)
}
l, err = mnet.Listen(h.address, fn)
}
if err != nil {
return err
}
2016-03-14 14:01:10 +03:00
log.Printf("Broker Listening on %s", l.Addr().String())
h.address = l.Addr().String()
go http.Serve(l, h.mux)
go h.run(l)
h.running = true
return nil
}
func (h *httpBroker) stop() error {
h.Lock()
defer h.Unlock()
if !h.running {
return nil
}
ch := make(chan error)
h.exit <- ch
err := <-ch
h.running = false
return err
}
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)
return
}
defer req.Body.Close()
2016-01-17 01:13:02 +03:00
req.ParseForm()
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
}
var m *Message
if err = h.opts.Codec.Unmarshal(b, &m); err != nil {
errr := errors.InternalServerError("go.micro.broker", fmt.Sprintf("Error parsing request body: %v", err))
w.WriteHeader(500)
w.Write([]byte(errr.Error()))
return
}
topic := m.Header[":topic"]
delete(m.Header, ":topic")
if len(topic) == 0 {
errr := errors.InternalServerError("go.micro.broker", "Topic not found")
w.WriteHeader(500)
w.Write([]byte(errr.Error()))
return
}
2015-12-23 22:07:26 +03:00
p := &httpPublication{m: m, t: topic}
2016-01-17 01:13:02 +03:00
id := req.Form.Get("id")
h.RLock()
for _, subscriber := range h.subscribers[topic] {
2016-01-17 01:13:02 +03:00
if id == subscriber.id {
// sub is sync; crufty rate limiting
// so we don't hose the cpu
2016-01-17 01:13:02 +03:00
subscriber.fn(p)
}
}
h.RUnlock()
}
func (h *httpBroker) Address() string {
return h.address
}
func (h *httpBroker) Connect() error {
return h.start()
}
func (h *httpBroker) Disconnect() error {
return h.stop()
}
2015-12-23 22:07:26 +03:00
func (h *httpBroker) Init(opts ...Option) error {
for _, o := range opts {
2016-01-17 02:10:14 +03:00
o(&h.opts)
}
if len(h.id) == 0 {
h.id = "broker-" + uuid.NewUUID().String()
}
2016-01-20 18:22:44 +03:00
reg, ok := h.opts.Context.Value(registryKey).(registry.Registry)
if !ok {
reg = registry.DefaultRegistry
}
h.r = reg
return nil
}
func (h *httpBroker) Options() Options {
return h.opts
}
2015-12-23 22:07:26 +03:00
func (h *httpBroker) Publish(topic string, msg *Message, opts ...PublishOption) error {
2016-01-20 18:22:44 +03:00
s, err := h.r.GetService("topic:" + topic)
if err != nil {
return err
}
m := &Message{
Header: make(map[string]string),
Body: msg.Body,
}
for k, v := range msg.Header {
m.Header[k] = v
2016-02-22 02:52:08 +03:00
}
m.Header[":topic"] = topic
b, err := h.opts.Codec.Marshal(m)
if err != nil {
return err
}
fn := func(node *registry.Node, b []byte) {
2016-01-17 01:13:02 +03:00
scheme := "http"
2016-01-17 01:13:02 +03:00
// check if secure is added in metadata
if node.Metadata["secure"] == "true" {
scheme = "https"
}
vals := url.Values{}
vals.Add("id", node.Id)
2016-01-17 01:13:02 +03:00
uri := fmt.Sprintf("%s://%s:%d%s?%s", scheme, node.Address, node.Port, DefaultSubPath, vals.Encode())
r, err := h.c.Post(uri, "application/json", bytes.NewReader(b))
2015-12-23 23:05:47 +03:00
if err == nil {
io.Copy(ioutil.Discard, r.Body)
2015-12-23 23:05:47 +03:00
r.Body.Close()
}
}
2015-11-08 04:48:48 +03:00
for _, service := range s {
// only process if we have nodes
if len(service.Nodes) == 0 {
continue
}
switch service.Version {
2015-12-23 23:05:47 +03:00
// broadcast version means broadcast to all nodes
case broadcastVersion:
2015-12-23 23:05:47 +03:00
for _, node := range service.Nodes {
// publish async
go fn(node, b)
2015-11-08 04:48:48 +03:00
}
2015-12-23 23:05:47 +03:00
default:
// select node to publish to
node := service.Nodes[rand.Int()%len(service.Nodes)]
// publish async
go fn(node, b)
}
}
2015-12-23 23:05:47 +03:00
return nil
}
2015-12-23 22:07:26 +03:00
func (h *httpBroker) Subscribe(topic string, handler Handler, opts ...SubscribeOption) (Subscriber, error) {
opt := newSubscribeOptions(opts...)
// parse address for host, port
parts := strings.Split(h.Address(), ":")
host := strings.Join(parts[:len(parts)-1], ":")
port, _ := strconv.Atoi(parts[len(parts)-1])
addr, err := maddr.Extract(host)
2016-02-16 00:57:17 +03:00
if err != nil {
return nil, err
}
id := uuid.NewUUID().String()
var secure bool
if h.opts.Secure || h.opts.TLSConfig != nil {
secure = true
}
// register service
node := &registry.Node{
2016-01-17 01:13:02 +03:00
Id: h.id + "." + id,
2016-02-16 00:57:17 +03:00
Address: addr,
Port: port,
2016-01-17 01:13:02 +03:00
Metadata: map[string]string{
"secure": fmt.Sprintf("%t", secure),
2016-01-17 01:13:02 +03:00
},
}
2015-12-23 23:05:47 +03:00
version := opt.Queue
if len(version) == 0 {
version = broadcastVersion
}
service := &registry.Service{
2015-12-23 23:05:47 +03:00
Name: "topic:" + topic,
Version: version,
Nodes: []*registry.Node{node},
}
subscriber := &httpSubscriber{
2015-12-23 22:07:26 +03:00
opts: opt,
2016-01-17 01:13:02 +03:00
id: h.id + "." + id,
topic: topic,
ch: h.unsubscribe,
fn: handler,
svc: service,
}
if err := h.r.Register(service, registry.RegisterTTL(registerTTL)); err != nil {
return nil, err
}
h.Lock()
h.subscribers[topic] = append(h.subscribers[topic], subscriber)
h.Unlock()
return subscriber, nil
}
2015-12-20 00:56:14 +03:00
func (h *httpBroker) String() string {
return "http"
}