Skip to content

Commit 3ab75f1

Browse files
committed
Use ctx everywhere & cache object only when the tx succeeds
1 parent 74cab07 commit 3ab75f1

File tree

8 files changed

+161
-123
lines changed

8 files changed

+161
-123
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/teambition/rrule-go v1.8.2
1212
go.uber.org/zap v1.23.0
1313
golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d
14+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
1415
)
1516

1617
require (
@@ -30,7 +31,6 @@ require (
3031
github.com/ssgreg/journald v1.0.0 // indirect
3132
go.uber.org/atomic v1.7.0 // indirect
3233
go.uber.org/multierr v1.6.0 // indirect
33-
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
3434
golang.org/x/sys v0.1.0 // indirect
3535
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
3636
gopkg.in/yaml.v3 v3.0.1 // indirect

internal/incident/db_types.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package incident
22

33
import (
4+
"context"
45
"fmt"
56
"github.com/icinga/icinga-notifications/internal/event"
67
"github.com/icinga/icinga-notifications/internal/recipient"
@@ -33,15 +34,15 @@ func (i *IncidentRow) Upsert() interface{} {
3334
// Sync synchronizes incidents to the database.
3435
// Fetches the last inserted incident id and modifies this incident's id.
3536
// Returns an error on database failure.
36-
func (i *IncidentRow) Sync(tx *sqlx.Tx, db *icingadb.DB, upsert bool) error {
37+
func (i *IncidentRow) Sync(ctx context.Context, tx *sqlx.Tx, db *icingadb.DB, upsert bool) error {
3738
if upsert {
3839
stmt, _ := db.BuildUpsertStmt(i)
39-
_, err := tx.NamedExec(stmt, i)
40+
_, err := tx.NamedExecContext(ctx, stmt, i)
4041
if err != nil {
4142
return fmt.Errorf("failed to upsert incident: %s", err)
4243
}
4344
} else {
44-
incidentId, err := utils.InsertAndFetchId(tx, utils.BuildInsertStmtWithout(db, i, "id"), i)
45+
incidentId, err := utils.InsertAndFetchId(ctx, tx, utils.BuildInsertStmtWithout(db, i, "id"), i)
4546
if err != nil {
4647
return err
4748
}

internal/incident/history.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package incident
22

33
import (
4+
"context"
45
"fmt"
56
"github.com/icinga/icinga-notifications/internal/event"
67
"github.com/icinga/icinga-notifications/internal/recipient"
@@ -14,7 +15,7 @@ import (
1415
// Sync initiates an *incident.IncidentRow from the current incident state and syncs it with the database.
1516
// Before syncing any incident related database entries, this method should be called at least once.
1617
// Returns an error on db failure.
17-
func (i *Incident) Sync(tx *sqlx.Tx) error {
18+
func (i *Incident) Sync(ctx context.Context, tx *sqlx.Tx) error {
1819
incidentRow := &IncidentRow{
1920
ID: i.incidentRowID,
2021
ObjectID: i.Object.ID,
@@ -23,7 +24,7 @@ func (i *Incident) Sync(tx *sqlx.Tx) error {
2324
Severity: i.Severity(),
2425
}
2526

26-
err := incidentRow.Sync(tx, i.db, i.incidentRowID != 0)
27+
err := incidentRow.Sync(ctx, tx, i.db, i.incidentRowID != 0)
2728
if err != nil {
2829
return err
2930
}
@@ -33,19 +34,19 @@ func (i *Incident) Sync(tx *sqlx.Tx) error {
3334
return nil
3435
}
3536

36-
func (i *Incident) AddHistory(tx *sqlx.Tx, historyRow *HistoryRow, fetchId bool) (types.Int, error) {
37+
func (i *Incident) AddHistory(ctx context.Context, tx *sqlx.Tx, historyRow *HistoryRow, fetchId bool) (types.Int, error) {
3738
historyRow.IncidentID = i.incidentRowID
3839

3940
stmt := utils.BuildInsertStmtWithout(i.db, historyRow, "id")
4041
if fetchId {
41-
historyId, err := utils.InsertAndFetchId(tx, stmt, historyRow)
42+
historyId, err := utils.InsertAndFetchId(ctx, tx, stmt, historyRow)
4243
if err != nil {
4344
return types.Int{}, err
4445
}
4546

4647
return utils.ToDBInt(historyId), nil
4748
} else {
48-
_, err := tx.NamedExec(stmt, historyRow)
49+
_, err := tx.NamedExecContext(ctx, stmt, historyRow)
4950
if err != nil {
5051
return types.Int{}, err
5152
}
@@ -54,30 +55,30 @@ func (i *Incident) AddHistory(tx *sqlx.Tx, historyRow *HistoryRow, fetchId bool)
5455
return types.Int{}, nil
5556
}
5657

57-
func (i *Incident) AddEscalationTriggered(tx *sqlx.Tx, state *EscalationState, hr *HistoryRow) (types.Int, error) {
58+
func (i *Incident) AddEscalationTriggered(ctx context.Context, tx *sqlx.Tx, state *EscalationState, hr *HistoryRow) (types.Int, error) {
5859
state.IncidentID = i.incidentRowID
5960

6061
stmt, _ := i.db.BuildUpsertStmt(state)
61-
_, err := tx.NamedExec(stmt, state)
62+
_, err := tx.NamedExecContext(ctx, stmt, state)
6263
if err != nil {
6364
return types.Int{}, err
6465
}
6566

66-
return i.AddHistory(tx, hr, true)
67+
return i.AddHistory(ctx, tx, hr, true)
6768
}
6869

6970
// AddEvent Inserts incident history record to the database and returns an error on db failure.
70-
func (i *Incident) AddEvent(tx *sqlx.Tx, ev *event.Event) error {
71+
func (i *Incident) AddEvent(ctx context.Context, tx *sqlx.Tx, ev *event.Event) error {
7172
ie := &EventRow{IncidentID: i.incidentRowID, EventID: ev.ID}
7273
stmt, _ := i.db.BuildInsertStmt(ie)
73-
_, err := tx.NamedExec(stmt, ie)
74+
_, err := tx.NamedExecContext(ctx, stmt, ie)
7475

7576
return err
7677
}
7778

7879
// AddRecipient adds recipient from the given *rule.Escalation to this incident.
7980
// Syncs also all the recipients with the database and returns an error on db failure.
80-
func (i *Incident) AddRecipient(tx *sqlx.Tx, escalation *rule.Escalation, eventId int64) error {
81+
func (i *Incident) AddRecipient(ctx context.Context, tx *sqlx.Tx, escalation *rule.Escalation, eventId int64) error {
8182
newRole := RoleRecipient
8283
if i.HasManager() {
8384
newRole = RoleSubscriber
@@ -110,7 +111,7 @@ func (i *Incident) AddRecipient(tx *sqlx.Tx, escalation *rule.Escalation, eventI
110111
OldRecipientRole: oldRole,
111112
}
112113

113-
_, err := i.AddHistory(tx, hr, false)
114+
_, err := i.AddHistory(ctx, tx, hr, false)
114115
if err != nil {
115116
return err
116117
}
@@ -119,7 +120,7 @@ func (i *Incident) AddRecipient(tx *sqlx.Tx, escalation *rule.Escalation, eventI
119120
}
120121

121122
stmt, _ := i.db.BuildUpsertStmt(cr)
122-
_, err := tx.NamedExec(stmt, cr)
123+
_, err := tx.NamedExecContext(ctx, stmt, cr)
123124
if err != nil {
124125
return fmt.Errorf("failed to upsert incident contact %s: %w", r, err)
125126
}
@@ -130,18 +131,18 @@ func (i *Incident) AddRecipient(tx *sqlx.Tx, escalation *rule.Escalation, eventI
130131

131132
// AddRuleMatchedHistory syncs the given *rule.Rule and history entry to the database.
132133
// Returns an error on database failure.
133-
func (i *Incident) AddRuleMatchedHistory(tx *sqlx.Tx, r *rule.Rule, hr *HistoryRow) (types.Int, error) {
134+
func (i *Incident) AddRuleMatchedHistory(ctx context.Context, tx *sqlx.Tx, r *rule.Rule, hr *HistoryRow) (types.Int, error) {
134135
rr := &RuleRow{IncidentID: i.incidentRowID, RuleID: r.ID}
135136
stmt, _ := i.db.BuildUpsertStmt(rr)
136-
_, err := tx.NamedExec(stmt, rr)
137+
_, err := tx.NamedExecContext(ctx, stmt, rr)
137138
if err != nil {
138139
return types.Int{}, err
139140
}
140141

141-
return i.AddHistory(tx, hr, true)
142+
return i.AddHistory(ctx, tx, hr, true)
142143
}
143144

144-
func (i *Incident) AddSourceSeverity(tx *sqlx.Tx, severity event.Severity, sourceID int64) error {
145+
func (i *Incident) AddSourceSeverity(ctx context.Context, tx *sqlx.Tx, severity event.Severity, sourceID int64) error {
145146
i.SeverityBySource[sourceID] = severity
146147

147148
sourceSeverity := &SourceSeverity{
@@ -151,7 +152,7 @@ func (i *Incident) AddSourceSeverity(tx *sqlx.Tx, severity event.Severity, sourc
151152
}
152153

153154
stmt, _ := i.db.BuildUpsertStmt(sourceSeverity)
154-
_, err := tx.NamedExec(stmt, sourceSeverity)
155+
_, err := tx.NamedExecContext(ctx, stmt, sourceSeverity)
155156

156157
return err
157158
}

0 commit comments

Comments
 (0)