ndt_runner.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // ndt_runner.go — proveedor M-Lab NDT (#1037): implementación del Runner
  2. // sobre github.com/m-lab/ndt7-client-go (cliente oficial ndt7, Go puro).
  3. // El descubrimiento del servidor lo hace la Locate API de M-Lab (el más
  4. // cercano sano); serverURL, si se indica, se interpreta como FQDN de un
  5. // servidor concreto (mismo contrato que el resto de runners).
  6. package speedtest
  7. import (
  8. "context"
  9. "fmt"
  10. "net/url"
  11. "strings"
  12. "time"
  13. ndt7 "github.com/m-lab/ndt7-client-go"
  14. "github.com/m-lab/ndt7-client-go/spec"
  15. )
  16. // NDTRunner es la implementación real (m-lab/ndt7-client-go). Sin estado:
  17. // segura para llamarla en serie desde el scheduler. El descubrimiento del
  18. // servidor lo hace la Locate API de M-Lab (el más cercano sano); serverURL,
  19. // si se indica, se interpreta como FQDN de un servidor concreto (mismo
  20. // contrato que el resto de runners).
  21. type NDTRunner struct{}
  22. // userAgentNDT identifica la app en las peticiones a M-Lab (locate + ndt7).
  23. const userAgentNDT = "netpulse-wan-monitor"
  24. func (NDTRunner) Run(ctx context.Context, serverURL string) (Result, error) {
  25. client := ndt7.NewClient(userAgentNDT, userAgentNDT)
  26. if h := ndtServerHost(serverURL); h != "" {
  27. client.Server = h
  28. }
  29. down, rttUs, err := ndtRunPhase(ctx, client.StartDownload)
  30. if err != nil {
  31. return Result{}, fmt.Errorf("ndt7 download: %w", err)
  32. }
  33. up, _, err := ndtRunPhase(ctx, client.StartUpload)
  34. if err != nil {
  35. return Result{}, fmt.Errorf("ndt7 upload: %w", err)
  36. }
  37. res := Result{
  38. TS: time.Now(),
  39. DownMbps: down,
  40. UpMbps: up,
  41. ServerName: client.FQDN,
  42. Origin: ProviderNDT,
  43. }
  44. if rttUs > 0 {
  45. ms := float64(rttUs) / 1000.0
  46. res.PingMs = &ms
  47. }
  48. return res, nil
  49. }
  50. // ndtServerHost normaliza serverURL a host:port usable por el cliente ndt7
  51. // (acepta "host", "host:port" o "https://host/path"; vacío = autodetectar).
  52. func ndtServerHost(serverURL string) string {
  53. s := strings.TrimSpace(serverURL)
  54. if s == "" {
  55. return ""
  56. }
  57. if u, err := url.Parse(s); err == nil && u.Host != "" {
  58. return u.Host
  59. }
  60. return s
  61. }
  62. // ndtRunPhase consume un phase (download o upload) y devuelve la tasa en
  63. // Mbps calculada sobre la ÚLTIMA medición AppInfo (bytes acumulados / tiempo
  64. // transcurrido, microsegundos) más el último RTT (TCP_INFO.MinRTT,
  65. // microsegundos) visto, para no depender del reloj local.
  66. func ndtRunPhase(
  67. ctx context.Context,
  68. start func(context.Context) (<-chan spec.Measurement, error),
  69. ) (mbps float64, minRTTus float64, err error) {
  70. ch, err := start(ctx)
  71. if err != nil {
  72. return 0, 0, err
  73. }
  74. var last *spec.AppInfo
  75. var rtt float64
  76. for m := range ch {
  77. if m.AppInfo != nil {
  78. cp := *m.AppInfo
  79. last = &cp
  80. }
  81. if m.TCPInfo != nil && m.TCPInfo.MinRTT > 0 {
  82. rtt = float64(m.TCPInfo.MinRTT)
  83. }
  84. }
  85. if last == nil || last.ElapsedTime <= 0 {
  86. return 0, 0, fmt.Errorf("no measurements from the server")
  87. }
  88. bps := float64(last.NumBytes) * 8 / (float64(last.ElapsedTime) / 1e6)
  89. return bps / 1e6, rtt, nil
  90. }