2017-08-07 07:31:53 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-04-06 08:27:25 +00:00
|
|
|
"io/ioutil"
|
2017-08-07 07:31:53 +00:00
|
|
|
"net/http"
|
2017-08-20 05:09:36 +00:00
|
|
|
"net/url"
|
2018-03-26 00:03:56 +00:00
|
|
|
"os"
|
2018-04-06 08:27:25 +00:00
|
|
|
"regexp"
|
2017-08-20 05:09:36 +00:00
|
|
|
"strings"
|
2017-08-07 07:31:53 +00:00
|
|
|
"time"
|
|
|
|
|
2018-03-26 00:03:56 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-08-07 07:31:53 +00:00
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
|
|
|
|
"github.com/prologic/msgbus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultReconnectInterval ...
|
|
|
|
DefaultReconnectInterval = 5
|
|
|
|
|
|
|
|
// DefaultRetryInterval ...
|
|
|
|
DefaultRetryInterval = 5
|
|
|
|
)
|
|
|
|
|
2018-04-06 08:27:25 +00:00
|
|
|
var (
|
|
|
|
// PublishedRegexp ...
|
|
|
|
PublishedRegexp = regexp.MustCompile(
|
|
|
|
"message successfully published to \\w+ with sequence \\d",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
|
retry time.Duration
|
|
|
|
reconnect time.Duration
|
|
|
|
|
|
|
|
ws *websocket.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options ...
|
|
|
|
type Options struct {
|
|
|
|
ReconnectInterval int
|
|
|
|
RetryInterval int
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (
|
|
|
|
reconnectInterval = DefaultReconnectInterval
|
|
|
|
retryInterval = DefaultRetryInterval
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.RetryInterval != 0 {
|
|
|
|
retryInterval = options.RetryInterval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client.reconnect = time.Duration(reconnectInterval) * time.Second
|
|
|
|
client.retry = time.Duration(retryInterval) * time.Second
|
|
|
|
|
|
|
|
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 ...
|
|
|
|
func (c *Client) Pull(topic string) {
|
|
|
|
var msg *msgbus.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{}
|
|
|
|
|
|
|
|
for {
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
2018-04-06 08:27:25 +00:00
|
|
|
log.Printf("error constructing request to %s: %s", url, err)
|
2017-08-07 07:31:53 +00:00
|
|
|
time.Sleep(c.retry)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil {
|
2018-04-06 08:27:25 +00:00
|
|
|
log.Printf("error sending request to %s: %s", url, err)
|
2017-08-07 07:31:53 +00:00
|
|
|
time.Sleep(c.retry)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.StatusCode == http.StatusNotFound {
|
2018-03-26 00:11:28 +00:00
|
|
|
// Empty queue
|
2017-08-07 07:31:53 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
err = json.NewDecoder(res.Body).Decode(&msg)
|
|
|
|
if err != nil {
|
2018-03-26 00:11:28 +00:00
|
|
|
log.Errorf(
|
2017-08-07 07:31:53 +00:00
|
|
|
"error decoding response from %s for %s: %s",
|
|
|
|
url, topic, err,
|
|
|
|
)
|
|
|
|
break
|
|
|
|
} else {
|
2018-03-26 00:11:28 +00:00
|
|
|
err := c.Handle(msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf(
|
|
|
|
"error handling message from %s for %s: %s",
|
|
|
|
url, topic, err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
break
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("unexpected non-200 response: %s", res.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error reading response: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !PublishedRegexp.Match(body) {
|
|
|
|
return fmt.Errorf("unexpected non-matching response: %s", body)
|
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 {
|
2017-08-19 07:15:45 +00:00
|
|
|
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
|
|
|
|
2017-08-19 07:15:45 +00:00
|
|
|
errch chan error
|
|
|
|
stopch 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
|
|
|
|
}
|
|
|
|
return &Subscriber{
|
|
|
|
client: client,
|
|
|
|
topic: topic,
|
|
|
|
handler: handler,
|
|
|
|
errch: make(chan error),
|
|
|
|
stopch: make(chan bool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 07:31:53 +00:00
|
|
|
// Stop ...
|
|
|
|
func (s *Subscriber) Stop() {
|
2017-08-19 07:15:45 +00:00
|
|
|
close(s.errch)
|
|
|
|
s.stopch <- true
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run ...
|
|
|
|
func (s *Subscriber) Run() {
|
2017-08-19 07:15:45 +00:00
|
|
|
var err error
|
2017-08-07 07:31:53 +00:00
|
|
|
|
|
|
|
origin := "http://localhost/"
|
|
|
|
|
2017-08-20 05:09:36 +00:00
|
|
|
u, err := url.Parse(s.client.url)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("invalid url: %s", s.client.url)
|
|
|
|
}
|
|
|
|
|
2018-05-01 06:08:44 +00:00
|
|
|
if strings.HasPrefix(s.client.url, "https") {
|
|
|
|
u.Scheme = "wss"
|
|
|
|
} else {
|
|
|
|
u.Scheme = "ws"
|
|
|
|
}
|
|
|
|
|
2017-08-20 05:09:36 +00:00
|
|
|
u.Path += fmt.Sprintf("/%s", s.topic)
|
|
|
|
|
|
|
|
url := u.String()
|
2017-08-07 07:31:53 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
s.conn, err = websocket.Dial(url, "", origin)
|
|
|
|
if err != nil {
|
2018-03-26 00:03:56 +00:00
|
|
|
log.Warnf("error connecting to %s: %s", url, err)
|
2017-08-07 07:31:53 +00:00
|
|
|
time.Sleep(s.client.reconnect)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-08-19 07:15:45 +00:00
|
|
|
go s.Reader()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err = <-s.errch:
|
2017-08-07 07:31:53 +00:00
|
|
|
if err != nil {
|
2018-03-26 00:03:56 +00:00
|
|
|
log.Warnf("lost connection to %s: %s", url, err)
|
2017-08-07 07:31:53 +00:00
|
|
|
time.Sleep(s.client.reconnect)
|
|
|
|
}
|
2017-08-19 07:15:45 +00:00
|
|
|
case <-s.stopch:
|
2018-03-26 00:03:56 +00:00
|
|
|
log.Infof("shutting down ...")
|
2017-08-19 07:15:45 +00:00
|
|
|
s.conn.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reader ...
|
|
|
|
func (s *Subscriber) Reader() {
|
|
|
|
var msg *msgbus.Message
|
|
|
|
|
|
|
|
for {
|
|
|
|
err := websocket.JSON.Receive(s.conn, &msg)
|
|
|
|
if err != nil {
|
|
|
|
s.errch <- err
|
|
|
|
break
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
2018-03-27 08:40:03 +00:00
|
|
|
err = s.handler(msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error handling message: %s", err)
|
|
|
|
}
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
|
|
|
}
|