Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RPC response timeouts, move them out to the config file #326

Merged
merged 1 commit into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions app/query/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
walletLoadRetries = 3
walletLoadRetryWait = 100 * time.Millisecond
builtinHookName = "builtin"
defaultRPCTimeout = 120 * time.Second

// AllMethodsHook is used as the first argument to Add*Hook to make it apply to all methods
AllMethodsHook = ""
Expand Down Expand Up @@ -87,40 +88,33 @@ func NewCaller(endpoint string, userID int) *Caller {
return c
}

func (c *Caller) newRPCClient(timeInSecondstimeOut time.Duration) jsonrpc.RPCClient {
func (c *Caller) newRPCClient(timeout time.Duration) jsonrpc.RPCClient {
client := jsonrpc.NewClientWithOpts(c.endpoint, &jsonrpc.RPCClientOpts{
HTTPClient: &http.Client{
Timeout: sdkrouter.RPCTimeout,
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: timeInSecondstimeOut,
KeepAlive: timeInSecondstimeOut,
Timeout: 15 * time.Second,
KeepAlive: 120 * time.Second,
}).Dial,
TLSHandshakeTimeout: 30 * time.Second,
ResponseHeaderTimeout: 600 * time.Second,
ResponseHeaderTimeout: timeout,
ExpectContinueTimeout: 1 * time.Second,
},
},
})
return client
}

func (c *Caller) getTimeSpanForJSONRPCMethod(method string) time.Duration {
timeSpanMap := map[string]time.Duration{
"txo_spend": time.Hour * 3,
"txo_list": time.Minute * 10,
"transaction_list": time.Minute * 10,
func (c *Caller) getRPCTimeout(method string) time.Duration {
t := config.GetRPCTimeout(method)
if t != nil {
return *t
}

if timeSpan, ok := timeSpanMap[method]; ok {
return timeSpan
}

return time.Minute * 2
return defaultRPCTimeout
}

func (c *Caller) getRPCClientForMethod(method string) jsonrpc.RPCClient {
var client jsonrpc.RPCClient = c.newRPCClient(c.getTimeSpanForJSONRPCMethod(method))
func (c *Caller) getRPCClient(method string) jsonrpc.RPCClient {
var client jsonrpc.RPCClient = c.newRPCClient(c.getRPCTimeout(method))
return client
}

Expand Down Expand Up @@ -223,7 +217,7 @@ func (c *Caller) SendQuery(q *Query) (*jsonrpc.RPCResponse, error) {

for i := 0; i < walletLoadRetries; i++ {
start := time.Now()
client := c.getRPCClientForMethod(q.Method())
client := c.getRPCClient(q.Method())
r, err = client.CallRaw(q.Request)
c.Duration = time.Since(start).Seconds()

Expand Down
40 changes: 26 additions & 14 deletions app/query/caller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,23 +543,35 @@ func TestCaller_CallQueryWithRetry(t *testing.T) {
require.Nil(t, r.Error)
}

func TestGetTimeSpanForJSONRPCMethod(t *testing.T) {
addr := test.RandServerAddress(t)
c := NewCaller(addr, 1)
threeHours := time.Hour * 3
tenMinutes := time.Minute * 10
twoMinutes := time.Minute * 2
duration := c.getTimeSpanForJSONRPCMethod("txo_spend")
require.Equal(t, threeHours, duration)
func TestCaller_timeouts(t *testing.T) {
srv := test.MockHTTPServer(nil)
defer srv.Close()

duration = c.getTimeSpanForJSONRPCMethod("txo_list")
require.Equal(t, tenMinutes, duration)
config.Override("RPCTimeouts", map[string]string{
"resolve": "300ms",
})

c := NewCaller(srv.URL, 0)
q, err := NewQuery(jsonrpc.NewRequest("resolve"), "")
require.NoError(t, err)
go func() {
time.Sleep(200 * time.Millisecond)
srv.NextResponse <- test.ResToStr(t, &jsonrpc.RPCResponse{
JSONRPC: "2.0",
Result: `""`,
})
time.Sleep(500 * time.Millisecond)
srv.NextResponse <- test.ResToStr(t, &jsonrpc.RPCResponse{
JSONRPC: "2.0",
Result: `""`,
})
}()

duration = c.getTimeSpanForJSONRPCMethod("transaction_list")
require.Equal(t, tenMinutes, duration)
_, err = c.SendQuery(q)
require.NoError(t, err)

duration = c.getTimeSpanForJSONRPCMethod("Any_other_method")
require.Equal(t, twoMinutes, duration)
_, err = c.SendQuery(q)
require.Regexp(t, `timeout awaiting response headers`, err.Error())
}

func TestCaller_DontReloadWalletAfterOtherErrors(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions apps/lbrytv/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/lbryio/lbrytv/models"

"github.com/sirupsen/logrus"
"github.com/spf13/cast"
)

const (
Expand Down Expand Up @@ -154,3 +155,15 @@ func GetTokenCacheTimeout() time.Duration {
func GetCORSDomains() []string {
return Config.Viper.GetStringSlice("CORSDomains")
}

func GetRPCTimeout(method string) *time.Duration {
ts := Config.Viper.GetStringMapString("RPCTimeouts")
if ts != nil {
if t, ok := ts[method]; ok {
d := cast.ToDuration(t)
return &d

}
}
return nil
}
12 changes: 12 additions & 0 deletions apps/lbrytv/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,15 @@ func TestGetTokenCacheTimeout(t *testing.T) {
defer Config.RestoreOverridden()
assert.Equal(t, 325*time.Second, GetTokenCacheTimeout())
}

func TestGetRPCTimeout(t *testing.T) {
Config.Override("RPCTimeouts", map[string]string{
"txo_list": "12s",
"resolve": "200ms",
})
defer Config.RestoreOverridden()

assert.Equal(t, 12*time.Second, *GetRPCTimeout("txo_list"))
assert.Equal(t, 200*time.Millisecond, *GetRPCTimeout("resolve"))
assert.Nil(t, GetRPCTimeout("random_method"))
}
6 changes: 6 additions & 0 deletions lbrytv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ PaidContentURL: https://cdn.lbryplayer.xyz/api/v3/streams/paid/
CORSDomains:
- https://odysee.com
- https://lbry.tv

RPCTimeouts:
txo_spend: 1m
txo_list: 1m
transaction_list: 1m
publish: 1m