mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-05-11 08:06:35 +00:00

Connections from cloudflared to Cloudflare edge are long lived and may break over time. That is expected for many reasons (ranging from network conditions to operations within Cloudflare edge). Hence, logging that as Error feels too strong and leads to users being concerned that something is failing when it is actually expected. With this change, we wrap logging about connection issues to be aware of the tunnel state: - if the tunnel has no connections active, we log as error - otherwise we log as warning
56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package tunnelstate
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/cloudflare/cloudflared/connection"
|
|
)
|
|
|
|
type ConnTracker struct {
|
|
sync.RWMutex
|
|
isConnected map[int]bool
|
|
log *zerolog.Logger
|
|
}
|
|
|
|
func NewConnTracker(log *zerolog.Logger) *ConnTracker {
|
|
return &ConnTracker{
|
|
isConnected: make(map[int]bool, 0),
|
|
log: log,
|
|
}
|
|
}
|
|
|
|
func MockedConnTracker(mocked map[int]bool) *ConnTracker {
|
|
return &ConnTracker{
|
|
isConnected: mocked,
|
|
}
|
|
}
|
|
|
|
func (ct *ConnTracker) OnTunnelEvent(c connection.Event) {
|
|
switch c.EventType {
|
|
case connection.Connected:
|
|
ct.Lock()
|
|
ct.isConnected[int(c.Index)] = true
|
|
ct.Unlock()
|
|
case connection.Disconnected, connection.Reconnecting, connection.RegisteringTunnel, connection.Unregistering:
|
|
ct.Lock()
|
|
ct.isConnected[int(c.Index)] = false
|
|
ct.Unlock()
|
|
default:
|
|
ct.log.Error().Msgf("Unknown connection event case %v", c)
|
|
}
|
|
}
|
|
|
|
func (ct *ConnTracker) CountActiveConns() uint {
|
|
ct.RLock()
|
|
defer ct.RUnlock()
|
|
active := uint(0)
|
|
for _, connected := range ct.isConnected {
|
|
if connected {
|
|
active++
|
|
}
|
|
}
|
|
return active
|
|
}
|