|
| 1 | +package calcium |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + "github.com/projecteru2/core/store" |
| 10 | + "github.com/projecteru2/core/types" |
| 11 | + "github.com/projecteru2/core/utils" |
| 12 | + log "github.com/sirupsen/logrus" |
| 13 | +) |
| 14 | + |
| 15 | +type serviceWatcher struct { |
| 16 | + once sync.Once |
| 17 | + subs sync.Map |
| 18 | +} |
| 19 | + |
| 20 | +func (w *serviceWatcher) Start(s store.Store, pushInterval time.Duration) { |
| 21 | + w.once.Do(func() { |
| 22 | + w.start(s, pushInterval) |
| 23 | + }) |
| 24 | +} |
| 25 | + |
| 26 | +func (w *serviceWatcher) start(s store.Store, pushInterval time.Duration) { |
| 27 | + ch, err := s.ServiceStatusStream(context.Background()) |
| 28 | + if err != nil { |
| 29 | + log.Errorf("[WatchServiceStatus] failed to start watch: %v", err) |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + go func() { |
| 34 | + defer log.Error("[WatchServiceStatus] goroutine exited") |
| 35 | + var ( |
| 36 | + latestStatus types.ServiceStatus |
| 37 | + timer *time.Timer = time.NewTimer(pushInterval / 2) |
| 38 | + ) |
| 39 | + for { |
| 40 | + select { |
| 41 | + case addresses, ok := <-ch: |
| 42 | + if !ok { |
| 43 | + log.Error("[WatchServiceStatus] watch channel closed") |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + latestStatus = types.ServiceStatus{ |
| 48 | + Addresses: addresses, |
| 49 | + Interval: pushInterval, |
| 50 | + } |
| 51 | + w.dispatch(latestStatus) |
| 52 | + |
| 53 | + case <-timer.C: |
| 54 | + w.dispatch(latestStatus) |
| 55 | + } |
| 56 | + timer.Stop() |
| 57 | + timer.Reset(pushInterval / 2) |
| 58 | + } |
| 59 | + }() |
| 60 | +} |
| 61 | + |
| 62 | +func (w *serviceWatcher) dispatch(status types.ServiceStatus) { |
| 63 | + w.subs.Range(func(k, v interface{}) bool { |
| 64 | + c, ok := v.(chan<- types.ServiceStatus) |
| 65 | + if !ok { |
| 66 | + log.Error("[WatchServiceStatus] failed to cast channel from map") |
| 67 | + return true |
| 68 | + } |
| 69 | + c <- status |
| 70 | + return true |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +func (w *serviceWatcher) Subscribe(ch chan<- types.ServiceStatus) uuid.UUID { |
| 75 | + id := uuid.New() |
| 76 | + _, _ = w.subs.LoadOrStore(id, ch) |
| 77 | + return id |
| 78 | +} |
| 79 | + |
| 80 | +func (w *serviceWatcher) Unsubscribe(id uuid.UUID) { |
| 81 | + w.subs.Delete(id) |
| 82 | +} |
| 83 | + |
| 84 | +func (c *Calcium) WatchServiceStatus(ctx context.Context) (<-chan types.ServiceStatus, error) { |
| 85 | + ch := make(chan types.ServiceStatus) |
| 86 | + c.watcher.Start(c.store, c.config.ServiceDiscoveryPushInterval) |
| 87 | + id := c.watcher.Subscribe(ch) |
| 88 | + go func() { |
| 89 | + <-ctx.Done() |
| 90 | + c.watcher.Unsubscribe(id) |
| 91 | + close(ch) |
| 92 | + }() |
| 93 | + return ch, nil |
| 94 | +} |
| 95 | + |
| 96 | +func (c *Calcium) RegisterService(ctx context.Context) (unregister func(), err error) { |
| 97 | + ctx, cancel := context.WithCancel(ctx) |
| 98 | + serviceAddress, err := utils.GetOutboundAddress(c.config.Bind) |
| 99 | + if err != nil { |
| 100 | + log.Errorf("[RegisterService] failed to get outbound address: %v", err) |
| 101 | + return |
| 102 | + } |
| 103 | + if err = c.store.RegisterService(ctx, serviceAddress, c.config.ServiceHeartbeatInterval); err != nil { |
| 104 | + log.Errorf("[RegisterService] failed to register service: %v", err) |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + done := make(chan struct{}) |
| 109 | + go func() { |
| 110 | + defer func() { |
| 111 | + if err := c.store.UnregisterService(context.Background(), serviceAddress); err != nil { |
| 112 | + log.Errorf("[RegisterService] failed to unregister service: %v", err) |
| 113 | + } |
| 114 | + close(done) |
| 115 | + }() |
| 116 | + |
| 117 | + timer := time.NewTicker(c.config.ServiceHeartbeatInterval / 2) |
| 118 | + for { |
| 119 | + select { |
| 120 | + case <-timer.C: |
| 121 | + if err := c.store.RegisterService(ctx, serviceAddress, c.config.ServiceHeartbeatInterval); err != nil { |
| 122 | + log.Errorf("[RegisterService] failed to register service: %v", err) |
| 123 | + } |
| 124 | + case <-ctx.Done(): |
| 125 | + log.Infof("[RegisterService] context done: %v", ctx.Err()) |
| 126 | + return |
| 127 | + } |
| 128 | + } |
| 129 | + }() |
| 130 | + return func() { |
| 131 | + cancel() |
| 132 | + <-done |
| 133 | + }, err |
| 134 | +} |
0 commit comments