mq.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package mq
  2. import (
  3. "context"
  4. "time"
  5. mqtt "github.com/eclipse/paho.mqtt.golang"
  6. "github.com/gravitl/netmaker/logger"
  7. "github.com/gravitl/netmaker/netclient/ncutils"
  8. "github.com/gravitl/netmaker/servercfg"
  9. "github.com/gravitl/netmaker/serverctl"
  10. )
  11. // KEEPALIVE_TIMEOUT - time in seconds for timeout
  12. const KEEPALIVE_TIMEOUT = 60 //timeout in seconds
  13. // MQ_DISCONNECT - disconnects MQ
  14. const MQ_DISCONNECT = 250
  15. // MQ_TIMEOUT - timeout for MQ
  16. const MQ_TIMEOUT = 30
  17. var peer_force_send = 0
  18. var mqclient mqtt.Client
  19. // SetupMQTT creates a connection to broker and return client
  20. func SetupMQTT() {
  21. opts := mqtt.NewClientOptions()
  22. broker, secure := servercfg.GetMessageQueueEndpoint()
  23. opts.AddBroker(broker)
  24. id := ncutils.MakeRandomString(23)
  25. opts.ClientID = id
  26. if secure {
  27. opts.SetTLSConfig(&serverctl.TlsConfig)
  28. }
  29. opts.SetAutoReconnect(true)
  30. opts.SetConnectRetry(true)
  31. opts.SetConnectRetryInterval(time.Second << 2)
  32. opts.SetKeepAlive(time.Minute)
  33. opts.SetWriteTimeout(time.Minute)
  34. opts.SetOnConnectHandler(func(client mqtt.Client) {
  35. if token := client.Subscribe("ping/#", 2, mqtt.MessageHandler(Ping)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  36. client.Disconnect(240)
  37. logger.Log(0, "ping subscription failed")
  38. }
  39. if token := client.Subscribe("update/#", 0, mqtt.MessageHandler(UpdateNode)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  40. client.Disconnect(240)
  41. logger.Log(0, "node update subscription failed")
  42. }
  43. if token := client.Subscribe("signal/#", 0, mqtt.MessageHandler(ClientPeerUpdate)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  44. client.Disconnect(240)
  45. logger.Log(0, "node client subscription failed")
  46. }
  47. opts.SetOrderMatters(true)
  48. opts.SetResumeSubs(true)
  49. })
  50. mqclient = mqtt.NewClient(opts)
  51. tperiod := time.Now().Add(10 * time.Second)
  52. for {
  53. if token := mqclient.Connect(); !token.WaitTimeout(MQ_TIMEOUT*time.Second) || token.Error() != nil {
  54. logger.Log(2, "unable to connect to broker, retrying ...")
  55. if time.Now().After(tperiod) {
  56. if token.Error() == nil {
  57. logger.FatalLog("could not connect to broker, token timeout, exiting ...")
  58. } else {
  59. logger.FatalLog("could not connect to broker, exiting ...", token.Error().Error())
  60. }
  61. }
  62. } else {
  63. break
  64. }
  65. time.Sleep(2 * time.Second)
  66. }
  67. }
  68. // Keepalive -- periodically pings all nodes to let them know server is still alive and doing well
  69. func Keepalive(ctx context.Context) {
  70. for {
  71. select {
  72. case <-ctx.Done():
  73. return
  74. case <-time.After(time.Second * KEEPALIVE_TIMEOUT):
  75. sendPeers()
  76. }
  77. }
  78. }