-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from forta-network/caner/json-rpc-redundancy
Support a ring of fallback RPCs in Ethereum client
- Loading branch information
Showing
7 changed files
with
258 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package provider | ||
|
||
import ( | ||
"github.com/forta-network/forta-core-go/utils/slicering" | ||
) | ||
|
||
// Element is the type of element provided by the provider. | ||
type Element interface { | ||
Close() | ||
} | ||
|
||
// Provider keeps one or many elements provides when requested. | ||
type Provider[T Element] interface { | ||
Provide() T | ||
Next() T | ||
Close() | ||
} | ||
|
||
// RingProvider provides elements from a slice-backed thread-safe ring. | ||
type RingProvider[T Element] struct { | ||
*slicering.ThreadSafeRing[T] | ||
} | ||
|
||
// NewRingProvider creates a new ring provider. | ||
func NewRingProvider[T Element](elements ...T) Provider[T] { | ||
return &RingProvider[T]{ThreadSafeRing: slicering.NewThreadSafeRing(elements...)} | ||
} | ||
|
||
// Provide provides the currently pointed element. | ||
func (rp *RingProvider[T]) Provide() T { | ||
return rp.Current() | ||
} | ||
|
||
// Close closes all elements in the ring. | ||
func (rp *RingProvider[T]) Close() { | ||
for _, el := range rp.Elements() { | ||
el.Close() | ||
} | ||
} | ||
|
||
// Ensuring type checks below. | ||
|
||
var _ Provider[*dummyElement] = &RingProvider[*dummyElement]{} | ||
|
||
type dummyElement struct{} | ||
|
||
func (dc *dummyElement) Close() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package provider_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/forta-network/forta-core-go/ethereum/provider" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type testElement struct { | ||
closed bool | ||
} | ||
|
||
func (te *testElement) Close() { | ||
te.closed = true | ||
} | ||
|
||
func TestRingProvider(t *testing.T) { | ||
r := require.New(t) | ||
|
||
el1 := &testElement{} | ||
el2 := &testElement{} | ||
|
||
p := provider.NewRingProvider(el1, el2) | ||
r.Equal(el1, p.Provide()) | ||
r.Equal(el1, p.Provide()) | ||
r.Equal(el2, p.Next()) | ||
r.Equal(el2, p.Provide()) | ||
r.Equal(el1, p.Next()) | ||
|
||
r.False(el1.closed) | ||
r.False(el2.closed) | ||
|
||
p.Close() | ||
|
||
r.True(el1.closed) | ||
r.True(el2.closed) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.