Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ctx to Recv #259

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2954,6 +2954,7 @@ import (
)

func main() {
ctx := context.Background()
client, err := ws.Connect(context.Background(), rpc.MainNetBeta_WS)
if err != nil {
panic(err)
Expand All @@ -2971,7 +2972,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand All @@ -2991,7 +2992,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand All @@ -3016,6 +3017,7 @@ import (
)

func main() {
ctx := context.Background()
client, err := ws.Connect(context.Background(), rpc.MainNetBeta_WS)
if err != nil {
panic(err)
Expand All @@ -3034,7 +3036,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand All @@ -3053,7 +3055,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand All @@ -3078,6 +3080,7 @@ import (
)

func main() {
ctx := context.Background()
client, err := ws.Connect(context.Background(), rpc.MainNetBeta_WS)
if err != nil {
panic(err)
Expand All @@ -3096,7 +3099,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -3130,6 +3133,7 @@ import (
)

func main() {
ctx := context.Background()
client, err := ws.Connect(context.Background(), rpc.TestNet_WS)
if err != nil {
panic(err)
Expand All @@ -3141,7 +3145,7 @@ func main() {
}

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand All @@ -3165,6 +3169,7 @@ import (
)

func main() {
ctx := context.Background()
client, err := ws.Connect(context.Background(), rpc.TestNet_WS)
if err != nil {
panic(err)
Expand All @@ -3182,7 +3187,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand All @@ -3205,6 +3210,7 @@ import (
)

func main() {
ctx := context.Background()
client, err := ws.Connect(context.Background(), rpc.TestNet_WS)
if err != nil {
panic(err)
Expand All @@ -3217,7 +3223,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand All @@ -3240,6 +3246,7 @@ import (
)

func main() {
ctx := context.Background()
client, err := ws.Connect(context.Background(), rpc.MainNetBeta_WS)
if err != nil {
panic(err)
Expand All @@ -3254,7 +3261,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions programs/serum/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ func FetchMarket(ctx context.Context, rpcCli *rpc.Client, marketAddr solana.Publ
return meta, nil
}

func StreamOpenOrders(client *ws.Client) error {
func StreamOpenOrders(ctx context.Context, client *ws.Client) error {
sub, err := client.ProgramSubscribe(DEXProgramIDV2, rpc.CommitmentSingleGossip)
if err != nil {
return fmt.Errorf("unable to subscribe to programID %q: %w", DEXProgramIDV2, err)
}
count := 0
for {
d, err := sub.Recv()
d, err := sub.Recv(ctx)
if err != nil {
return fmt.Errorf("received error from programID subscription: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion programs/serum/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ func TestStreamOpenOrders(t *testing.T) {
client, err := ws.Connect(context.Background(), rpcURL)
require.NoError(t, err)

err = StreamOpenOrders(client)
err = StreamOpenOrders(context.Background(), client)
require.NoError(t, err)
}
6 changes: 5 additions & 1 deletion rpc/ws/accountSubscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package ws

import (
"context"

"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
)
Expand Down Expand Up @@ -83,8 +85,10 @@ type AccountSubscription struct {
sub *Subscription
}

func (sw *AccountSubscription) Recv() (*AccountResult, error) {
func (sw *AccountSubscription) Recv(ctx context.Context) (*AccountResult, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case d, ok := <-sw.sub.stream:
if !ok {
return nil, ErrSubscriptionClosed
Expand Down
5 changes: 4 additions & 1 deletion rpc/ws/blockSubscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package ws

import (
"context"
"fmt"

"github.com/gagliardetto/solana-go"
Expand Down Expand Up @@ -148,8 +149,10 @@ type BlockSubscription struct {
sub *Subscription
}

func (sw *BlockSubscription) Recv() (*BlockResult, error) {
func (sw *BlockSubscription) Recv(ctx context.Context) (*BlockResult, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case d, ok := <-sw.sub.stream:
if !ok {
return nil, ErrSubscriptionClosed
Expand Down
8 changes: 4 additions & 4 deletions rpc/ws/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Test_AccountSubscribe(t *testing.T) {
sub, err := c.AccountSubscribe(accountID, "")
require.NoError(t, err)

data, err := sub.Recv()
data, err := sub.Recv(context.Background())
if err != nil {
fmt.Println("receive an error: ", err)
return
Expand Down Expand Up @@ -95,7 +95,7 @@ func Test_AccountSubscribeWithHttpHeader(t *testing.T) {
sub.Unsubscribe()
}(sub)

data, err := sub.Recv()
data, err := sub.Recv(context.Background())
if err != nil {
t.Errorf("Received an error: %v", err)
}
Expand Down Expand Up @@ -127,7 +127,7 @@ func Test_ProgramSubscribe(t *testing.T) {
require.NoError(t, err)

for {
data, err := sub.Recv()
data, err := sub.Recv(context.Background())
if err != nil {
fmt.Println("receive an error: ", err)
return
Expand All @@ -148,7 +148,7 @@ func Test_SlotSubscribe(t *testing.T) {
sub, err := c.SlotSubscribe()
require.NoError(t, err)

data, err := sub.Recv()
data, err := sub.Recv(context.Background())
if err != nil {
fmt.Println("receive an error: ", err)
return
Expand Down
4 changes: 2 additions & 2 deletions rpc/ws/examples/accountSubscribe/accountSubscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(context.Background())
if err != nil {
panic(err)
}
Expand All @@ -62,7 +62,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(context.Background())
if err != nil {
panic(err)
}
Expand Down
5 changes: 3 additions & 2 deletions rpc/ws/examples/logsSubscribe/logsSubscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
)

func main() {
ctx := context.Background()
client, err := ws.Connect(context.Background(), rpc.MainNetBeta_WS)
if err != nil {
panic(err)
Expand All @@ -43,7 +44,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand All @@ -62,7 +63,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion rpc/ws/examples/programSubscribe/programSubscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
)

func main() {
ctx := context.Background()
client, err := ws.Connect(context.Background(), rpc.MainNetBeta_WS)
if err != nil {
panic(err)
Expand All @@ -43,7 +44,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion rpc/ws/examples/rootSubscribe/rootSubscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

func main() {
ctx := context.Background()
client, err := ws.Connect(context.Background(), rpc.TestNet_WS)
if err != nil {
panic(err)
Expand All @@ -35,7 +36,7 @@ func main() {
}

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion rpc/ws/examples/signatureSubscribe/signatureSubscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
)

func main() {
ctx := context.Background()
client, err := ws.Connect(context.Background(), rpc.TestNet_WS)
if err != nil {
panic(err)
Expand All @@ -42,7 +43,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion rpc/ws/examples/slotSubscribe/slotSubscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

func main() {
ctx := context.Background()
client, err := ws.Connect(context.Background(), rpc.TestNet_WS)
if err != nil {
panic(err)
Expand All @@ -36,7 +37,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion rpc/ws/examples/voteSubscribe/voteSubscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

func main() {
ctx := context.Background()
client, err := ws.Connect(context.Background(), rpc.MainNetBeta_WS)
if err != nil {
panic(err)
Expand All @@ -38,7 +39,7 @@ func main() {
defer sub.Unsubscribe()

for {
got, err := sub.Recv()
got, err := sub.Recv(ctx)
if err != nil {
panic(err)
}
Expand Down
6 changes: 5 additions & 1 deletion rpc/ws/logsSubscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package ws

import (
"context"

"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
)
Expand Down Expand Up @@ -107,8 +109,10 @@ type LogSubscription struct {
sub *Subscription
}

func (sw *LogSubscription) Recv() (*LogResult, error) {
func (sw *LogSubscription) Recv(ctx context.Context) (*LogResult, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case d, ok := <-sw.sub.stream:
if !ok {
return nil, ErrSubscriptionClosed
Expand Down
Loading
Loading