Skip to content

Conversation

@carlcortright
Copy link

@carlcortright carlcortright commented Jul 27, 2022

Support retries with a fixed interval backoff.

log logger
Debug bool
retries int
retryBackoff int // milliseconds
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why milliseconds? Golang has the type time.Duration

@onrik
Copy link
Owner

onrik commented Jul 27, 2022

Thanks for PR. But i think this is not responsibility of ethrpc. Retries can be implemented by transaport in custom http client.

package main

import (
    "fmt"
    "log"
    "time"

    "github.com/onrik/ethrpc"
)

type RetryTransport struct {
	transport http.RoundTripper
	attempts  int
	delay     time.Duration
}

func (t *RetryTransport) RoundTrip(request *http.Request) (r *http.Response, err error) {
	for i := 0; i < t.attempts; i++ {
		r, err = t.transport.RoundTrip(request)
		if err != nil {
			time.Sleep(t.delay)
			continue
		}
	}

	return
}

func main() {
    client := ethrpc.New("http://127.0.0.1:8545", WithHttpClient(&http.Client{
       Transport: &RetryTransport{
		transport: http.DefaultTransport,
		attempts:  10,
                delay:     10 * time.Millisecond,
       }
    })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants