mq.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. broker, secure := servercfg.GetMessageQueueEndpoint()
  22. opts.AddBroker(broker)
  23. id := ncutils.MakeRandomString(23)
  24. opts.ClientID = id
  25. if secure {
  26. opts.SetTLSConfig(&serverctl.TlsConfig)
  27. }
  28. opts.SetAutoReconnect(true)
  29. opts.SetConnectRetry(true)
  30. opts.SetConnectRetryInterval(time.Second << 2)
  31. opts.SetKeepAlive(time.Minute)
  32. opts.SetWriteTimeout(time.Minute)
  33. opts.SetOnConnectHandler(func(client mqtt.Client) {
  34. if !publish {
  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. })
  51. client := mqtt.NewClient(opts)
  52. tperiod := time.Now().Add(10 * time.Second)
  53. for {
  54. if token := client.Connect(); !token.WaitTimeout(MQ_TIMEOUT*time.Second) || token.Error() != nil {
  55. logger.Log(2, "unable to connect to broker, retrying ...")
  56. if time.Now().After(tperiod) {
  57. if token.Error() == nil {
  58. logger.FatalLog("could not connect to broker, token timeout, exiting ...")
  59. } else {
  60. logger.FatalLog("could not connect to broker, exiting ...", token.Error().Error())
  61. }
  62. }
  63. } else {
  64. break
  65. }
  66. time.Sleep(2 * time.Second)
  67. }
  68. if !publish {
  69. logger.Log(0, "successfully connected to mq broker")
  70. }
  71. return client
  72. }
  73. // Keepalive -- periodically pings all nodes to let them know server is still alive and doing well
  74. func Keepalive(ctx context.Context) {
  75. for {
  76. select {
  77. case <-ctx.Done():
  78. return
  79. case <-time.After(time.Second * KEEPALIVE_TIMEOUT):
  80. sendPeers()
  81. }
  82. }
  83. }