2
0

mq.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package mq
  2. import (
  3. "context"
  4. "time"
  5. mqtt "github.com/eclipse/paho.mqtt.golang"
  6. "github.com/netmakerio/netmaker/logger"
  7. "github.com/netmakerio/netmaker/netclient/ncutils"
  8. "github.com/netmakerio/netmaker/servercfg"
  9. "github.com/netmakerio/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. if token := client.Subscribe("metrics/#", 0, mqtt.MessageHandler(UpdateMetrics)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  48. client.Disconnect(240)
  49. logger.Log(0, "node metrics subscription failed")
  50. }
  51. opts.SetOrderMatters(true)
  52. opts.SetResumeSubs(true)
  53. })
  54. mqclient = mqtt.NewClient(opts)
  55. tperiod := time.Now().Add(10 * time.Second)
  56. for {
  57. if token := mqclient.Connect(); !token.WaitTimeout(MQ_TIMEOUT*time.Second) || token.Error() != nil {
  58. logger.Log(2, "unable to connect to broker, retrying ...")
  59. if time.Now().After(tperiod) {
  60. if token.Error() == nil {
  61. logger.FatalLog("could not connect to broker, token timeout, exiting ...")
  62. } else {
  63. logger.FatalLog("could not connect to broker, exiting ...", token.Error().Error())
  64. }
  65. }
  66. } else {
  67. break
  68. }
  69. time.Sleep(2 * time.Second)
  70. }
  71. }
  72. // Keepalive -- periodically pings all nodes to let them know server is still alive and doing well
  73. func Keepalive(ctx context.Context) {
  74. for {
  75. select {
  76. case <-ctx.Done():
  77. return
  78. case <-time.After(time.Second * KEEPALIVE_TIMEOUT):
  79. sendPeers()
  80. }
  81. }
  82. }