Asynchronous message processing with guaranteed sequential per-chat delivery
This commit is contained in:
parent
307d5136d4
commit
7ea5e9ac73
|
@ -55,6 +55,7 @@ type Client struct {
|
|||
|
||||
type clientLocks struct {
|
||||
authorizationReady sync.WaitGroup
|
||||
chatMessageLocks map[int64]*sync.Mutex
|
||||
}
|
||||
|
||||
// NewClient instantiates a Telegram App
|
||||
|
@ -107,6 +108,8 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component
|
|||
content: &conf.Content,
|
||||
cache: cache.NewCache(),
|
||||
options: options,
|
||||
locks: clientLocks{},
|
||||
locks: clientLocks{
|
||||
chatMessageLocks: make(map[int64]*sync.Mutex),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
|
|
@ -35,6 +35,16 @@ func int64SliceToStringSlice(ints []int64) []string {
|
|||
return strings
|
||||
}
|
||||
|
||||
func (c *Client) getChatMessageLock(chatID int64) *sync.Mutex {
|
||||
lock, ok := c.locks.chatMessageLocks[chatID]
|
||||
if !ok {
|
||||
lock = &sync.Mutex{}
|
||||
c.locks.chatMessageLocks[chatID] = lock
|
||||
}
|
||||
|
||||
return lock
|
||||
}
|
||||
|
||||
func (c *Client) updateHandler() {
|
||||
listener := c.client.GetListener()
|
||||
defer listener.Close()
|
||||
|
@ -121,6 +131,7 @@ func (c *Client) updateUserStatus(update *client.UpdateUserStatus) {
|
|||
|
||||
// new chat discovered
|
||||
func (c *Client) updateNewChat(update *client.UpdateNewChat) {
|
||||
go func() {
|
||||
if update.Chat != nil && update.Chat.Photo != nil && update.Chat.Photo.Small != nil {
|
||||
_, err := c.client.DownloadFile(&client.DownloadFileRequest{
|
||||
FileId: update.Chat.Photo.Small.Id,
|
||||
|
@ -155,12 +166,19 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) {
|
|||
}
|
||||
|
||||
if update.Chat.Id < 0 {
|
||||
go c.processStatusUpdate(update.Chat.Id, update.Chat.Title, "chat")
|
||||
c.processStatusUpdate(update.Chat.Id, update.Chat.Title, "chat")
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// message received
|
||||
func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
|
||||
go func() {
|
||||
// guarantee sequential message delivering per chat
|
||||
lock := c.getChatMessageLock(update.Message.ChatId)
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
// ignore self outgoing messages
|
||||
if update.Message.IsOutgoing &&
|
||||
update.Message.SendingState != nil &&
|
||||
|
@ -209,6 +227,7 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
|
|||
})
|
||||
// forward message to XMPP
|
||||
gateway.SendMessage(c.jid, strconv.FormatInt(update.Message.ChatId, 10), text, c.xmpp)
|
||||
}()
|
||||
}
|
||||
|
||||
// message content updated
|
||||
|
|
Loading…
Reference in a new issue