Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion supervisor/fuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (f *booleanFuse) Value() bool {
return f.value == 1
}

func (f *booleanFuse) Fuse(result bool) {
func (f *booleanFuse) Fuse(result bool) bool {
f.mu.Lock()
defer f.mu.Unlock()
newValue := int32(2)
Expand All @@ -36,7 +36,9 @@ func (f *booleanFuse) Fuse(result bool) {
if f.value == 0 {
f.value = newValue
f.cond.Broadcast()
return true
}
return false
}

// Await blocks until Fuse has been called at least once.
Expand Down
12 changes: 8 additions & 4 deletions supervisor/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,12 @@ type TunnelServer interface {
}

func (e *EdgeTunnelServer) Serve(ctx context.Context, connIndex uint8, protocolFallback *protocolFallback, connectedSignal *signal.Signal) error {
haConnections.Inc()
defer haConnections.Dec()

connectedFuse := newBooleanFuse()
defer func() {
if connectedFuse.Value() {
haConnections.Dec()
}
}()
go func() {
if connectedFuse.Await() {
connectedSignal.Notify()
Expand Down Expand Up @@ -702,7 +704,9 @@ type connectedFuse struct {
}

func (cf *connectedFuse) Connected() {
cf.fuse.Fuse(true)
if cf.fuse.Fuse(true) {
haConnections.Inc()
}
cf.backoff.reset()
}

Expand Down
36 changes: 36 additions & 0 deletions supervisor/tunnel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"testing"
"time"

dto "github.com/prometheus/client_model/go"
"github.com/quic-go/quic-go"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/cloudflare/cloudflared/connection"
"github.com/cloudflare/cloudflared/edgediscovery"
Expand All @@ -18,6 +20,40 @@ type dynamicMockFetcher struct {
err error
}

func TestConnectedFuseUpdatesHAConnectionsOnce(t *testing.T) {
fuse := newBooleanFuse()
connectedFuse := &connectedFuse{
fuse: fuse,
backoff: &protocolFallback{
BackoffHandler: retry.NewBackoff(3, time.Millisecond, false),
},
}
initial := haConnectionsValue(t)
defer haConnections.Set(initial)

connectedFuse.Connected()
assert.Equal(t, initial+1, haConnectionsValue(t))

connectedFuse.Connected()
assert.Equal(t, initial+1, haConnectionsValue(t))
}

func TestUnconnectedFuseDoesNotUpdateHAConnections(t *testing.T) {
initial := haConnectionsValue(t)
defer haConnections.Set(initial)

fuse := newBooleanFuse()
fuse.Fuse(false)

assert.Equal(t, initial, haConnectionsValue(t))
}

func haConnectionsValue(t *testing.T) float64 {
var metric dto.Metric
require.NoError(t, haConnections.Write(&metric))
return metric.GetGauge().GetValue()
}

func (dmf *dynamicMockFetcher) fetch() edgediscovery.PercentageFetcher {
return func() (edgediscovery.ProtocolPercents, error) {
return dmf.protocolPercents, dmf.err
Expand Down