2017-08-07 07:31:53 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-08-20 05:09:36 +00:00
|
|
|
"net/url"
|
2018-03-26 00:03:56 +00:00
|
|
|
"os"
|
2018-05-07 23:13:57 +00:00
|
|
|
"strconv"
|
2017-08-20 05:09:36 +00:00
|
|
|
"strings"
|
2018-05-05 21:08:39 +00:00
|
|
|
"sync"
|
2017-08-07 07:31:53 +00:00
|
|
|
"time"
|
|
|
|
|
2018-05-02 06:19:04 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2018-05-05 21:08:39 +00:00
|
|
|
"github.com/jpillora/backoff"
|
2018-03-26 00:03:56 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-08-07 07:31:53 +00:00
|
|
|
|
|
|
|
"github.com/prologic/msgbus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultReconnectInterval ...
|
2018-05-05 21:08:39 +00:00
|
|
|
DefaultReconnectInterval = 2
|
2017-08-07 07:31:53 +00:00
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
// DefaultMaxReconnectInterval ...
|
|
|
|
DefaultMaxReconnectInterval = 64
|
2018-05-07 23:13:57 +00:00
|
|
|
|
|
|
|
// Time allowed to write a message to the peer.
|
|
|
|
writeWait = 10 * time.Second
|
|
|
|
|
|
|
|
// Time allowed to read the next pong message from the peer.
|
|
|
|
pongWait = 60 * time.Second
|
|
|
|
|
|
|
|
// Send pings to peer with this period. Must be less than pongWait.
|
|
|
|
pingPeriod = (pongWait * 9) / 10
|
2017-08-07 07:31:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client ...
|
|
|
|
type Client struct {
|
2017-08-20 05:09:36 +00:00
|
|
|
url string
|
2017-08-07 07:31:53 +00:00
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
reconnectInterval time.Duration
|
|
|
|
maxReconnectInterval time.Duration
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Options ...
|
|
|
|
type Options struct {
|
2018-05-05 21:08:39 +00:00
|
|
|
ReconnectInterval int
|
|
|
|
MaxReconnectInterval int
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient ...
|
2017-08-20 05:09:36 +00:00
|
|
|
func NewClient(url string, options *Options) *Client {
|
2017-08-07 07:31:53 +00:00
|
|
|
var (
|
2018-05-05 21:08:39 +00:00
|
|
|
reconnectInterval = DefaultReconnectInterval
|
|
|
|
maxReconnectInterval = DefaultMaxReconnectInterval
|
2017-08-07 07:31:53 +00:00
|
|
|
)
|
|
|
|
|
2017-08-20 05:09:36 +00:00
|
|
|
url = strings.TrimSuffix(url, "/")
|
|
|
|
|
|
|
|
client := &Client{url: url}
|
2017-08-07 07:31:53 +00:00
|
|
|
|
|
|
|
if options != nil {
|
|
|
|
if options.ReconnectInterval != 0 {
|
|
|
|
reconnectInterval = options.ReconnectInterval
|
|
|
|
}
|
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
if options.MaxReconnectInterval != 0 {
|
|
|
|
maxReconnectInterval = options.MaxReconnectInterval
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
client.reconnectInterval = time.Duration(reconnectInterval) * time.Second
|
|
|
|
client.maxReconnectInterval = time.Duration(maxReconnectInterval) * time.Second
|
2017-08-07 07:31:53 +00:00
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle ...
|
2018-03-26 00:03:56 +00:00
|
|
|
func (c *Client) Handle(msg *msgbus.Message) error {
|
|
|
|
out, err := json.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error marshalling message: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Stdout.Write(out)
|
2018-03-26 06:45:03 +00:00
|
|
|
os.Stdout.Write([]byte{'\r', '\n'})
|
2018-03-26 00:03:56 +00:00
|
|
|
return nil
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pull ...
|
2018-05-05 21:08:39 +00:00
|
|
|
func (c *Client) Pull(topic string) (msg *msgbus.Message, err error) {
|
2017-08-20 05:09:36 +00:00
|
|
|
url := fmt.Sprintf("%s/%s", c.url, topic)
|
2017-08-07 07:31:53 +00:00
|
|
|
client := &http.Client{}
|
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error constructing request to %s: %s", url, err)
|
|
|
|
return
|
|
|
|
}
|
2017-08-07 07:31:53 +00:00
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error sending request to %s: %s", url, err)
|
|
|
|
return
|
|
|
|
}
|
2017-08-07 07:31:53 +00:00
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
if res.StatusCode == http.StatusNotFound {
|
|
|
|
// Empty queue
|
|
|
|
return
|
|
|
|
}
|
2017-08-07 07:31:53 +00:00
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
defer res.Body.Close()
|
|
|
|
err = json.NewDecoder(res.Body).Decode(&msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf(
|
|
|
|
"error decoding response from %s for %s: %s",
|
|
|
|
url, topic, err,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = c.Handle(msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf(
|
|
|
|
"error handling message from %s for %s: %s",
|
|
|
|
url, topic, err,
|
|
|
|
)
|
|
|
|
return
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
2018-05-05 21:08:39 +00:00
|
|
|
|
|
|
|
return
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Publish ...
|
|
|
|
func (c *Client) Publish(topic, message string) error {
|
|
|
|
var payload bytes.Buffer
|
|
|
|
|
|
|
|
payload.Write([]byte(message))
|
|
|
|
|
2017-08-20 05:09:36 +00:00
|
|
|
url := fmt.Sprintf("%s/%s", c.url, topic)
|
2017-08-07 07:31:53 +00:00
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", url, &payload)
|
|
|
|
if err != nil {
|
2018-04-06 08:27:25 +00:00
|
|
|
return fmt.Errorf("error constructing request: %s", err)
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 08:27:25 +00:00
|
|
|
res, err := client.Do(req)
|
2017-08-07 07:31:53 +00:00
|
|
|
if err != nil {
|
2018-04-06 08:27:25 +00:00
|
|
|
return fmt.Errorf("error publishing message: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-05-15 06:59:01 +00:00
|
|
|
if res.StatusCode != http.StatusAccepted {
|
2018-05-14 10:04:45 +00:00
|
|
|
return fmt.Errorf("unexpected response: %s", res.Status)
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe ...
|
2018-05-01 06:09:12 +00:00
|
|
|
func (c *Client) Subscribe(topic string, handler msgbus.HandlerFunc) *Subscriber {
|
2018-03-27 05:46:35 +00:00
|
|
|
return NewSubscriber(c, topic, handler)
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subscriber ...
|
|
|
|
type Subscriber struct {
|
2018-05-05 21:08:39 +00:00
|
|
|
sync.RWMutex
|
|
|
|
|
|
|
|
conn *websocket.Conn
|
|
|
|
|
2017-08-07 07:31:53 +00:00
|
|
|
client *Client
|
|
|
|
|
2018-03-27 05:46:35 +00:00
|
|
|
topic string
|
2018-05-01 06:09:12 +00:00
|
|
|
handler msgbus.HandlerFunc
|
2017-08-07 07:31:53 +00:00
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
url string
|
|
|
|
reconnectInterval time.Duration
|
|
|
|
maxReconnectInterval time.Duration
|
2018-05-12 18:18:51 +00:00
|
|
|
|
|
|
|
closeWriteChan chan bool
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 05:46:35 +00:00
|
|
|
// NewSubscriber ...
|
2018-05-01 06:09:12 +00:00
|
|
|
func NewSubscriber(client *Client, topic string, handler msgbus.HandlerFunc) *Subscriber {
|
2018-03-27 05:46:35 +00:00
|
|
|
if handler == nil {
|
|
|
|
handler = client.Handle
|
|
|
|
}
|
2017-08-07 07:31:53 +00:00
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
u, err := url.Parse(client.url)
|
2017-08-20 05:09:36 +00:00
|
|
|
if err != nil {
|
2018-05-05 21:08:39 +00:00
|
|
|
log.Fatal("invalid url: %s", client.url)
|
2017-08-20 05:09:36 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
if strings.HasPrefix(client.url, "https") {
|
2018-05-01 06:08:44 +00:00
|
|
|
u.Scheme = "wss"
|
|
|
|
} else {
|
|
|
|
u.Scheme = "ws"
|
|
|
|
}
|
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
u.Path += fmt.Sprintf("/%s", topic)
|
2017-08-20 05:09:36 +00:00
|
|
|
|
|
|
|
url := u.String()
|
2017-08-07 07:31:53 +00:00
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
return &Subscriber{
|
|
|
|
client: client,
|
|
|
|
topic: topic,
|
|
|
|
handler: handler,
|
|
|
|
|
|
|
|
url: url,
|
|
|
|
reconnectInterval: client.reconnectInterval,
|
|
|
|
maxReconnectInterval: client.maxReconnectInterval,
|
2018-05-12 18:18:51 +00:00
|
|
|
|
|
|
|
closeWriteChan: make(chan bool, 1),
|
2018-05-05 21:08:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subscriber) closeAndReconnect() {
|
2018-05-12 18:18:51 +00:00
|
|
|
s.closeWriteChan <- true
|
2018-05-05 21:08:39 +00:00
|
|
|
s.conn.Close()
|
|
|
|
go s.connect()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subscriber) connect() {
|
|
|
|
b := &backoff.Backoff{
|
|
|
|
Min: s.reconnectInterval,
|
|
|
|
Max: s.maxReconnectInterval,
|
|
|
|
Factor: 2,
|
|
|
|
Jitter: false,
|
|
|
|
}
|
|
|
|
|
2017-08-07 07:31:53 +00:00
|
|
|
for {
|
2018-05-05 21:08:39 +00:00
|
|
|
d := b.Duration()
|
|
|
|
|
|
|
|
conn, _, err := websocket.DefaultDialer.Dial(s.url, nil)
|
|
|
|
|
2017-08-07 07:31:53 +00:00
|
|
|
if err != nil {
|
2018-05-05 21:08:39 +00:00
|
|
|
log.Warnf("error connecting to %s: %s", s.url, err)
|
|
|
|
log.Infof("reconnecting in %s", d)
|
|
|
|
time.Sleep(d)
|
|
|
|
continue
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
log.Infof("successfully connected to %s", s.url)
|
2017-08-19 07:15:45 +00:00
|
|
|
|
2018-05-06 16:34:52 +00:00
|
|
|
s.Lock()
|
2018-05-05 21:08:39 +00:00
|
|
|
s.conn = conn
|
2018-05-12 18:18:51 +00:00
|
|
|
s.closeWriteChan = make(chan bool, 1)
|
2018-05-06 16:34:52 +00:00
|
|
|
s.Unlock()
|
2018-05-05 21:08:39 +00:00
|
|
|
|
|
|
|
go s.readLoop()
|
2018-05-07 23:13:57 +00:00
|
|
|
go s.writeLoop()
|
2018-05-05 21:08:39 +00:00
|
|
|
|
|
|
|
break
|
2017-08-19 07:15:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
func (s *Subscriber) readLoop() {
|
2017-08-19 07:15:45 +00:00
|
|
|
var msg *msgbus.Message
|
|
|
|
|
2018-05-07 23:13:57 +00:00
|
|
|
s.conn.SetReadDeadline(time.Now().Add(pongWait))
|
|
|
|
|
|
|
|
s.conn.SetPongHandler(func(message string) error {
|
|
|
|
log.Debugf("recieved pong from %s: %s", s.url, message)
|
|
|
|
t, err := strconv.ParseInt(message, 10, 64)
|
|
|
|
d := time.Duration(time.Now().UnixNano() - t)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("garbage pong reply from %s: %s", s.url, err)
|
|
|
|
} else {
|
|
|
|
log.Debugf("pong latency of %s: %s", s.url, d)
|
|
|
|
}
|
|
|
|
s.conn.SetReadDeadline(time.Now().Add(pongWait))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2017-08-19 07:15:45 +00:00
|
|
|
for {
|
2018-05-02 06:19:04 +00:00
|
|
|
err := s.conn.ReadJSON(&msg)
|
2017-08-19 07:15:45 +00:00
|
|
|
if err != nil {
|
2018-05-05 21:08:39 +00:00
|
|
|
log.Errorf("error reading from %s: %s", s.url, err)
|
|
|
|
s.closeAndReconnect()
|
|
|
|
return
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
2018-05-05 21:08:39 +00:00
|
|
|
|
2018-03-27 08:40:03 +00:00
|
|
|
err = s.handler(msg)
|
|
|
|
if err != nil {
|
2018-05-05 21:08:39 +00:00
|
|
|
log.Warnf("error handling message: %s", err)
|
2018-03-27 08:40:03 +00:00
|
|
|
}
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-05 21:08:39 +00:00
|
|
|
|
2018-05-07 23:13:57 +00:00
|
|
|
func (s *Subscriber) writeLoop() {
|
|
|
|
ticker := time.NewTicker(pingPeriod)
|
|
|
|
defer func() {
|
|
|
|
ticker.Stop()
|
|
|
|
if s.conn != nil {
|
|
|
|
s.conn.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
2018-05-12 18:18:51 +00:00
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
s.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
|
|
|
t := time.Now()
|
|
|
|
message := []byte(fmt.Sprintf("%d", t.UnixNano()))
|
|
|
|
if err := s.conn.WriteMessage(websocket.PingMessage, message); err != nil {
|
|
|
|
log.Errorf("error sending ping to %s: %s", s.url, err)
|
|
|
|
s.closeAndReconnect()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-s.closeWriteChan:
|
2018-05-07 23:13:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
// Start ...
|
|
|
|
func (s *Subscriber) Start() {
|
|
|
|
go s.connect()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop ...
|
|
|
|
func (s *Subscriber) Stop() {
|
|
|
|
log.Infof("shutting down ...")
|
|
|
|
|
|
|
|
err := s.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("error sending close message: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.conn.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("error closing connection: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.conn = nil
|
|
|
|
}
|