Skip to content

Commit 8bfd336

Browse files
entropidelicMarcosNicolau
authored andcommitted
Add first version of retry connection in Go
1 parent 6873cbe commit 8bfd336

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

core/connection.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package connection
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/cenkalti/backoff/v4"
8+
)
9+
10+
type PermanentError struct {
11+
Inner error
12+
}
13+
14+
func (e PermanentError) Error() string { return e.Inner.Error() }
15+
func (e PermanentError) Unwrap() error {
16+
return e.Inner
17+
}
18+
func (e PermanentError) Is(err error) bool {
19+
_, ok := err.(PermanentError)
20+
return ok
21+
}
22+
23+
func Retry(functionToRetry func() (interface{}, error), minDelay uint64, factor float64, maxTries uint64) (interface{}, error) {
24+
f := func() (interface{}, error) {
25+
val, err := functionToRetry()
26+
if perm, ok := err.(PermanentError); err != nil && ok {
27+
fmt.Println("ENTRE")
28+
return nil, backoff.Permanent(perm.Inner)
29+
}
30+
return val, err
31+
}
32+
33+
randomOption := backoff.WithRandomizationFactor(0)
34+
35+
initialRetryOption := backoff.WithInitialInterval(time.Millisecond * time.Duration(minDelay))
36+
multiplierOption := backoff.WithMultiplier(factor)
37+
expBackoff := backoff.NewExponentialBackOff(randomOption, multiplierOption, initialRetryOption)
38+
maxRetriesBackoff := backoff.WithMaxRetries(expBackoff, maxTries)
39+
40+
return backoff.RetryWithData(f, maxRetriesBackoff)
41+
}

core/connection_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package connection_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/yetanotherco/aligned_layer/core"
8+
)
9+
10+
func DummyFunction(x uint64) (uint64, error) {
11+
fmt.Println("Doing some work...")
12+
if x == 42 {
13+
return 0, connection.PermanentError{Inner: fmt.Errorf("Permanent error!")}
14+
} else if x < 42 {
15+
return 0, fmt.Errorf("Transient error!")
16+
}
17+
return x, nil
18+
}
19+
20+
func TestRetry(t *testing.T) {
21+
function := func() (interface{}, error) { return DummyFunction(42) }
22+
data, err := connection.Retry(function, 1000, 2, 3)
23+
if err != nil {
24+
t.Errorf("Retry error!: %s", err)
25+
} else {
26+
fmt.Printf("DATA: %d\n", data)
27+
}
28+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require (
4040
github.com/bits-and-blooms/bitset v1.10.0 // indirect
4141
github.com/blang/semver/v4 v4.0.0 // indirect
4242
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
43+
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
4344
github.com/cespare/xxhash/v2 v2.3.0 // indirect
4445
github.com/consensys/bavard v0.1.13 // indirect
4546
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOF
5656
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
5757
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
5858
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
59+
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
60+
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
5961
github.com/cespare/cp v0.1.0 h1:SE+dxFebS7Iik5LK0tsi1k9ZCxEaFX4AjQmoyA+1dJk=
6062
github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s=
6163
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=

operator/sp1/sp1_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
)
99

1010
const ProofFilePath = "../../scripts/test_files/sp1/sp1_fibonacci.proof"
11-
1211
const ElfFilePath = "../../scripts/test_files/sp1/sp1_fibonacci.elf"
1312

1413
func TestFibonacciSp1ProofVerifies(t *testing.T) {

0 commit comments

Comments
 (0)