2021-04-07 02:06:17 +00:00
|
|
|
package msgstream
|
|
|
|
|
2021-04-08 07:41:28 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
)
|
2021-04-07 02:06:17 +00:00
|
|
|
|
|
|
|
type SimpleMsgStream struct {
|
|
|
|
msgChan chan *MsgPack
|
|
|
|
|
|
|
|
msgCount int
|
|
|
|
msgCountMtx sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *SimpleMsgStream) Start() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *SimpleMsgStream) Close() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *SimpleMsgStream) Chan() <-chan *MsgPack {
|
|
|
|
return ms.msgChan
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *SimpleMsgStream) AsProducer(channels []string) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *SimpleMsgStream) AsConsumer(channels []string, subName string) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *SimpleMsgStream) SetRepackFunc(repackFunc RepackFunc) {
|
|
|
|
}
|
|
|
|
|
2021-04-08 07:41:28 +00:00
|
|
|
func (ms *SimpleMsgStream) getMsgCount() int {
|
|
|
|
ms.msgCountMtx.RLock()
|
|
|
|
defer ms.msgCountMtx.RUnlock()
|
|
|
|
|
|
|
|
return ms.msgCount
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *SimpleMsgStream) increaseMsgCount(delta int) {
|
2021-04-07 02:06:17 +00:00
|
|
|
ms.msgCountMtx.Lock()
|
|
|
|
defer ms.msgCountMtx.Unlock()
|
|
|
|
|
2021-04-08 07:41:28 +00:00
|
|
|
ms.msgCount += delta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *SimpleMsgStream) decreaseMsgCount(delta int) {
|
|
|
|
ms.increaseMsgCount(-delta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *SimpleMsgStream) Produce(pack *MsgPack) error {
|
|
|
|
defer ms.increaseMsgCount(1)
|
|
|
|
|
2021-04-07 02:06:17 +00:00
|
|
|
ms.msgChan <- pack
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *SimpleMsgStream) Broadcast(pack *MsgPack) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *SimpleMsgStream) Consume() *MsgPack {
|
2021-04-08 07:41:28 +00:00
|
|
|
if ms.getMsgCount() <= 0 {
|
2021-04-07 02:06:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-08 07:41:28 +00:00
|
|
|
defer ms.decreaseMsgCount(1)
|
|
|
|
|
2021-04-07 02:06:17 +00:00
|
|
|
return <-ms.msgChan
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *SimpleMsgStream) Seek(offset *MsgPosition) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSimpleMsgStream() *SimpleMsgStream {
|
|
|
|
return &SimpleMsgStream{
|
|
|
|
msgChan: make(chan *MsgPack, 1024),
|
|
|
|
msgCount: 0,
|
|
|
|
}
|
|
|
|
}
|
2021-04-08 07:41:28 +00:00
|
|
|
|
|
|
|
type SimpleMsgStreamFactory struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (factory *SimpleMsgStreamFactory) SetParams(params map[string]interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (factory *SimpleMsgStreamFactory) NewMsgStream(ctx context.Context) (MsgStream, error) {
|
|
|
|
return NewSimpleMsgStream(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (factory *SimpleMsgStreamFactory) NewTtMsgStream(ctx context.Context) (MsgStream, error) {
|
|
|
|
return NewSimpleMsgStream(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (factory *SimpleMsgStreamFactory) NewQueryMsgStream(ctx context.Context) (MsgStream, error) {
|
|
|
|
return NewSimpleMsgStream(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSimpleMsgStreamFactory() *SimpleMsgStreamFactory {
|
|
|
|
return &SimpleMsgStreamFactory{}
|
|
|
|
}
|