mq.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package mq
  2. import (
  3. "context"
  4. "log"
  5. "time"
  6. mqtt "github.com/eclipse/paho.mqtt.golang"
  7. "github.com/gravitl/netmaker/logger"
  8. "github.com/gravitl/netmaker/netclient/ncutils"
  9. "github.com/gravitl/netmaker/servercfg"
  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.SetAutoReconnect(true)
  25. opts.SetConnectRetry(true)
  26. opts.SetConnectRetryInterval(time.Second << 2)
  27. opts.SetKeepAlive(time.Minute)
  28. opts.SetWriteTimeout(time.Minute)
  29. opts.SetOnConnectHandler(func(client mqtt.Client) {
  30. if !publish {
  31. if token := client.Subscribe("ping/#", 2, mqtt.MessageHandler(Ping)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  32. client.Disconnect(240)
  33. logger.Log(0, "ping subscription failed")
  34. }
  35. if token := client.Subscribe("update/#", 0, mqtt.MessageHandler(UpdateNode)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  36. client.Disconnect(240)
  37. logger.Log(0, "node update subscription failed")
  38. }
  39. if token := client.Subscribe("signal/#", 0, mqtt.MessageHandler(ClientPeerUpdate)); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  40. client.Disconnect(240)
  41. logger.Log(0, "node client subscription failed")
  42. }
  43. opts.SetOrderMatters(true)
  44. opts.SetResumeSubs(true)
  45. }
  46. })
  47. client := mqtt.NewClient(opts)
  48. tperiod := time.Now().Add(10 * time.Second)
  49. for {
  50. if token := client.Connect(); token.WaitTimeout(MQ_TIMEOUT*time.Second) && token.Error() != nil {
  51. logger.Log(2, "unable to connect to broker, retrying ...")
  52. if time.Now().After(tperiod) {
  53. if token.Error() == nil {
  54. log.Fatal(0, "could not connect to broker, token timeout, exiting ...")
  55. } else {
  56. log.Fatal(0, "could not connect to broker, exiting ...", token.Error())
  57. }
  58. }
  59. } else {
  60. break
  61. }
  62. time.Sleep(2 * time.Second)
  63. }
  64. return client
  65. }
  66. // Keepalive -- periodically pings all nodes to let them know server is still alive and doing well
  67. func Keepalive(ctx context.Context) {
  68. for {
  69. select {
  70. case <-ctx.Done():
  71. return
  72. case <-time.After(time.Second * KEEPALIVE_TIMEOUT):
  73. sendPeers()
  74. }
  75. }
  76. }