Skip to content

Commit

Permalink
Change the timeout from all queries to per query (#9471)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhsu-ms authored Jul 27, 2021
1 parent 51720f3 commit 27b9808
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
1 change: 1 addition & 0 deletions plugins/inputs/sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ generate it using `telegraf --usage <plugin-name>`.
dsn = "username:password@mysqlserver:3307/dbname?param=value"

## Timeout for any operation
## Note that the timeout for queries is per query not per gather.
# timeout = "5s"

## Connection time limits
Expand Down
8 changes: 3 additions & 5 deletions plugins/inputs/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const sampleConfig = `
dsn = "username:password@mysqlserver:3307/dbname?param=value"
## Timeout for any operation
## Note that the timeout for queries is per query not per gather.
# timeout = "5s"
## Connection time limits
Expand Down Expand Up @@ -486,16 +487,13 @@ func (s *SQL) Stop() {

func (s *SQL) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(s.Timeout))
defer cancel()

tstart := time.Now()
for _, query := range s.Queries {
wg.Add(1)

go func(q Query) {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(s.Timeout))
defer cancel()
if err := s.executeQuery(ctx, acc, q, tstart); err != nil {
acc.AddError(err)
}
Expand Down

0 comments on commit 27b9808

Please sign in to comment.