2017-08-07 07:31:53 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-03-25 12:35:28 +00:00
|
|
|
"context"
|
2017-08-07 07:31:53 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-08-20 05:09:36 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2018-05-05 21:08:39 +00:00
|
|
|
"sync"
|
2017-08-07 07:31:53 +00:00
|
|
|
"time"
|
|
|
|
|
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"
|
2022-03-25 12:35:28 +00:00
|
|
|
"nhooyr.io/websocket"
|
|
|
|
"nhooyr.io/websocket/wsjson"
|
2017-08-07 07:31:53 +00:00
|
|
|
|
2021-07-12 21:57:54 +00:00
|
|
|
"git.mills.io/prologic/msgbus"
|
2017-08-07 07:31:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-03-25 12:35:28 +00:00
|
|
|
// DefaultPingInterval is the default time interval between pings
|
|
|
|
DefaultPingInterval = 60 * time.Second
|
2017-08-07 07:31:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client ...
|
|
|
|
type Client struct {
|
2022-03-20 14:29:02 +00:00
|
|
|
sync.RWMutex
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull ...
|
2018-05-05 21:08:39 +00:00
|
|
|
func (c *Client) Pull(topic string) (msg *msgbus.Message, err error) {
|
2022-03-20 14:29:02 +00:00
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
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 {
|
2022-03-20 16:12:31 +00:00
|
|
|
return nil, err
|
2018-05-05 21:08:39 +00:00
|
|
|
}
|
2017-08-07 07:31:53 +00:00
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil {
|
2022-03-20 16:12:31 +00:00
|
|
|
return nil, err
|
2018-05-05 21:08:39 +00:00
|
|
|
}
|
2017-08-07 07:31:53 +00:00
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
if res.StatusCode == http.StatusNotFound {
|
|
|
|
// Empty queue
|
2022-03-20 16:12:31 +00:00
|
|
|
return nil, nil
|
2018-05-05 21:08:39 +00:00
|
|
|
}
|
2017-08-07 07:31:53 +00:00
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
defer res.Body.Close()
|
2022-03-20 16:12:31 +00:00
|
|
|
if err := json.NewDecoder(res.Body).Decode(&msg); err != nil {
|
|
|
|
return nil, err
|
2018-05-05 21:08:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 16:12:31 +00:00
|
|
|
return msg, nil
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Publish ...
|
|
|
|
func (c *Client) Publish(topic, message string) error {
|
2022-03-20 14:29:02 +00:00
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
2017-08-07 07:31:53 +00:00
|
|
|
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
|
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-12-31 08:05:23 +00:00
|
|
|
log.Fatalf("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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subscriber) closeAndReconnect() {
|
2022-03-20 14:29:02 +00:00
|
|
|
s.RLock()
|
2022-03-25 12:35:28 +00:00
|
|
|
s.conn.Close(websocket.StatusNormalClosure, "Closing and reconnecting...")
|
2022-03-20 14:29:02 +00:00
|
|
|
s.RUnlock()
|
2018-05-05 21:08:39 +00:00
|
|
|
go s.connect()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subscriber) connect() {
|
2022-03-20 14:29:02 +00:00
|
|
|
s.RLock()
|
2018-05-05 21:08:39 +00:00
|
|
|
b := &backoff.Backoff{
|
|
|
|
Min: s.reconnectInterval,
|
|
|
|
Max: s.maxReconnectInterval,
|
|
|
|
Factor: 2,
|
|
|
|
Jitter: false,
|
|
|
|
}
|
2022-03-20 14:29:02 +00:00
|
|
|
s.RUnlock()
|
2018-05-05 21:08:39 +00:00
|
|
|
|
2022-03-27 04:47:14 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2017-08-07 07:31:53 +00:00
|
|
|
for {
|
2022-03-27 04:47:14 +00:00
|
|
|
conn, _, err := websocket.Dial(ctx, s.url, nil)
|
2017-08-07 07:31:53 +00:00
|
|
|
if err != nil {
|
2022-03-25 12:35:28 +00:00
|
|
|
time.Sleep(b.Duration())
|
2018-05-05 21:08:39 +00:00
|
|
|
continue
|
2017-08-07 07:31:53 +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-06 16:34:52 +00:00
|
|
|
s.Unlock()
|
2018-05-05 21:08:39 +00:00
|
|
|
|
2022-03-27 04:47:14 +00:00
|
|
|
go s.readLoop(ctx)
|
|
|
|
go s.heartbeat(ctx, DefaultPingInterval)
|
2018-05-05 21:08:39 +00:00
|
|
|
|
|
|
|
break
|
2017-08-19 07:15:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-27 04:47:14 +00:00
|
|
|
func (s *Subscriber) readLoop(ctx context.Context) {
|
2017-08-19 07:15:45 +00:00
|
|
|
var msg *msgbus.Message
|
|
|
|
|
|
|
|
for {
|
2022-03-27 04:47:14 +00:00
|
|
|
err := wsjson.Read(ctx, s.conn, &msg)
|
|
|
|
if err != nil {
|
2022-03-27 13:41:31 +00:00
|
|
|
if websocket.CloseStatus(err) != websocket.StatusNormalClosure {
|
|
|
|
s.closeAndReconnect()
|
|
|
|
}
|
2018-05-05 21:08:39 +00:00
|
|
|
return
|
2017-08-07 07:31:53 +00:00
|
|
|
}
|
2018-05-05 21:08:39 +00:00
|
|
|
|
2022-03-25 12:35:28 +00:00
|
|
|
if err := s.handler(msg); 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
|
|
|
|
|
|
|
// Start ...
|
|
|
|
func (s *Subscriber) Start() {
|
|
|
|
go s.connect()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop ...
|
|
|
|
func (s *Subscriber) Stop() {
|
2022-03-20 15:10:02 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2018-05-05 21:08:39 +00:00
|
|
|
|
2022-03-25 12:35:28 +00:00
|
|
|
if err := s.conn.Close(websocket.StatusNormalClosure, "Subscriber stopped"); err != nil {
|
2018-05-05 21:08:39 +00:00
|
|
|
log.Warnf("error sending close message: %s", err)
|
2022-03-27 04:47:14 +00:00
|
|
|
|
2018-05-05 21:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.conn = nil
|
|
|
|
}
|
2022-03-27 04:47:14 +00:00
|
|
|
|
|
|
|
func (s *Subscriber) heartbeat(ctx context.Context, d time.Duration) {
|
|
|
|
t := time.NewTimer(d)
|
|
|
|
defer t.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-t.C:
|
|
|
|
}
|
|
|
|
|
|
|
|
// c.Ping returns on receiving a pong
|
|
|
|
err := s.conn.Ping(ctx)
|
|
|
|
if err != nil {
|
2022-03-27 13:41:31 +00:00
|
|
|
if websocket.CloseStatus(err) != websocket.StatusNormalClosure {
|
|
|
|
s.closeAndReconnect()
|
|
|
|
}
|
|
|
|
return
|
2022-03-27 04:47:14 +00:00
|
|
|
}
|
|
|
|
t.Reset(time.Minute)
|
|
|
|
}
|
|
|
|
}
|