mirror of
https://github.com/tailscale/tailscale.git
synced 2025-10-27 20:08:40 +00:00
net/netmon: remove usage of direct callbacks from netmon (#17292)
The callback itself is not removed as it is used in other repos, making it simpler for those to slowly transition to the eventbus. Updates #15160 Signed-off-by: Claus Lensbøl <claus@tailscale.com>
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"tailscale.com/tstime"
|
||||
"tailscale.com/types/logid"
|
||||
"tailscale.com/util/eventbus"
|
||||
)
|
||||
|
||||
// DefaultHost is the default host name to upload logs to when
|
||||
@@ -34,6 +35,7 @@ type Config struct {
|
||||
LowMemory bool // if true, logtail minimizes memory use
|
||||
Clock tstime.Clock // if set, Clock.Now substitutes uses of time.Now
|
||||
Stderr io.Writer // if set, logs are sent here instead of os.Stderr
|
||||
Bus *eventbus.Bus // if set, uses the eventbus for awaitInternetUp instead of callback
|
||||
StderrLevel int // max verbosity level to write to stderr; 0 means the non-verbose messages only
|
||||
Buffer Buffer // temp storage, if nil a MemoryBuffer
|
||||
CompressLogs bool // whether to compress the log uploads
|
||||
|
||||
@@ -32,6 +32,7 @@ import (
|
||||
"tailscale.com/tstime"
|
||||
tslogger "tailscale.com/types/logger"
|
||||
"tailscale.com/types/logid"
|
||||
"tailscale.com/util/eventbus"
|
||||
"tailscale.com/util/set"
|
||||
"tailscale.com/util/truncate"
|
||||
"tailscale.com/util/zstdframe"
|
||||
@@ -120,6 +121,10 @@ func NewLogger(cfg Config, logf tslogger.Logf) *Logger {
|
||||
shutdownStart: make(chan struct{}),
|
||||
shutdownDone: make(chan struct{}),
|
||||
}
|
||||
|
||||
if cfg.Bus != nil {
|
||||
l.eventClient = cfg.Bus.Client("logtail.Logger")
|
||||
}
|
||||
l.SetSockstatsLabel(sockstats.LabelLogtailLogger)
|
||||
l.compressLogs = cfg.CompressLogs
|
||||
|
||||
@@ -156,6 +161,7 @@ type Logger struct {
|
||||
privateID logid.PrivateID
|
||||
httpDoCalls atomic.Int32
|
||||
sockstatsLabel atomicSocktatsLabel
|
||||
eventClient *eventbus.Client
|
||||
|
||||
procID uint32
|
||||
includeProcSequence bool
|
||||
@@ -221,6 +227,9 @@ func (l *Logger) Shutdown(ctx context.Context) error {
|
||||
l.httpc.CloseIdleConnections()
|
||||
}()
|
||||
|
||||
if l.eventClient != nil {
|
||||
l.eventClient.Close()
|
||||
}
|
||||
l.shutdownStartMu.Lock()
|
||||
select {
|
||||
case <-l.shutdownStart:
|
||||
@@ -417,6 +426,10 @@ func (l *Logger) internetUp() bool {
|
||||
}
|
||||
|
||||
func (l *Logger) awaitInternetUp(ctx context.Context) {
|
||||
if l.eventClient != nil {
|
||||
l.awaitInternetUpBus(ctx)
|
||||
return
|
||||
}
|
||||
upc := make(chan bool, 1)
|
||||
defer l.netMonitor.RegisterChangeCallback(func(delta *netmon.ChangeDelta) {
|
||||
if delta.New.AnyInterfaceUp() {
|
||||
@@ -436,6 +449,24 @@ func (l *Logger) awaitInternetUp(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) awaitInternetUpBus(ctx context.Context) {
|
||||
if l.internetUp() {
|
||||
return
|
||||
}
|
||||
sub := eventbus.Subscribe[netmon.ChangeDelta](l.eventClient)
|
||||
defer sub.Close()
|
||||
select {
|
||||
case delta := <-sub.Events():
|
||||
if delta.New.AnyInterfaceUp() {
|
||||
fmt.Fprintf(l.stderr, "logtail: internet back up\n")
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(l.stderr, "logtail: network changed, but is not up")
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// upload uploads body to the log server.
|
||||
// origlen indicates the pre-compression body length.
|
||||
// origlen of -1 indicates that the body is not compressed.
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/go-json-experiment/json/jsontext"
|
||||
"tailscale.com/tstest"
|
||||
"tailscale.com/tstime"
|
||||
"tailscale.com/util/eventbus/eventbustest"
|
||||
"tailscale.com/util/must"
|
||||
)
|
||||
|
||||
@@ -30,6 +31,7 @@ func TestFastShutdown(t *testing.T) {
|
||||
|
||||
l := NewLogger(Config{
|
||||
BaseURL: testServ.URL,
|
||||
Bus: eventbustest.NewBus(t),
|
||||
}, t.Logf)
|
||||
err := l.Shutdown(ctx)
|
||||
if err != nil {
|
||||
@@ -62,7 +64,10 @@ func NewLogtailTestHarness(t *testing.T) (*LogtailTestServer, *Logger) {
|
||||
|
||||
t.Cleanup(ts.srv.Close)
|
||||
|
||||
l := NewLogger(Config{BaseURL: ts.srv.URL}, t.Logf)
|
||||
l := NewLogger(Config{
|
||||
BaseURL: ts.srv.URL,
|
||||
Bus: eventbustest.NewBus(t),
|
||||
}, t.Logf)
|
||||
|
||||
// There is always an initial "logtail started" message
|
||||
body := <-ts.uploaded
|
||||
|
||||
Reference in New Issue
Block a user