mq.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. )
  10. // KEEPALIVE_TIMEOUT - time in seconds for timeout
  11. const KEEPALIVE_TIMEOUT = 60 //timeout in seconds
  12. // MQ_DISCONNECT - disconnects MQ
  13. const MQ_DISCONNECT = 250
  14. // MQ_TIMEOUT - timeout for MQ
  15. const MQ_TIMEOUT = 30
  16. var peer_force_send = 0
  17. var mqclient mqtt.Client
  18. func setMqOptions(user, password string, opts *mqtt.ClientOptions) {
  19. broker, _ := servercfg.GetMessageQueueEndpoint()
  20. opts.AddBroker(broker)
  21. id := ncutils.MakeRandomString(23)
  22. opts.ClientID = id
  23. opts.SetUsername(user)
  24. opts.SetPassword(password)
  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. }
  31. // SetupMQTT creates a connection to broker and return client
  32. func SetupMQTT() {
  33. opts := mqtt.NewClientOptions()
  34. setMqOptions(servercfg.GetMqUserName(), servercfg.GetMqPassword(), opts)
  35. opts.SetOnConnectHandler(func(client mqtt.Client) {
  36. if token := client.Subscribe("ping/#", 2, mqtt.MessageHandler(Ping)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  37. client.Disconnect(240)
  38. logger.Log(0, "ping subscription failed")
  39. }
  40. if token := client.Subscribe("update/#", 0, mqtt.MessageHandler(UpdateNode)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  41. client.Disconnect(240)
  42. logger.Log(0, "node update subscription failed")
  43. }
  44. if token := client.Subscribe("host/serverupdate/#", 0, mqtt.MessageHandler(UpdateHost)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  45. client.Disconnect(240)
  46. logger.Log(0, "host update subscription failed")
  47. }
  48. if token := client.Subscribe("signal/#", 0, mqtt.MessageHandler(ClientPeerUpdate)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  49. client.Disconnect(240)
  50. logger.Log(0, "node client subscription failed")
  51. }
  52. if token := client.Subscribe("metrics/#", 0, mqtt.MessageHandler(UpdateMetrics)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  53. client.Disconnect(240)
  54. logger.Log(0, "node metrics subscription failed")
  55. }
  56. opts.SetOrderMatters(true)
  57. opts.SetResumeSubs(true)
  58. })
  59. mqclient = mqtt.NewClient(opts)
  60. tperiod := time.Now().Add(10 * time.Second)
  61. for {
  62. if token := mqclient.Connect(); !token.WaitTimeout(MQ_TIMEOUT*time.Second) || token.Error() != nil {
  63. logger.Log(2, "unable to connect to broker, retrying ...")
  64. if time.Now().After(tperiod) {
  65. if token.Error() == nil {
  66. logger.FatalLog("could not connect to broker, token timeout, exiting ...")
  67. } else {
  68. logger.FatalLog("could not connect to broker, exiting ...", token.Error().Error())
  69. }
  70. }
  71. } else {
  72. break
  73. }
  74. time.Sleep(2 * time.Second)
  75. }
  76. }
  77. // Keepalive -- periodically pings all nodes to let them know server is still alive and doing well
  78. func Keepalive(ctx context.Context) {
  79. for {
  80. select {
  81. case <-ctx.Done():
  82. return
  83. case <-time.After(time.Second * KEEPALIVE_TIMEOUT):
  84. sendPeers()
  85. }
  86. }
  87. }
  88. // IsConnected - function for determining if the mqclient is connected or not
  89. func IsConnected() bool {
  90. return mqclient != nil && mqclient.IsConnected()
  91. }
  92. // CloseClient - function to close the mq connection from server
  93. func CloseClient() {
  94. mqclient.Disconnect(250)
  95. }