sync.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package syncman
  15. import (
  16. "fmt"
  17. "sync/atomic"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/onecloud/pkg/appsrv"
  22. )
  23. type ISyncClient interface {
  24. DoSync(first bool, timeout bool) (time.Duration, error)
  25. NeedSync(dat *jsonutils.JSONDict) bool
  26. Name() string
  27. }
  28. type SSyncManager struct {
  29. ISyncClient
  30. lastSync time.Time
  31. syncTimer *time.Timer
  32. syncOnce int32
  33. syncWorkerManager *appsrv.SWorkerManager
  34. }
  35. func (manager *SSyncManager) InitSync(client ISyncClient) {
  36. manager.ISyncClient = client
  37. manager.syncWorkerManager = appsrv.NewWorkerManagerIgnoreOverflow(fmt.Sprintf("(%s)sync_worker", client.Name()), 1, 1, true, true)
  38. }
  39. func (manager *SSyncManager) syncInternal(isFirst bool, isTimeout bool) error {
  40. if manager.syncTimer != nil {
  41. manager.syncTimer.Stop()
  42. manager.syncTimer = nil
  43. }
  44. next, err := manager.DoSync(isFirst, isTimeout)
  45. if err == nil {
  46. manager.lastSync = time.Now()
  47. }
  48. manager.syncTimer = time.AfterFunc(next, func() {
  49. manager.SyncOnce(false, true)
  50. })
  51. return err
  52. }
  53. type SyncTask struct {
  54. manager *SSyncManager
  55. isFirst bool
  56. isTimeout bool
  57. }
  58. func (t *SyncTask) Run() {
  59. atomic.StoreInt32(&t.manager.syncOnce, 0)
  60. t.manager.syncInternal(t.isFirst, t.isTimeout)
  61. }
  62. func (t *SyncTask) Dump() string {
  63. return "SyncTask"
  64. }
  65. func (manager *SSyncManager) SyncOnce(isFirst bool, isTimeout bool) {
  66. log.Debugf("[%s] SyncOnce isFirst %v isTimeout %v", manager.Name(), isFirst, isTimeout)
  67. if atomic.CompareAndSwapInt32(&manager.syncOnce, 0, 1) {
  68. task := SyncTask{
  69. manager: manager,
  70. isFirst: isFirst,
  71. isTimeout: isTimeout,
  72. }
  73. manager.syncWorkerManager.Run(&task, nil, nil)
  74. }
  75. }
  76. func (manager *SSyncManager) FirstSync() error {
  77. return manager.syncInternal(true, false)
  78. }