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. var peer_force_send = 0
  16. // SetupMQTT creates a connection to broker and return client
  17. func SetupMQTT(publish bool) mqtt.Client {
  18. opts := mqtt.NewClientOptions()
  19. opts.AddBroker("tcp://127.0.0.1:1883")
  20. id := ncutils.MakeRandomString(23)
  21. opts.ClientID = id
  22. opts.SetAutoReconnect(true)
  23. opts.SetConnectRetry(true)
  24. opts.SetConnectRetryInterval(time.Second << 2)
  25. opts.SetKeepAlive(time.Minute)
  26. opts.SetWriteTimeout(time.Minute)
  27. opts.SetOnConnectHandler(func(client mqtt.Client) {
  28. if !publish {
  29. if servercfg.GetDebug() {
  30. if token := client.Subscribe("#", 2, mqtt.MessageHandler(DefaultHandler)); token.Wait() && token.Error() != nil {
  31. client.Disconnect(240)
  32. logger.Log(0, "default subscription failed")
  33. }
  34. }
  35. if token := client.Subscribe("ping/#", 2, mqtt.MessageHandler(Ping)); token.Wait() && 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.Wait() && 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.Wait() && token.Error() != nil {
  44. client.Disconnect(240)
  45. logger.Log(0, "node client subscription failed")
  46. }
  47. logger.Log(3, "message queue subscriptions succeeded")
  48. }
  49. })
  50. opts.SetOrderMatters(true)
  51. opts.SetResumeSubs(true)
  52. client := mqtt.NewClient(opts)
  53. tperiod := time.Now().Add(10 * time.Second)
  54. for {
  55. if token := client.Connect(); token.Wait() && token.Error() != nil {
  56. logger.Log(2, "unable to connect to broker, retrying ...")
  57. if time.Now().After(tperiod) {
  58. log.Fatal(0, "could not connect to broker, exiting ...", token.Error())
  59. }
  60. } else {
  61. break
  62. }
  63. time.Sleep(2 * time.Second)
  64. }
  65. logger.Log(2, "connected to broker")
  66. return client
  67. }
  68. // Keepalive -- periodically pings all nodes to let them know server is still alive and doing well
  69. func Keepalive(ctx context.Context) {
  70. for {
  71. select {
  72. case <-ctx.Done():
  73. return
  74. case <-time.After(time.Second * KEEPALIVE_TIMEOUT):
  75. sendPeers()
  76. }
  77. }
  78. }