probe.go 805 B

12345678910111213141516171819202122232425262728293031323334
  1. // probe.go: comprobación puntual del broker (#838) para el botón "probar
  2. // conexión" de Ajustes.
  3. package mqttpub
  4. import (
  5. "context"
  6. "time"
  7. "github.com/gonzalop/mq"
  8. )
  9. // Probe hace un CONNECT contra el broker y desconecta.
  10. func Probe(ctx context.Context, cfg Config) error {
  11. addr := brokerURL(cfg)
  12. opts := []mq.Option{
  13. mq.WithProtocolVersion(mq.ProtocolV311),
  14. mq.WithClientID("netpulse-probe"),
  15. mq.WithConnectTimeout(5 * time.Second),
  16. }
  17. if opt, ok := tlsOption(cfg); ok {
  18. opts = append(opts, opt)
  19. }
  20. if cfg.User != "" {
  21. opts = append(opts, mq.WithCredentials(cfg.User, cfg.Pass))
  22. }
  23. pctx, cancel := context.WithTimeout(ctx, 6*time.Second)
  24. defer cancel()
  25. client, err := mq.DialContext(pctx, addr, opts...)
  26. if err != nil {
  27. return err
  28. }
  29. client.Disconnect(pctx)
  30. return nil
  31. }