mirror of
https://github.com/binwiederhier/ntfy.git
synced 2024-11-22 11:24:04 +01:00
Use prepared statement for bulk writes
When executing the same statement multiple times, avoid the overhead of re-parsing the statement for each insert.
This commit is contained in:
parent
321ed12663
commit
1235ea5bb5
1 changed files with 12 additions and 2 deletions
|
@ -258,12 +258,23 @@ func (c *messageCache) addMessages(ms []*message) error {
|
||||||
if c.nop {
|
if c.nop {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if len(ms) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
tx, err := c.db.Begin()
|
tx, err := c.db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
statement, err := tx.Prepare(
|
||||||
|
insertMessageQuery,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer statement.Close()
|
||||||
|
|
||||||
for _, m := range ms {
|
for _, m := range ms {
|
||||||
if m.Event != messageEvent {
|
if m.Event != messageEvent {
|
||||||
return errUnexpectedMessageType
|
return errUnexpectedMessageType
|
||||||
|
@ -291,8 +302,7 @@ func (c *messageCache) addMessages(ms []*message) error {
|
||||||
if m.Sender.IsValid() {
|
if m.Sender.IsValid() {
|
||||||
sender = m.Sender.String()
|
sender = m.Sender.String()
|
||||||
}
|
}
|
||||||
_, err := tx.Exec(
|
_, err := statement.Exec(
|
||||||
insertMessageQuery,
|
|
||||||
m.ID,
|
m.ID,
|
||||||
m.Time,
|
m.Time,
|
||||||
m.Topic,
|
m.Topic,
|
||||||
|
|
Loading…
Reference in a new issue