Skip to content

Commit

Permalink
limit HistoryBySchedule to 3 concurrent calls (#3334)
Browse files Browse the repository at this point in the history
  • Loading branch information
mastercactapus authored Oct 5, 2023
1 parent 3319556 commit 47df50f
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions oncall/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type Store struct {

ruleStore *rule.Store
schedStore *schedule.Store

histLim chan struct{}
}

// NewStore will create a new DB, preparing required statements using the provided context.
Expand All @@ -68,6 +70,8 @@ func NewStore(ctx context.Context, db *sql.DB, ruleStore *rule.Store, schedStore
ruleStore: ruleStore,
schedStore: schedStore,

histLim: make(chan struct{}, 3), // limit concurrent history queries to 3

schedOverrides: p.P(`
select
start_time,
Expand Down Expand Up @@ -202,6 +206,16 @@ func (s *Store) HistoryBySchedule(ctx context.Context, scheduleID string, start,
return nil, err
}

// Since this operation is expensive, and holds open a transaction for a long time,
// for several queries, we limit the number of concurrent operations to prevent
// exhausting the database connection pool.
select {
case s.histLim <- struct{}{}:
defer func() { <-s.histLim }()
case <-ctx.Done():
return nil, ctx.Err()
}

tx, err := s.db.BeginTx(ctx, &sql.TxOptions{
ReadOnly: true,
Isolation: sql.LevelRepeatableRead,
Expand Down

0 comments on commit 47df50f

Please sign in to comment.