mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-19 12:01:27 +02:00
sqldb+config: validate maximum batch size config value
Now that the SQL query config values are configurable, we add some validation to make sure that the user doesnt set a max batch size that is larger than the limits for sqlite/postgres that have been determined by the TestSQLSliceQueries test.
This commit is contained in:
@@ -5,6 +5,18 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
// maxSQLiteBatchSize is the maximum number of items that can be
|
||||
// included in a batch query IN clause for SQLite. This was determined
|
||||
// using the TestSQLSliceQueries test.
|
||||
maxSQLiteBatchSize = 32766
|
||||
|
||||
// maxPostgresBatchSize is the maximum number of items that can be
|
||||
// included in a batch query IN clause for Postgres. This was determined
|
||||
// using the TestSQLSliceQueries test.
|
||||
maxPostgresBatchSize = 65535
|
||||
)
|
||||
|
||||
// QueryConfig holds configuration values for SQL queries.
|
||||
//
|
||||
//nolint:ll
|
||||
@@ -18,6 +30,34 @@ type QueryConfig struct {
|
||||
MaxPageSize int32 `long:"max-page-size" description:"The maximum number of items to return in a single page of results. This is used for paginated queries."`
|
||||
}
|
||||
|
||||
// Validate checks that the QueryConfig values are valid.
|
||||
func (c *QueryConfig) Validate(sqlite bool) error {
|
||||
if c.MaxBatchSize <= 0 {
|
||||
return fmt.Errorf("max batch size must be greater than "+
|
||||
"zero, got %d", c.MaxBatchSize)
|
||||
}
|
||||
if c.MaxPageSize <= 0 {
|
||||
return fmt.Errorf("max page size must be greater than "+
|
||||
"zero, got %d", c.MaxPageSize)
|
||||
}
|
||||
|
||||
if sqlite {
|
||||
if c.MaxBatchSize > maxSQLiteBatchSize {
|
||||
return fmt.Errorf("max batch size for SQLite cannot "+
|
||||
"exceed %d, got %d", maxSQLiteBatchSize,
|
||||
c.MaxBatchSize)
|
||||
}
|
||||
} else {
|
||||
if c.MaxBatchSize > maxPostgresBatchSize {
|
||||
return fmt.Errorf("max batch size for Postgres cannot "+
|
||||
"exceed %d, got %d", maxPostgresBatchSize,
|
||||
c.MaxBatchSize)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DefaultQueryConfig returns a default configuration for SQL queries.
|
||||
func DefaultQueryConfig() *QueryConfig {
|
||||
return &QueryConfig{
|
||||
|
Reference in New Issue
Block a user