-
Notifications
You must be signed in to change notification settings - Fork 2
/
sender.go
65 lines (54 loc) · 1.61 KB
/
sender.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Copyright 2019 Adevinta
*/
package stream
import (
"net/http"
"time"
"github.com/danfaizer/gowse"
"github.com/sirupsen/logrus"
)
// SenderConfig defines required Vulcan websocket event server configuration
type SenderConfig struct {
HTTPStream string
PingInterval time.Duration
}
// Sender defines a websocket event server
type Sender struct {
topic *gowse.Topic
logger logrus.FieldLogger
config SenderConfig
}
// NewSender creates a Vulcan Stream sender instance
func NewSender(l logrus.FieldLogger, c SenderConfig) *Sender {
server := gowse.NewServer(l.WithFields(logrus.Fields{}))
topic := server.CreateTopic(c.HTTPStream)
return &Sender{topic: topic, logger: l, config: c}
}
// Start initializes a websocket event server instance with provided configuration
func (s *Sender) Start() {
go s.ping()
s.logger.Info("Vulcan Stream Sender started")
}
// HandleConn handles a connection to sender web socket topic.
func (s *Sender) HandleConn(w http.ResponseWriter, r *http.Request) {
if err := s.topic.SubscriberHandler(w, r); err != nil {
s.logger.Error("error handling subscriber request: %+v", err)
}
}
// Broadcast emits msg to the specified Stream channel
func (s *Sender) Broadcast(msg Message) {
s.topic.Broadcast(msg)
s.logger.WithFields(logrus.Fields{
"msg": msg,
}).Info("Message pushed to the stream successfully")
}
// ping starts a scheduler which will broadcast pings at configured interval
func (s *Sender) ping() {
pingMsg := Message{Action: "ping"}
ticker := time.NewTicker(s.config.PingInterval * time.Second)
defer ticker.Stop()
for range ticker.C {
s.topic.Broadcast(pingMsg)
}
}