2021-01-15 17:09:41 +08:00
|
|
|
package timesync
|
|
|
|
|
|
|
|
import (
|
2021-01-22 11:07:07 +08:00
|
|
|
"context"
|
2021-01-19 12:10:49 +08:00
|
|
|
|
2021-03-11 14:15:18 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/log"
|
2021-01-15 17:09:41 +08:00
|
|
|
ms "github.com/zilliztech/milvus-distributed/internal/msgstream"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TimeTickWatcher interface {
|
|
|
|
Watch(msg *ms.TimeTickMsg)
|
2021-01-22 11:07:07 +08:00
|
|
|
StartBackgroundLoop(ctx context.Context)
|
2021-01-15 17:09:41 +08:00
|
|
|
}
|
2021-01-19 12:10:49 +08:00
|
|
|
|
|
|
|
type MsgTimeTickWatcher struct {
|
2021-01-22 11:07:07 +08:00
|
|
|
streams []ms.MsgStream
|
|
|
|
msgQueue chan *ms.TimeTickMsg
|
2021-01-19 12:10:49 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 09:43:41 +08:00
|
|
|
func NewMsgTimeTickWatcher(streams ...ms.MsgStream) *MsgTimeTickWatcher {
|
|
|
|
watcher := &MsgTimeTickWatcher{
|
|
|
|
streams: streams,
|
|
|
|
msgQueue: make(chan *ms.TimeTickMsg),
|
|
|
|
}
|
|
|
|
return watcher
|
|
|
|
}
|
|
|
|
|
2021-01-19 12:10:49 +08:00
|
|
|
func (watcher *MsgTimeTickWatcher) Watch(msg *ms.TimeTickMsg) {
|
2021-01-22 11:07:07 +08:00
|
|
|
watcher.msgQueue <- msg
|
2021-01-19 12:10:49 +08:00
|
|
|
}
|
|
|
|
|
2021-01-22 11:07:07 +08:00
|
|
|
func (watcher *MsgTimeTickWatcher) StartBackgroundLoop(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-03-11 14:15:18 +08:00
|
|
|
log.Debug("msg time tick watcher closed")
|
2021-01-22 11:07:07 +08:00
|
|
|
return
|
|
|
|
case msg := <-watcher.msgQueue:
|
|
|
|
msgPack := &ms.MsgPack{}
|
|
|
|
msgPack.Msgs = append(msgPack.Msgs, msg)
|
|
|
|
for _, stream := range watcher.streams {
|
2021-02-24 09:48:17 +08:00
|
|
|
if err := stream.Broadcast(ctx, msgPack); err != nil {
|
2021-03-11 14:15:18 +08:00
|
|
|
log.Warn("stream broadcast failed", zap.Error(err))
|
2021-01-22 11:07:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-19 12:10:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (watcher *MsgTimeTickWatcher) Close() {
|
|
|
|
}
|