forked from cerc-io/plugeth
rpc: add context argument to EthSubscribe
It's inconsistent not to pass it and most callers will work with contexts anyway.
This commit is contained in:
parent
f5f042ffdc
commit
e32925397b
@ -354,12 +354,14 @@ func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
|
|||||||
// sent to the given channel. The element type of the channel must match the
|
// sent to the given channel. The element type of the channel must match the
|
||||||
// expected type of content returned by the subscription.
|
// expected type of content returned by the subscription.
|
||||||
//
|
//
|
||||||
|
// The context argument cancels the RPC request that sets up the subscription but has no
|
||||||
|
// effect on the subscription after EthSubscribe has returned.
|
||||||
//
|
//
|
||||||
// Slow subscribers will be dropped eventually. Client buffers up to 8000 notifications
|
// Slow subscribers will be dropped eventually. Client buffers up to 8000 notifications
|
||||||
// before considering the subscriber dead. The subscription Err channel will receive
|
// before considering the subscriber dead. The subscription Err channel will receive
|
||||||
// ErrSubscriptionQueueOverflow. Use a sufficiently large buffer on the channel or ensure
|
// ErrSubscriptionQueueOverflow. Use a sufficiently large buffer on the channel or ensure
|
||||||
// that the channel usually has at least one reader to prevent this issue.
|
// that the channel usually has at least one reader to prevent this issue.
|
||||||
func (c *Client) EthSubscribe(channel interface{}, args ...interface{}) (*ClientSubscription, error) {
|
func (c *Client) EthSubscribe(ctx context.Context, channel interface{}, args ...interface{}) (*ClientSubscription, error) {
|
||||||
// Check type of channel first.
|
// Check type of channel first.
|
||||||
chanVal := reflect.ValueOf(channel)
|
chanVal := reflect.ValueOf(channel)
|
||||||
if chanVal.Kind() != reflect.Chan || chanVal.Type().ChanDir()&reflect.SendDir == 0 {
|
if chanVal.Kind() != reflect.Chan || chanVal.Type().ChanDir()&reflect.SendDir == 0 {
|
||||||
@ -381,8 +383,6 @@ func (c *Client) EthSubscribe(channel interface{}, args ...interface{}) (*Client
|
|||||||
resp: make(chan *jsonrpcMessage),
|
resp: make(chan *jsonrpcMessage),
|
||||||
sub: newClientSubscription(c, chanVal),
|
sub: newClientSubscription(c, chanVal),
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), subscribeTimeout)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
// Send the subscription request.
|
// Send the subscription request.
|
||||||
// The arrival and validity of the response is signaled on sub.quit.
|
// The arrival and validity of the response is signaled on sub.quit.
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
// In this example, our client whishes to track the latest 'block number'
|
// In this example, our client whishes to track the latest 'block number'
|
||||||
@ -41,7 +42,16 @@ func ExampleClientSubscription() {
|
|||||||
// Connect the client.
|
// Connect the client.
|
||||||
client, _ := rpc.Dial("ws://127.0.0.1:8485")
|
client, _ := rpc.Dial("ws://127.0.0.1:8485")
|
||||||
subch := make(chan Block)
|
subch := make(chan Block)
|
||||||
go subscribeBlocks(client, subch)
|
|
||||||
|
// Ensure that subch receives the latest block.
|
||||||
|
go func() {
|
||||||
|
for i := 0; ; i++ {
|
||||||
|
if i > 0 {
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
}
|
||||||
|
subscribeBlocks(client, subch)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Print events from the subscription as they arrive.
|
// Print events from the subscription as they arrive.
|
||||||
for block := range subch {
|
for block := range subch {
|
||||||
@ -52,32 +62,27 @@ func ExampleClientSubscription() {
|
|||||||
// subscribeBlocks runs in its own goroutine and maintains
|
// subscribeBlocks runs in its own goroutine and maintains
|
||||||
// a subscription for new blocks.
|
// a subscription for new blocks.
|
||||||
func subscribeBlocks(client *rpc.Client, subch chan Block) {
|
func subscribeBlocks(client *rpc.Client, subch chan Block) {
|
||||||
for i := 0; ; i++ {
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
if i > 0 {
|
defer cancel()
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Subscribe to new blocks.
|
// Subscribe to new blocks.
|
||||||
sub, err := client.EthSubscribe(subch, "newBlocks")
|
sub, err := client.EthSubscribe(ctx, subch, "newBlocks")
|
||||||
if err == rpc.ErrClientQuit {
|
if err != nil {
|
||||||
return // Stop reconnecting if the client was closed.
|
fmt.Println("subscribe error:", err)
|
||||||
} else if err != nil {
|
return
|
||||||
fmt.Println("subscribe error:", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// The connection is established now.
|
|
||||||
// Update the channel with the current block.
|
|
||||||
var lastBlock Block
|
|
||||||
if err := client.Call(&lastBlock, "eth_getBlockByNumber", "latest"); err != nil {
|
|
||||||
fmt.Println("can't get latest block:", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
subch <- lastBlock
|
|
||||||
|
|
||||||
// The subscription will deliver events to the channel. Wait for the
|
|
||||||
// subscription to end for any reason, then loop around to re-establish
|
|
||||||
// the connection.
|
|
||||||
fmt.Println("connection lost: ", <-sub.Err())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The connection is established now.
|
||||||
|
// Update the channel with the current block.
|
||||||
|
var lastBlock Block
|
||||||
|
if err := client.CallContext(ctx, &lastBlock, "eth_getBlockByNumber", "latest"); err != nil {
|
||||||
|
fmt.Println("can't get latest block:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
subch <- lastBlock
|
||||||
|
|
||||||
|
// The subscription will deliver events to the channel. Wait for the
|
||||||
|
// subscription to end for any reason, then loop around to re-establish
|
||||||
|
// the connection.
|
||||||
|
fmt.Println("connection lost: ", <-sub.Err())
|
||||||
}
|
}
|
||||||
|
@ -215,7 +215,7 @@ func TestClientSubscribeInvalidArg(t *testing.T) {
|
|||||||
t.Error(string(buf))
|
t.Error(string(buf))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
client.EthSubscribe(arg, "foo_bar")
|
client.EthSubscribe(context.Background(), arg, "foo_bar")
|
||||||
}
|
}
|
||||||
check(true, nil)
|
check(true, nil)
|
||||||
check(true, 1)
|
check(true, 1)
|
||||||
@ -233,7 +233,7 @@ func TestClientSubscribe(t *testing.T) {
|
|||||||
|
|
||||||
nc := make(chan int)
|
nc := make(chan int)
|
||||||
count := 10
|
count := 10
|
||||||
sub, err := client.EthSubscribe(nc, "someSubscription", count, 0)
|
sub, err := client.EthSubscribe(context.Background(), nc, "someSubscription", count, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("can't subscribe:", err)
|
t.Fatal("can't subscribe:", err)
|
||||||
}
|
}
|
||||||
@ -275,7 +275,7 @@ func TestClientSubscribeClose(t *testing.T) {
|
|||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
go func() {
|
go func() {
|
||||||
sub, err = client.EthSubscribe(nc, "hangSubscription", 999)
|
sub, err = client.EthSubscribe(context.Background(), nc, "hangSubscription", 999)
|
||||||
errc <- err
|
errc <- err
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -311,7 +311,7 @@ func TestClientNotificationStorm(t *testing.T) {
|
|||||||
// Subscribe on the server. It will start sending many notifications
|
// Subscribe on the server. It will start sending many notifications
|
||||||
// very quickly.
|
// very quickly.
|
||||||
nc := make(chan int)
|
nc := make(chan int)
|
||||||
sub, err := client.EthSubscribe(nc, "someSubscription", count, 0)
|
sub, err := client.EthSubscribe(ctx, nc, "someSubscription", count, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("can't subscribe:", err)
|
t.Fatal("can't subscribe:", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user