mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 20:50:00 +00:00
TUN-3738: Refactor observer to avoid potential of blocking on tunnel notifications
This commit is contained in:

committed by
Arég Harutyunyan

parent
8c9d725eeb
commit
04b1e4f859
@@ -27,13 +27,7 @@ var (
|
||||
Scheme: "https",
|
||||
Host: "connectiontest.argotunnel.com",
|
||||
}
|
||||
testTunnelEventChan = make(chan Event)
|
||||
testObserver = &Observer{
|
||||
&log,
|
||||
m,
|
||||
[]chan Event{testTunnelEventChan},
|
||||
false,
|
||||
}
|
||||
testObserver = NewObserver(&log, false)
|
||||
testLargeResp = make([]byte, largeFileSize)
|
||||
)
|
||||
|
||||
|
@@ -299,7 +299,7 @@ func convertRTTMilliSec(t time.Duration) float64 {
|
||||
}
|
||||
|
||||
// Metrics that can be collected without asking the edge
|
||||
func newTunnelMetrics() *tunnelMetrics {
|
||||
func initTunnelMetrics() *tunnelMetrics {
|
||||
maxConcurrentRequestsPerTunnel := prometheus.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: MetricsNamespace,
|
||||
@@ -403,3 +403,15 @@ func (t *tunnelMetrics) registerServerLocation(connectionID, loc string) {
|
||||
t.serverLocations.WithLabelValues(connectionID, loc).Inc()
|
||||
t.oldServerLocations[connectionID] = loc
|
||||
}
|
||||
|
||||
var tunnelMetricsInternal struct {
|
||||
sync.Once
|
||||
metrics *tunnelMetrics
|
||||
}
|
||||
|
||||
func newTunnelMetrics() *tunnelMetrics {
|
||||
tunnelMetricsInternal.Do(func() {
|
||||
tunnelMetricsInternal.metrics = initTunnelMetrics()
|
||||
})
|
||||
return tunnelMetricsInternal.metrics
|
||||
}
|
@@ -10,22 +10,37 @@ import (
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
const LogFieldLocation = "location"
|
||||
const (
|
||||
LogFieldLocation = "location"
|
||||
observerChannelBufferSize = 16
|
||||
)
|
||||
|
||||
type Observer struct {
|
||||
log *zerolog.Logger
|
||||
metrics *tunnelMetrics
|
||||
tunnelEventChans []chan Event
|
||||
uiEnabled bool
|
||||
log *zerolog.Logger
|
||||
metrics *tunnelMetrics
|
||||
tunnelEventChan chan Event
|
||||
uiEnabled bool
|
||||
addSinkChan chan EventSink
|
||||
}
|
||||
|
||||
func NewObserver(log *zerolog.Logger, tunnelEventChans []chan Event, uiEnabled bool) *Observer {
|
||||
return &Observer{
|
||||
log,
|
||||
newTunnelMetrics(),
|
||||
tunnelEventChans,
|
||||
uiEnabled,
|
||||
type EventSink interface {
|
||||
OnTunnelEvent(event Event)
|
||||
}
|
||||
|
||||
func NewObserver(log *zerolog.Logger, uiEnabled bool) *Observer {
|
||||
o := &Observer{
|
||||
log: log,
|
||||
metrics: newTunnelMetrics(),
|
||||
uiEnabled: uiEnabled,
|
||||
tunnelEventChan: make(chan Event, observerChannelBufferSize),
|
||||
addSinkChan: make(chan EventSink, observerChannelBufferSize),
|
||||
}
|
||||
go o.dispatchEvents()
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *Observer) RegisterSink(sink EventSink) {
|
||||
o.addSinkChan <- sink
|
||||
}
|
||||
|
||||
func (o *Observer) logServerInfo(connIndex uint8, location, msg string) {
|
||||
@@ -105,7 +120,30 @@ func (o *Observer) SendDisconnect(connIndex uint8) {
|
||||
}
|
||||
|
||||
func (o *Observer) sendEvent(e Event) {
|
||||
for _, ch := range o.tunnelEventChans {
|
||||
ch <- e
|
||||
select {
|
||||
case o.tunnelEventChan <- e:
|
||||
break
|
||||
default:
|
||||
o.log.Warn().Msg("observer channel buffer is full")
|
||||
}
|
||||
}
|
||||
|
||||
func (o *Observer) dispatchEvents() {
|
||||
var sinks []EventSink
|
||||
for {
|
||||
select {
|
||||
case sink := <-o.addSinkChan:
|
||||
sinks = append(sinks, sink)
|
||||
case evt := <-o.tunnelEventChan:
|
||||
for _, sink := range sinks {
|
||||
sink.OnTunnelEvent(evt)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type EventSinkFunc func(event Event)
|
||||
|
||||
func (f EventSinkFunc) OnTunnelEvent(event Event) {
|
||||
f(event)
|
||||
}
|
||||
|
@@ -4,14 +4,13 @@ import (
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// can only be called once
|
||||
var m = newTunnelMetrics()
|
||||
|
||||
func TestRegisterServerLocation(t *testing.T) {
|
||||
m := newTunnelMetrics()
|
||||
tunnels := 20
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(tunnels)
|
||||
@@ -43,3 +42,27 @@ func TestRegisterServerLocation(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestObserverEventsDontBlock(t *testing.T) {
|
||||
observer := NewObserver(&log, false)
|
||||
var mu sync.Mutex
|
||||
observer.RegisterSink(EventSinkFunc(func(_ Event) {
|
||||
// callback will block if lock is already held
|
||||
mu.Lock()
|
||||
mu.Unlock()
|
||||
}))
|
||||
|
||||
timeout := time.AfterFunc(5*time.Second, func() {
|
||||
mu.Unlock() // release the callback on timer expiration
|
||||
t.Fatal("observer is blocked")
|
||||
})
|
||||
|
||||
mu.Lock() // block the callback
|
||||
for i := 0; i < 2 * observerChannelBufferSize; i++ {
|
||||
observer.sendRegisteringEvent()
|
||||
}
|
||||
if pending := timeout.Stop(); pending {
|
||||
// release the callback if timer hasn't expired yet
|
||||
mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user