mq.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // SetupMQTT creates a connection to broker and return client
  19. func SetupMQTT(publish bool) mqtt.Client {
  20. opts := mqtt.NewClientOptions()
  21. opts.AddBroker(servercfg.GetMessageQueueEndpoint())
  22. id := ncutils.MakeRandomString(23)
  23. opts.ClientID = id
  24. opts.SetTLSConfig(&serverctl.TlsConfig)
  25. opts.SetAutoReconnect(true)
  26. opts.SetConnectRetry(true)
  27. opts.SetConnectRetryInterval(time.Second << 2)
  28. opts.SetKeepAlive(time.Minute)
  29. opts.SetWriteTimeout(time.Minute)
  30. opts.SetOnConnectHandler(func(client mqtt.Client) {
  31. if !publish {
  32. if token := client.Subscribe("ping/#", 2, mqtt.MessageHandler(Ping)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  33. client.Disconnect(240)
  34. logger.Log(0, "ping subscription failed")
  35. }
  36. if token := client.Subscribe("update/#", 0, mqtt.MessageHandler(UpdateNode)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  37. client.Disconnect(240)
  38. logger.Log(0, "node update subscription failed")
  39. }
  40. if token := client.Subscribe("signal/#", 0, mqtt.MessageHandler(ClientPeerUpdate)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  41. client.Disconnect(240)
  42. logger.Log(0, "node client subscription failed")
  43. }
  44. opts.SetOrderMatters(true)
  45. opts.SetResumeSubs(true)
  46. }
  47. })
  48. client := mqtt.NewClient(opts)
  49. tperiod := time.Now().Add(10 * time.Second)
  50. for {
  51. if token := client.Connect(); !token.WaitTimeout(MQ_TIMEOUT*time.Second) || token.Error() != nil {
  52. logger.Log(2, "unable to connect to broker, retrying ...")
  53. if time.Now().After(tperiod) {
  54. if token.Error() == nil {
  55. logger.FatalLog("could not connect to broker, token timeout, exiting ...")
  56. } else {
  57. logger.FatalLog("could not connect to broker, exiting ...", token.Error().Error())
  58. }
  59. }
  60. } else {
  61. break
  62. }
  63. time.Sleep(2 * time.Second)
  64. }
  65. if !publish {
  66. logger.Log(0, "successfully connected to mq broker")
  67. }
  68. return client
  69. }
  70. // Keepalive -- periodically pings all nodes to let them know server is still alive and doing well
  71. func Keepalive(ctx context.Context) {
  72. for {
  73. select {
  74. case <-ctx.Done():
  75. return
  76. case <-time.After(time.Second * KEEPALIVE_TIMEOUT):
  77. sendPeers()
  78. }
  79. }
  80. }