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:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user