diff --git a/cmd/config.go b/cmd/config.go index 1d6b5022b..412e14a28 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -88,14 +88,18 @@ type NotificationConfig struct { TransactionTimeout string `yaml:"transaction_timeout"` // TransactionMaxRetries defines the maximum number of attempts to make a transaction TransactionMaxRetries int `yaml:"transaction_max_retries"` + // TransactionHeuristicLimit maximum allowable limit, after this limit all notifications + // without limit will be taken + TransactionHeuristicLimit int64 `yaml:"transaction_heuristic_limit"` } // GetSettings returns notification storage configuration func (notificationConfig *NotificationConfig) GetSettings() redis.NotificationConfig { return redis.NotificationConfig{ - DelayedTime: to.Duration(notificationConfig.DelayedTime), - TransactionTimeout: to.Duration(notificationConfig.TransactionTimeout), - TransactionMaxRetries: notificationConfig.TransactionMaxRetries, + DelayedTime: to.Duration(notificationConfig.DelayedTime), + TransactionTimeout: to.Duration(notificationConfig.TransactionTimeout), + TransactionMaxRetries: notificationConfig.TransactionMaxRetries, + TransactionHeuristicLimit: notificationConfig.TransactionHeuristicLimit, } } diff --git a/database/redis/config.go b/database/redis/config.go index 12d87e760..676a48b4a 100644 --- a/database/redis/config.go +++ b/database/redis/config.go @@ -31,4 +31,7 @@ type NotificationConfig struct { TransactionTimeout time.Duration // TransactionMaxRetries defines the maximum number of attempts to make a transaction TransactionMaxRetries int + // TransactionHeuristicLimit maximum allowable limit, after this limit all notifications + // without limit will be taken + TransactionHeuristicLimit int64 } diff --git a/database/redis/database.go b/database/redis/database.go index 5824a02da..07b0c72c6 100644 --- a/database/redis/database.go +++ b/database/redis/database.go @@ -96,9 +96,10 @@ func NewTestDatabase(logger moira.Logger) *DbConnector { NotificationHistoryQueryLimit: 1000, }, NotificationConfig{ - DelayedTime: time.Minute, - TransactionTimeout: 200 * time.Millisecond, - TransactionMaxRetries: 10, + DelayedTime: time.Minute, + TransactionTimeout: 200 * time.Millisecond, + TransactionMaxRetries: 10, + TransactionHeuristicLimit: 10000, }, testSource) } @@ -112,9 +113,10 @@ func NewTestDatabaseWithIncorrectConfig(logger moira.Logger) *DbConnector { NotificationHistoryQueryLimit: 1000, }, NotificationConfig{ - DelayedTime: time.Minute, - TransactionTimeout: 200 * time.Millisecond, - TransactionMaxRetries: 10, + DelayedTime: time.Minute, + TransactionTimeout: 200 * time.Millisecond, + TransactionMaxRetries: 10, + TransactionHeuristicLimit: 10000, }, testSource) } diff --git a/database/redis/notification.go b/database/redis/notification.go index f22871a95..aab10b38e 100644 --- a/database/redis/notification.go +++ b/database/redis/notification.go @@ -15,11 +15,6 @@ import ( "github.com/moira-alert/moira/database/redis/reply" ) -const ( - transactionTriesLimit = 10 - transactionHeuristicLimit = 10000 -) - // Custom error for transaction error type transactionError struct{} @@ -155,7 +150,7 @@ func (connector *DbConnector) FetchNotifications(to int64, limit int64) ([]*moir } // Hope count will be not greater then limit when we call fetchNotificationsNoLimit - if limit > transactionHeuristicLimit && count < limit/2 { + if limit > connector.notification.TransactionHeuristicLimit && count < limit/2 { return connector.fetchNotificationsNoLimit(to) } @@ -180,7 +175,7 @@ func (connector *DbConnector) fetchNotificationsWithLimit(to int64, limit int64) // fetchNotifecationsWithLimitDo uses WATCH, so transaction may fail and will retry it // see https://redis.io/topics/transactions - for i := 0; i < transactionTriesLimit; i++ { + for i := 0; i < connector.notification.TransactionMaxRetries; i++ { res, err := connector.fetchNotificationsWithLimitDo(to, limit) if err == nil {