2022-11-17 18:59:09 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2023-02-26 11:31:49 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2022-11-17 18:59:09 +08:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2024-01-23 19:09:04 +08:00
|
|
|
"go.uber.org/atomic"
|
2022-11-17 18:59:09 +08:00
|
|
|
"go.uber.org/zap"
|
2023-04-06 19:14:32 +08:00
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
2022-11-17 18:59:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type refresher struct {
|
|
|
|
refreshInterval time.Duration
|
2023-05-24 18:49:25 +08:00
|
|
|
intervalDone chan struct{}
|
2022-11-17 18:59:09 +08:00
|
|
|
intervalInitOnce sync.Once
|
2024-01-23 19:09:04 +08:00
|
|
|
eh atomic.Pointer[EventHandler]
|
2022-11-17 18:59:09 +08:00
|
|
|
|
|
|
|
fetchFunc func() error
|
2023-05-24 18:49:25 +08:00
|
|
|
stopOnce sync.Once
|
|
|
|
wg sync.WaitGroup
|
2022-11-17 18:59:09 +08:00
|
|
|
}
|
|
|
|
|
2023-03-23 19:27:58 +08:00
|
|
|
func newRefresher(interval time.Duration, fetchFunc func() error) *refresher {
|
|
|
|
return &refresher{
|
2022-11-17 18:59:09 +08:00
|
|
|
refreshInterval: interval,
|
2023-05-24 18:49:25 +08:00
|
|
|
intervalDone: make(chan struct{}),
|
2022-11-17 18:59:09 +08:00
|
|
|
fetchFunc: fetchFunc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 19:27:58 +08:00
|
|
|
func (r *refresher) start(name string) {
|
2022-11-17 18:59:09 +08:00
|
|
|
if r.refreshInterval > 0 {
|
|
|
|
r.intervalInitOnce.Do(func() {
|
2023-05-24 18:49:25 +08:00
|
|
|
r.wg.Add(1)
|
2022-12-07 18:01:19 +08:00
|
|
|
go r.refreshPeriodically(name)
|
2022-11-17 18:59:09 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 19:27:58 +08:00
|
|
|
func (r *refresher) stop() {
|
2023-05-24 18:49:25 +08:00
|
|
|
r.stopOnce.Do(func() {
|
|
|
|
close(r.intervalDone)
|
|
|
|
r.wg.Wait()
|
|
|
|
})
|
2022-11-17 18:59:09 +08:00
|
|
|
}
|
|
|
|
|
2023-03-23 19:27:58 +08:00
|
|
|
func (r *refresher) refreshPeriodically(name string) {
|
2023-05-24 18:49:25 +08:00
|
|
|
defer r.wg.Done()
|
2022-11-17 18:59:09 +08:00
|
|
|
ticker := time.NewTicker(r.refreshInterval)
|
2023-02-23 18:59:45 +08:00
|
|
|
defer ticker.Stop()
|
2023-11-08 23:34:22 +08:00
|
|
|
log.Debug("start refreshing configurations", zap.String("source", name))
|
2022-11-17 18:59:09 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
err := r.fetchFunc()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("can not pull configs", zap.Error(err))
|
2023-05-24 18:49:25 +08:00
|
|
|
r.stop()
|
2022-11-17 18:59:09 +08:00
|
|
|
}
|
|
|
|
case <-r.intervalDone:
|
2023-09-05 10:31:48 +08:00
|
|
|
log.Info("stop refreshing configurations", zap.String("source", name))
|
2022-11-17 18:59:09 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 19:07:00 +08:00
|
|
|
func (r *refresher) fireEvents(events ...*Event) {
|
2023-08-24 09:18:24 +08:00
|
|
|
// Generate OnEvent Callback based on the events created
|
2024-01-23 19:09:04 +08:00
|
|
|
ptr := r.eh.Load()
|
|
|
|
if ptr != nil && *ptr != nil {
|
2022-11-17 18:59:09 +08:00
|
|
|
for _, e := range events {
|
2024-01-23 19:09:04 +08:00
|
|
|
(*ptr).OnEvent(e)
|
2022-11-17 18:59:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-23 19:09:04 +08:00
|
|
|
|
|
|
|
func (r *refresher) SetEventHandler(eh EventHandler) {
|
|
|
|
r.eh.Store(&eh)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *refresher) GetEventHandler() EventHandler {
|
|
|
|
var eh EventHandler
|
|
|
|
ptr := r.eh.Load()
|
|
|
|
if ptr != nil {
|
|
|
|
eh = *ptr
|
|
|
|
}
|
|
|
|
return eh
|
|
|
|
}
|