go-pattern-examples/gomore/27_messages/message.go

161 lines
3.6 KiB
Go
Raw Normal View History

2020-04-28 07:31:29 +03:00
package gomore
import (
2020-04-29 12:27:46 +03:00
"context"
2020-04-28 07:31:29 +03:00
"errors"
"time"
)
2020-04-28 12:24:15 +03:00
//Message for msg in Message bus
2020-04-28 07:31:29 +03:00
type Message struct {
2020-04-29 09:09:22 +03:00
//Type 类型[code :1,2,3,4]
Type int
Text string
From Session //消息来源
2020-04-28 07:31:29 +03:00
}
//User for user
type User struct {
ID uint64
Name string
}
2020-04-29 12:27:46 +03:00
//Session inherit user
2020-04-28 07:31:29 +03:00
type Session struct {
2020-04-29 12:27:46 +03:00
User
2020-04-28 07:31:29 +03:00
Timestamp time.Time
}
//Subscription for user
2020-04-29 09:09:22 +03:00
//Subscription is a session
2020-04-28 07:31:29 +03:00
type Subscription struct {
2020-04-29 09:09:22 +03:00
Session
2020-04-29 12:27:46 +03:00
topicName string
ctx context.Context
cancel context.CancelFunc
ch chan<- Message //发送队列
Inbox <-chan Message //接收消息的队列
2020-04-28 07:31:29 +03:00
}
2020-04-29 12:27:46 +03:00
func newSubscription(uid uint64, topicName string, UserQueueSize int) Subscription {
ctx, cancel := context.WithCancel(context.Background())
return Subscription{
Session: Session{User{ID: uid}, time.Now()},
ctx: ctx,
cancel: cancel,
ch: make(chan<- Message, UserQueueSize), //用于跟单个用户通信的消息队列,用户发送消息
Inbox: make(<-chan Message, UserQueueSize), //用于跟单个用户通信的消息队列,用户接收消息
}
}
//Cancel Message
func (s *Subscription) Cancel() {
s.cancel()
}
//Publish 只有当channel无数据且channel被close了才会返回ok=false
2020-04-28 07:31:29 +03:00
//Publish a message to subscription queue
func (s *Subscription) Publish(msg Message) error {
2020-04-29 12:27:46 +03:00
select {
case <-s.ctx.Done():
2020-04-28 07:31:29 +03:00
return errors.New("Topic has been closed")
2020-04-29 12:27:46 +03:00
default:
s.ch <- msg
2020-04-28 07:31:29 +03:00
}
2020-04-29 12:27:46 +03:00
return nil
}
2020-04-28 07:31:29 +03:00
2020-04-29 12:27:46 +03:00
//Receive message
func (s *Subscription) Receive(out *Message) error {
select {
case <-s.ctx.Done():
return errors.New("Topic has been closed")
default:
2020-04-29 12:33:31 +03:00
*out = <-s.Inbox
2020-04-29 12:27:46 +03:00
}
2020-04-28 07:31:29 +03:00
return nil
}
//Topic that user is interested in
2020-04-29 09:09:22 +03:00
//Topic locate in MQ
2020-04-28 07:31:29 +03:00
type Topic struct {
2020-04-29 12:27:46 +03:00
UserQueueSize int
2020-04-28 12:24:15 +03:00
Name string
2020-04-29 12:27:46 +03:00
Subscribers map[uint64]Subscription //user list
MessageHistory []Message //当前主题的消息历史,实际项目中可能需要限定大小并设置过期时间
2020-04-28 12:24:15 +03:00
}
2020-04-29 09:09:22 +03:00
//Queue hold all topics
type Queue struct {
2020-04-29 12:27:46 +03:00
Topics map[string]Topic //topic ID<-----> topic Object
}
//AddTopic to Queue
func (q *Queue) AddTopic(topicName string, topicUserQueueSize int) Topic {
if q.Topics == nil {
q.Topics = make(map[string]Topic)
}
if _, found := q.Topics[topicName]; !found {
q.Topics[topicName] = Topic{UserQueueSize: topicUserQueueSize, Name: topicName}
}
return q.Topics[topicName]
2020-04-28 17:48:25 +03:00
}
2020-04-28 12:24:15 +03:00
//String remove Subscription
func (t *Topic) String() string {
return t.Name
2020-04-28 07:31:29 +03:00
}
2020-04-29 12:27:46 +03:00
func (t *Topic) findUserSubscription(uid uint64, topicName string) (Subscription, bool) {
2020-04-29 09:09:22 +03:00
// Get session or create one if it's the first
2020-04-29 12:27:46 +03:00
if topicName != t.Name || t.Subscribers == nil || len(t.Subscribers) == 0 {
return Subscription{}, false
2020-04-29 09:09:22 +03:00
}
2020-04-29 12:27:46 +03:00
if subscription, found := t.Subscribers[uid]; found {
return subscription, true
2020-04-29 09:09:22 +03:00
}
2020-04-29 12:27:46 +03:00
return Subscription{}, false
2020-04-29 09:09:22 +03:00
}
2020-04-29 12:27:46 +03:00
//Subscribe a spec topic
func (t *Topic) Subscribe(uid uint64, topicName string) (Subscription, bool) {
2020-04-29 09:09:22 +03:00
2020-04-29 12:27:46 +03:00
if t.Name != topicName {
return Subscription{}, false
}
var subscription Subscription
2020-04-28 12:24:15 +03:00
// Get session or create one if it's the first
2020-04-29 12:27:46 +03:00
if _, found := t.findUserSubscription(uid, topicName); !found {
if t.Subscribers == nil {
t.Subscribers = make(map[uint64]Subscription)
2020-04-29 09:09:22 +03:00
}
2020-04-29 12:27:46 +03:00
subscription = newSubscription(uid, topicName, t.UserQueueSize)
t.Subscribers[uid] = subscription
2020-04-29 09:09:22 +03:00
}
2020-04-28 07:31:29 +03:00
2020-04-29 12:27:46 +03:00
return subscription, true
2020-04-28 07:31:29 +03:00
}
//Unsubscribe remove Subscription
2020-04-29 09:09:22 +03:00
func (t *Topic) Unsubscribe(s Subscription) error {
2020-04-29 12:27:46 +03:00
if _, found := t.findUserSubscription(s.ID, s.topicName); found {
delete(t.Subscribers, s.ID)
2020-04-29 09:09:22 +03:00
}
2020-04-28 07:31:29 +03:00
return nil
}
2020-04-28 12:24:15 +03:00
//Delete topic
2020-04-28 07:31:29 +03:00
func (t *Topic) Delete() error {
2020-04-29 09:09:22 +03:00
t.Subscribers = nil
t.Name = ""
t.MessageHistory = nil
2020-04-28 07:31:29 +03:00
return nil
}