Skip to content

Commit b825361

Browse files
author
root
committed
reporter: make custom query fetch timeout configurable
1 parent ff13b7d commit b825361

3 files changed

Lines changed: 77 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Custom query API keys are read from the generated `custom_query_config.toml` ent
4646

4747
Custom query JSON API responses are cached briefly in memory to reduce duplicate upstream calls during fast cycle-list polling. Set `CUSTOM_QUERY_CACHE_TTL` to a Go duration such as `3s` or `500ms` to tune the freshness window. The default is `3s`; set it to `0` to disable this cache.
4848

49+
The full custom-query fetch is limited to five seconds by default. Set `CUSTOM_QUERY_FETCH_TIMEOUT` to a positive Go duration such as `2s` to preserve more of the reporting window for transaction submission when an optional source is slow.
50+
4951
## Task loops
5052

5153
## PriceFetcher

custom_query/request.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"math"
7+
"os"
78
"strconv"
89
"strings"
910
"sync"
@@ -19,6 +20,25 @@ import (
1920
"github.com/cosmos/cosmos-sdk/telemetry"
2021
)
2122

23+
const (
24+
customQueryFetchTimeoutEnv = "CUSTOM_QUERY_FETCH_TIMEOUT"
25+
defaultCustomQueryFetchTimeout = 5 * time.Second
26+
)
27+
28+
func customQueryFetchTimeout() time.Duration {
29+
rawTimeout := strings.TrimSpace(os.Getenv(customQueryFetchTimeoutEnv))
30+
if rawTimeout == "" {
31+
return defaultCustomQueryFetchTimeout
32+
}
33+
34+
timeout, err := time.ParseDuration(rawTimeout)
35+
if err != nil || timeout <= 0 {
36+
return defaultCustomQueryFetchTimeout
37+
}
38+
39+
return timeout
40+
}
41+
2242
// Result holds the value returned from an endpoint
2343
type Result struct {
2444
Value float64
@@ -65,8 +85,8 @@ func FetchPrice(
6585
query QueryConfig,
6686
priceCache *pricefeedservertypes.MarketToExchangePrices,
6787
) (*FetchPriceResult, error) {
68-
// Create a context with timeout
69-
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
88+
// Bound the full fetch so reporting still has time to submit before the cycle closes.
89+
ctx, cancel := context.WithTimeout(ctx, customQueryFetchTimeout())
7090
defer cancel()
7191

7292
totalEndpoints := len(query.RpcReaders) + len(query.ContractReaders) + len(query.CombinedReaders)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package customquery
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestCustomQueryFetchTimeout(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
value string
14+
expected time.Duration
15+
}{
16+
{
17+
name: "unset uses default",
18+
expected: defaultCustomQueryFetchTimeout,
19+
},
20+
{
21+
name: "valid duration",
22+
value: "2s",
23+
expected: 2 * time.Second,
24+
},
25+
{
26+
name: "surrounding whitespace",
27+
value: " 1500ms ",
28+
expected: 1500 * time.Millisecond,
29+
},
30+
{
31+
name: "invalid duration uses default",
32+
value: "soon",
33+
expected: defaultCustomQueryFetchTimeout,
34+
},
35+
{
36+
name: "zero duration uses default",
37+
value: "0s",
38+
expected: defaultCustomQueryFetchTimeout,
39+
},
40+
{
41+
name: "negative duration uses default",
42+
value: "-1s",
43+
expected: defaultCustomQueryFetchTimeout,
44+
},
45+
}
46+
47+
for _, test := range tests {
48+
t.Run(test.name, func(t *testing.T) {
49+
t.Setenv(customQueryFetchTimeoutEnv, test.value)
50+
require.Equal(t, test.expected, customQueryFetchTimeout())
51+
})
52+
}
53+
}

0 commit comments

Comments
 (0)