/
publish_handler.go
163 lines (145 loc) · 5.13 KB
/
publish_handler.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package proxy
import (
"encoding/base64"
"time"
"github.com/centrifugal/centrifugo/v5/internal/proxyproto"
"github.com/centrifugal/centrifugo/v5/internal/rule"
"github.com/centrifugal/centrifuge"
"github.com/prometheus/client_golang/prometheus"
)
// PublishHandlerConfig ...
type PublishHandlerConfig struct {
Proxies map[string]PublishProxy
GranularProxyMode bool
}
// PublishHandler ...
type PublishHandler struct {
config PublishHandlerConfig
summary prometheus.Observer
histogram prometheus.Observer
errors prometheus.Counter
granularSummary map[string]prometheus.Observer
granularHistogram map[string]prometheus.Observer
granularErrors map[string]prometheus.Counter
}
// NewPublishHandler ...
func NewPublishHandler(c PublishHandlerConfig) *PublishHandler {
h := &PublishHandler{
config: c,
}
if h.config.GranularProxyMode {
summary := map[string]prometheus.Observer{}
histogram := map[string]prometheus.Observer{}
errors := map[string]prometheus.Counter{}
for k := range c.Proxies {
name := k
if name == "" {
name = "__default__"
}
summary[name] = granularProxyCallDurationSummary.WithLabelValues("publish", name)
histogram[name] = granularProxyCallDurationHistogram.WithLabelValues("publish", name)
errors[name] = granularProxyCallErrorCount.WithLabelValues("publish", name)
}
h.granularSummary = summary
h.granularHistogram = histogram
h.granularErrors = errors
} else {
h.summary = proxyCallDurationSummary.WithLabelValues(h.config.Proxies[""].Protocol(), "publish")
h.histogram = proxyCallDurationHistogram.WithLabelValues(h.config.Proxies[""].Protocol(), "publish")
h.errors = proxyCallErrorCount.WithLabelValues(h.config.Proxies[""].Protocol(), "publish")
}
return h
}
// PublishHandlerFunc ...
type PublishHandlerFunc func(Client, centrifuge.PublishEvent, rule.ChannelOptions, PerCallData) (centrifuge.PublishReply, error)
// Handle Publish.
func (h *PublishHandler) Handle(node *centrifuge.Node) PublishHandlerFunc {
return func(client Client, e centrifuge.PublishEvent, chOpts rule.ChannelOptions, pcd PerCallData) (centrifuge.PublishReply, error) {
started := time.Now()
var p PublishProxy
var summary prometheus.Observer
var histogram prometheus.Observer
var errors prometheus.Counter
if h.config.GranularProxyMode {
proxyName := chOpts.PublishProxyName
if proxyName == "" {
node.Log(centrifuge.NewLogEntry(centrifuge.LogLevelInfo, "publish proxy not configured for a channel", map[string]any{"channel": e.Channel}))
return centrifuge.PublishReply{}, centrifuge.ErrorNotAvailable
}
p = h.config.Proxies[proxyName]
summary = h.granularSummary[proxyName]
histogram = h.granularHistogram[proxyName]
errors = h.granularErrors[proxyName]
} else {
p = h.config.Proxies[""]
summary = h.summary
histogram = h.histogram
errors = h.errors
}
req := &proxyproto.PublishRequest{
Client: client.ID(),
Protocol: string(client.Transport().Protocol()),
Transport: client.Transport().Name(),
Encoding: getEncoding(p.UseBase64()),
User: client.UserID(),
Channel: e.Channel,
}
if p.IncludeMeta() && pcd.Meta != nil {
req.Meta = proxyproto.Raw(pcd.Meta)
}
if !p.UseBase64() {
req.Data = e.Data
} else {
req.B64Data = base64.StdEncoding.EncodeToString(e.Data)
}
publishRep, err := p.ProxyPublish(client.Context(), req)
duration := time.Since(started).Seconds()
if err != nil {
select {
case <-client.Context().Done():
// Client connection already closed.
return centrifuge.PublishReply{}, centrifuge.DisconnectConnectionClosed
default:
}
summary.Observe(duration)
histogram.Observe(duration)
errors.Inc()
node.Log(centrifuge.NewLogEntry(centrifuge.LogLevelError, "error proxying publish", map[string]any{"error": err.Error()}))
return centrifuge.PublishReply{}, centrifuge.ErrorInternal
}
summary.Observe(duration)
histogram.Observe(duration)
if publishRep.Disconnect != nil {
return centrifuge.PublishReply{}, proxyproto.DisconnectFromProto(publishRep.Disconnect)
}
if publishRep.Error != nil {
return centrifuge.PublishReply{}, proxyproto.ErrorFromProto(publishRep.Error)
}
historySize := chOpts.HistorySize
historyTTL := chOpts.HistoryTTL
historyMetaTTL := chOpts.HistoryMetaTTL
data := e.Data
if publishRep.Result != nil {
if publishRep.Result.Data != nil {
data = publishRep.Result.Data
} else if publishRep.Result.B64Data != "" {
decodedData, err := base64.StdEncoding.DecodeString(publishRep.Result.B64Data)
if err != nil {
node.Log(centrifuge.NewLogEntry(centrifuge.LogLevelError, "error decoding base64 data", map[string]any{"client": client.ID(), "error": err.Error()}))
return centrifuge.PublishReply{}, centrifuge.ErrorInternal
}
data = decodedData
}
if publishRep.Result.SkipHistory {
historySize = 0
historyTTL = 0
}
}
result, err := node.Publish(
e.Channel, data,
centrifuge.WithClientInfo(e.ClientInfo),
centrifuge.WithHistory(historySize, time.Duration(historyTTL), time.Duration(historyMetaTTL)),
)
return centrifuge.PublishReply{Result: &result}, err
}
}