add per-room mutex, possibly fixes #8

This commit is contained in:
Aine
2022-08-27 22:10:22 +03:00
parent 9484758f33
commit d1c48b9b31
4 changed files with 38 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ type Bot struct {
rooms sync.Map rooms sync.Map
log *logger.Logger log *logger.Logger
lp *linkpearl.Linkpearl lp *linkpearl.Linkpearl
mu map[id.RoomID]*sync.Mutex
handledMembershipEvents sync.Map handledMembershipEvents sync.Map
} }
@@ -35,6 +36,7 @@ func New(lp *linkpearl.Linkpearl, log *logger.Logger, prefix, domain string, noo
rooms: sync.Map{}, rooms: sync.Map{},
log: log, log: log,
lp: lp, lp: lp,
mu: map[id.RoomID]*sync.Mutex{},
} }
} }

View File

@@ -49,6 +49,8 @@ func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
if !ok { if !ok {
return errors.New("room not found") return errors.New("room not found")
} }
b.lock(roomID)
defer b.unlock(roomID)
cfg, err := b.getSettings(roomID) cfg, err := b.getSettings(roomID)
if err != nil { if err != nil {

26
bot/mutext.go Normal file
View File

@@ -0,0 +1,26 @@
package bot
import (
"sync"
"maunium.net/go/mautrix/id"
)
func (b *Bot) lock(roomID id.RoomID) {
_, ok := b.mu[roomID]
if !ok {
b.mu[roomID] = &sync.Mutex{}
}
b.mu[roomID].Lock()
}
func (b *Bot) unlock(roomID id.RoomID) {
_, ok := b.mu[roomID]
if !ok {
return
}
b.mu[roomID].Unlock()
delete(b.mu, roomID)
}

8
e2e/send-stress Executable file
View File

@@ -0,0 +1,8 @@
#!/bin/bash
for i in {0..10..1}; do
echo "#${i}..."
ssmtp test@localhost < $1
done
echo "done"