Files
NTrace-core/trace/probe_setup.go
T
tsosunchia 205bdaa7f1 feat(mtr): 支持实时 NDJSON 与最终 JSON 报告 (#230)
- 为 full、tiny 和 ntr 提供 MTR JSON 输出并保留传统模式
- 传播初始化和监听错误,完善取消、资源清理及非零退出
- 补充事件顺序、信号处理和跨平台回归测试及文档
2026-09-07 14:35:59 +08:00

62 lines
1.5 KiB
Go

package trace
import (
"context"
"errors"
"os"
"os/signal"
"syscall"
"time"
"github.com/nxtrace/NTrace-core/trace/internal"
)
// probeSetupError cannot be recovered by retrying a TTL or counting a timeout.
type probeSetupError = internal.InitializationError
// IsInitializationError reports failures to initialize or replace probe resources.
func IsInitializationError(err error) bool {
var setup *probeSetupError
return errors.As(err, &setup)
}
// A caller-supplied context owns cancellation, including the CLI's signal cause.
func traceSignalContext(parent context.Context) (context.Context, context.CancelFunc) {
if parent != nil {
return context.WithCancel(parent)
}
return signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
}
func wrapProbeSetupError(err error) error {
if err == nil {
return nil
}
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return err
}
var setup *probeSetupError
if errors.As(err, &setup) {
return err
}
return &probeSetupError{Err: err}
}
func waitProbeListeners(ctx context.Context, ready ...chan struct{}) error {
timer := time.NewTimer(5 * time.Second)
defer timer.Stop()
for _, ch := range ready {
select {
case <-ctx.Done():
return context.Cause(ctx)
case <-timer.C:
return wrapProbeSetupError(errors.New("probe listener startup timeout"))
case <-ch:
}
}
if !waitForTraceDelay(ctx, 100*time.Millisecond) {
return context.Cause(ctx)
}
return nil
}