@@ -18,13 +18,13 @@ import (
1818 "github.com/ethereum/go-ethereum/accounts/keystore"
1919 "github.com/ethereum/go-ethereum/crypto"
2020 "github.com/pkg/errors"
21+ "github.com/rs/zerolog/log"
2122
2223 "github.com/smartcontractkit/chainlink-testing-framework/framework"
2324 "github.com/smartcontractkit/chainlink-testing-framework/framework/components/clnode"
2425
2526 "github.com/ethereum/go-ethereum/common"
2627 "github.com/go-resty/resty/v2"
27- "github.com/rs/zerolog/log"
2828 "golang.org/x/sync/errgroup"
2929)
3030
@@ -33,6 +33,14 @@ const (
3333 ChainlinkKeyPassword string = "twochains"
3434 // NodeURL string for logging
3535 NodeURL string = "Node URL"
36+ // DefaultRetries default CL node client retries
37+ DefaultRetries = 30
38+ // DefaultRetryInterval default CL node client retry interval
39+ DefaultRetryInterval = 5 * time .Second
40+ // DefaultTimeout is a default CL node client timeout
41+ DefaultTimeout = 10 * time .Second
42+ // DefaultAuthRetryCount is a default CL node authorization retry count
43+ DefaultAuthRetryCount = 20
3644)
3745
3846var (
@@ -82,36 +90,56 @@ func New(outs []*clnode.Output) ([]*ChainlinkClient, error) {
8290 return clients , nil
8391}
8492
85- func initRestyClient (url string , email string , password string , headers map [string ]string , timeout * time.Duration ) (* resty.Client , error ) {
93+ func initRestyClient (url string , email string , password string , headers map [string ]string , _ * time.Duration ) (* resty.Client , error ) {
8694 isDebug := os .Getenv ("RESTY_DEBUG" ) == "true"
8795 // G402 - TODO: certificates
8896 //nolint
89- rc := resty .New ().SetBaseURL (url ).SetHeaders (headers ).SetTLSClientConfig (& tls.Config {InsecureSkipVerify : true }).SetDebug (isDebug )
90- if timeout != nil {
91- rc .SetTimeout (* timeout )
92- }
93- session := & Session {Email : email , Password : password }
94- // Retry the connection on boot up, sometimes pods can still be starting up and not ready to accept connections
95- var resp * resty.Response
97+ s := & Session {Email : email , Password : password }
98+ rc := resty .New ().
99+ SetBaseURL (url ).
100+ SetHeaders (headers ).
101+ SetTLSClientConfig (& tls.Config {InsecureSkipVerify : true }). //nolint:gosec
102+ SetDebug (isDebug ).
103+ SetRetryWaitTime (DefaultRetryInterval ).
104+ SetRetryCount (DefaultRetries ).
105+ SetRetryAfter (func (c * resty.Client , r * resty.Response ) (time.Duration , error ) {
106+ return DefaultRetryInterval , nil
107+ }).
108+ SetTimeout (DefaultTimeout )
109+
110+ rc .AddRetryCondition (func (r * resty.Response , err error ) bool {
111+ return r .StatusCode () == http .StatusNotFound
112+ })
113+
114+ // Retry the connection on boot up, retrying authorization every time slows down AddRetryCondition too much
96115 var err error
97- retryCount := 20
98- for i := 0 ; i < retryCount ; i ++ {
99- resp , err = rc .R ().SetBody (session ).Post ("/sessions" )
116+ for i := 0 ; i < DefaultAuthRetryCount ; i ++ {
117+ err = Authorize (rc , s )
100118 if err != nil {
101- log .Warn ().Err (err ).Str ("URL" , url ).Interface ("Session Details" , session ).Msg ("Error connecting to Chainlink node, retrying" )
102- time .Sleep (5 * time . Second )
119+ log .Warn ().Err (err ).Str ("URL" , url ).Interface ("Session Details" , s ).Msg ("Error connecting to Chainlink node, retrying" )
120+ time .Sleep (DefaultRetryInterval )
103121 } else {
104122 break
105123 }
106124 }
107125 if err != nil {
108- return nil , fmt .Errorf ("error connecting to chainlink node after %d attempts: %w" , retryCount , err )
126+ return nil , fmt .Errorf ("error connecting to chainlink node after %d attempts: %w" , DefaultAuthRetryCount , err )
109127 }
110- rc .SetCookies (resp .Cookies ())
111128 framework .L .Debug ().Str ("URL" , url ).Msg ("Connected to Chainlink node" )
112129 return rc , nil
113130}
114131
132+ func Authorize (rc * resty.Client , session * Session ) error {
133+ resp , err := rc .R ().
134+ SetBody (session ).
135+ Post ("/sessions" )
136+ if err != nil {
137+ return fmt .Errorf ("error authorizing in CL node: %w" , err )
138+ }
139+ rc .SetCookies (resp .Cookies ())
140+ return nil
141+ }
142+
115143// URL Chainlink instance http url
116144func (c * ChainlinkClient ) URL () string {
117145 return c .Config .URL
@@ -192,7 +220,6 @@ func (c *ChainlinkClient) WaitHealthy(pattern, status string, attempts uint) err
192220 Msg ("Retrying health check" )
193221 }),
194222 )
195-
196223 if err != nil {
197224 return fmt .Errorf ("health check failed after %d attempts: %w" , attempts , err )
198225 }
0 commit comments