6
0
mirror of https://git.mills.io/prologic/msgbus.git synced 2024-06-25 00:09:08 +00:00
prologic-msgbus/client/client.go

291 lines
5.3 KiB
Go
Raw Normal View History

package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
2017-08-20 05:09:36 +00:00
"net/url"
"strconv"
2017-08-20 05:09:36 +00:00
"strings"
"time"
2018-05-05 21:08:39 +00:00
"github.com/jpillora/backoff"
sync "github.com/sasha-s/go-deadlock"
2018-03-26 00:03:56 +00:00
log "github.com/sirupsen/logrus"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
2021-07-12 21:57:54 +00:00
"git.mills.io/prologic/msgbus"
)
const (
// DefaultReconnectInterval ...
2018-05-05 21:08:39 +00:00
DefaultReconnectInterval = 2
2018-05-05 21:08:39 +00:00
// DefaultMaxReconnectInterval ...
DefaultMaxReconnectInterval = 64
// DefaultPingInterval is the default time interval between pings
DefaultPingInterval = 60 * time.Second
)
2022-03-27 15:49:03 +00:00
func noopHandler(msg *msgbus.Message) error { return nil }
// Client ...
type Client struct {
2022-03-20 14:29:02 +00:00
sync.RWMutex
2017-08-20 05:09:36 +00:00
url string
2018-05-05 21:08:39 +00:00
reconnectInterval time.Duration
maxReconnectInterval time.Duration
}
// Options ...
type Options struct {
2018-05-05 21:08:39 +00:00
ReconnectInterval int
MaxReconnectInterval int
}
// NewClient ...
2017-08-20 05:09:36 +00:00
func NewClient(url string, options *Options) *Client {
var (
2018-05-05 21:08:39 +00:00
reconnectInterval = DefaultReconnectInterval
maxReconnectInterval = DefaultMaxReconnectInterval
)
2017-08-20 05:09:36 +00:00
url = strings.TrimSuffix(url, "/")
client := &Client{url: url}
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
}
}
2018-05-05 21:08:39 +00:00
client.reconnectInterval = time.Duration(reconnectInterval) * time.Second
client.maxReconnectInterval = time.Duration(maxReconnectInterval) * time.Second
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)
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
}
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
}
// XXX: StatusNotFound is for backwards compatibility only for older clients.
if res.StatusCode == http.StatusNoContent || res.StatusCode == http.StatusNotFound {
2018-05-05 21:08:39 +00:00
// Empty queue
2022-03-20 16:12:31 +00:00
return nil, nil
2018-05-05 21:08:39 +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
}
// Publish ...
func (c *Client) Publish(topic, message string) error {
2022-03-20 14:29:02 +00:00
c.RLock()
defer c.RUnlock()
var payload bytes.Buffer
payload.Write([]byte(message))
2017-08-20 05:09:36 +00:00
url := fmt.Sprintf("%s/%s", c.url, topic)
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)
}
2018-04-06 08:27:25 +00:00
res, err := client.Do(req)
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)
}
return nil
}
// Subscribe ...
func (c *Client) Subscribe(topic string, index int, handler msgbus.HandlerFunc) *Subscriber {
return NewSubscriber(c, topic, index, handler)
}
// Subscriber ...
type Subscriber struct {
2018-05-05 21:08:39 +00:00
sync.RWMutex
conn *websocket.Conn
client *Client
topic string
index int
handler msgbus.HandlerFunc
2018-05-05 21:08:39 +00:00
url string
reconnectInterval time.Duration
maxReconnectInterval time.Duration
}
// NewSubscriber ...
func NewSubscriber(client *Client, topic string, index int, handler msgbus.HandlerFunc) *Subscriber {
if handler == nil {
2022-03-27 15:49:03 +00:00
handler = noopHandler
}
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 {
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") {
u.Scheme = "wss"
} else {
u.Scheme = "ws"
}
2018-05-05 21:08:39 +00:00
u.Path += fmt.Sprintf("/%s", topic)
q := u.Query()
q.Set("index", strconv.Itoa(index))
u.RawQuery = q.Encode()
2017-08-20 05:09:36 +00:00
url := u.String()
2018-05-05 21:08:39 +00:00
return &Subscriber{
client: client,
topic: topic,
index: index,
2018-05-05 21:08:39 +00:00
handler: handler,
url: url,
reconnectInterval: client.reconnectInterval,
maxReconnectInterval: client.maxReconnectInterval,
}
}
func (s *Subscriber) closeAndReconnect() {
2022-03-20 14:29:02 +00:00
s.RLock()
if s.conn != nil {
s.conn.Close(websocket.StatusNormalClosure, "Closing and reconnecting...")
go s.connect()
}
2022-03-20 14:29:02 +00:00
s.RUnlock()
2018-05-05 21:08:39 +00:00
}
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()
for {
2022-03-27 04:47:14 +00:00
conn, _, err := websocket.Dial(ctx, s.url, nil)
if err != nil {
time.Sleep(b.Duration())
2018-05-05 21:08:39 +00:00
continue
}
s.Lock()
2018-05-05 21:08:39 +00:00
s.conn = conn
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
}
}
2022-03-27 04:47:14 +00:00
func (s *Subscriber) readLoop(ctx context.Context) {
var msg *msgbus.Message
for {
2022-03-27 04:47:14 +00:00
err := wsjson.Read(ctx, s.conn, &msg)
if err != nil {
s.closeAndReconnect()
2018-05-05 21:08:39 +00:00
return
}
2018-05-05 21:08:39 +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-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
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 {
s.closeAndReconnect()
2022-03-27 04:47:14 +00:00
}
t.Reset(time.Minute)
}
}