publisher.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package discov
  2. import (
  3. "github.com/tal-tech/go-zero/core/discov/internal"
  4. "github.com/tal-tech/go-zero/core/lang"
  5. "github.com/tal-tech/go-zero/core/logx"
  6. "github.com/tal-tech/go-zero/core/proc"
  7. "github.com/tal-tech/go-zero/core/syncx"
  8. "github.com/tal-tech/go-zero/core/threading"
  9. "go.etcd.io/etcd/clientv3"
  10. )
  11. type (
  12. PublisherOption func(client *Publisher)
  13. Publisher struct {
  14. endpoints []string
  15. key string
  16. fullKey string
  17. id int64
  18. value string
  19. lease clientv3.LeaseID
  20. quit *syncx.DoneChan
  21. pauseChan chan lang.PlaceholderType
  22. resumeChan chan lang.PlaceholderType
  23. }
  24. )
  25. func NewPublisher(endpoints []string, key, value string, opts ...PublisherOption) *Publisher {
  26. publisher := &Publisher{
  27. endpoints: endpoints,
  28. key: key,
  29. value: value,
  30. quit: syncx.NewDoneChan(),
  31. pauseChan: make(chan lang.PlaceholderType),
  32. resumeChan: make(chan lang.PlaceholderType),
  33. }
  34. for _, opt := range opts {
  35. opt(publisher)
  36. }
  37. return publisher
  38. }
  39. func (p *Publisher) KeepAlive() error {
  40. cli, err := internal.GetRegistry().GetConn(p.endpoints)
  41. if err != nil {
  42. return err
  43. }
  44. p.lease, err = p.register(cli)
  45. if err != nil {
  46. return err
  47. }
  48. proc.AddWrapUpListener(func() {
  49. p.Stop()
  50. })
  51. return p.keepAliveAsync(cli)
  52. }
  53. func (p *Publisher) Pause() {
  54. p.pauseChan <- lang.Placeholder
  55. }
  56. func (p *Publisher) Resume() {
  57. p.resumeChan <- lang.Placeholder
  58. }
  59. func (p *Publisher) Stop() {
  60. p.quit.Close()
  61. }
  62. func (p *Publisher) keepAliveAsync(cli internal.EtcdClient) error {
  63. ch, err := cli.KeepAlive(cli.Ctx(), p.lease)
  64. if err != nil {
  65. return err
  66. }
  67. threading.GoSafe(func() {
  68. for {
  69. select {
  70. case _, ok := <-ch:
  71. if !ok {
  72. p.revoke(cli)
  73. if err := p.KeepAlive(); err != nil {
  74. logx.Errorf("KeepAlive: %s", err.Error())
  75. }
  76. return
  77. }
  78. case <-p.pauseChan:
  79. logx.Infof("paused etcd renew, key: %s, value: %s", p.key, p.value)
  80. p.revoke(cli)
  81. select {
  82. case <-p.resumeChan:
  83. if err := p.KeepAlive(); err != nil {
  84. logx.Errorf("KeepAlive: %s", err.Error())
  85. }
  86. return
  87. case <-p.quit.Done():
  88. return
  89. }
  90. case <-p.quit.Done():
  91. p.revoke(cli)
  92. return
  93. }
  94. }
  95. })
  96. return nil
  97. }
  98. func (p *Publisher) register(client internal.EtcdClient) (clientv3.LeaseID, error) {
  99. resp, err := client.Grant(client.Ctx(), TimeToLive)
  100. if err != nil {
  101. return clientv3.NoLease, err
  102. }
  103. lease := resp.ID
  104. if p.id > 0 {
  105. p.fullKey = makeEtcdKey(p.key, p.id)
  106. } else {
  107. p.fullKey = makeEtcdKey(p.key, int64(lease))
  108. }
  109. _, err = client.Put(client.Ctx(), p.fullKey, p.value, clientv3.WithLease(lease))
  110. return lease, err
  111. }
  112. func (p *Publisher) revoke(cli internal.EtcdClient) {
  113. if _, err := cli.Revoke(cli.Ctx(), p.lease); err != nil {
  114. logx.Error(err)
  115. }
  116. }
  117. func WithId(id int64) PublisherOption {
  118. return func(publisher *Publisher) {
  119. publisher.id = id
  120. }
  121. }