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