Skip to content

Commit

Permalink
add lock for album fetch queue. #147
Browse files Browse the repository at this point in the history
  • Loading branch information
AmarnathCJD committed Sep 24, 2024
1 parent 1c59d48 commit 897581c
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions telegram/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func (h *rawHandle) GetGroup() string {
}

type UpdateDispatcher struct {
sync.Mutex
messageHandles map[string][]*messageHandle
inlineHandles map[string][]*inlineHandle
callbackHandles map[string][]*callbackHandle
Expand Down Expand Up @@ -420,24 +421,24 @@ func (c *Client) handleAlbum(message MessageObj) {
if group, ok := c.dispatcher.activeAlbums[message.GroupedID]; ok {
group.Add(packMessage(c, &message))
} else {
abox := &albumBox{
albBox := &albumBox{
waitExit: make(chan struct{}),
messages: []*NewMessage{packMessage(c, &message)},
groupedId: message.GroupedID,
}
if c.dispatcher.activeAlbums == nil {
c.dispatcher.activeAlbums = make(map[int64]*albumBox)
}
c.dispatcher.activeAlbums[message.GroupedID] = abox
c.dispatcher.activeAlbums[message.GroupedID] = albBox
go func() {
<-abox.waitExit
<-albBox.waitExit

for gp, handlers := range c.dispatcher.albumHandles {
for _, handler := range handlers {
handle := func(h *albumHandle) error {
if err := h.Handler(&Album{
GroupedID: abox.groupedId,
Messages: abox.messages,
GroupedID: albBox.groupedId,
Messages: albBox.messages,
Client: c,
}); err != nil {
if errors.Is(err, EndGroup) {
Expand All @@ -458,9 +459,11 @@ func (c *Client) handleAlbum(message MessageObj) {
}
}

c.dispatcher.Lock()
delete(c.dispatcher.activeAlbums, message.GroupedID)
c.dispatcher.Unlock()
}()
go abox.Wait()
go albBox.Wait()
}
}

Expand Down

0 comments on commit 897581c

Please sign in to comment.