2022-07-21 19:39:53 +00:00
|
|
|
package rpcclient
|
2020-04-29 19:51:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-05-23 10:47:52 +00:00
|
|
|
"fmt"
|
2022-02-18 17:28:13 +00:00
|
|
|
"strconv"
|
2022-02-21 13:40:50 +00:00
|
|
|
"sync"
|
2023-10-11 10:24:16 +00:00
|
|
|
"sync/atomic"
|
2020-04-29 19:51:43 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2020-05-12 08:18:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-09-03 16:58:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-05-12 08:18:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2022-07-22 16:09:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc"
|
2022-07-22 18:26:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2022-10-17 10:31:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/rpcevent"
|
2020-04-29 19:51:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// WSClient is a websocket-enabled RPC client that can be used with appropriate
|
|
|
|
// servers. It's supposed to be faster than Client because it has persistent
|
2022-04-20 18:30:09 +00:00
|
|
|
// connection to the server and at the same time it exposes some functionality
|
2020-04-29 19:51:43 +00:00
|
|
|
// that is only provided via websockets (like event subscription mechanism).
|
2022-02-22 13:49:38 +00:00
|
|
|
// WSClient is thread-safe and can be used from multiple goroutines to perform
|
|
|
|
// RPC requests.
|
2023-03-16 18:27:35 +00:00
|
|
|
//
|
|
|
|
// It exposes a set of Receive* methods with the same behaviour pattern that
|
|
|
|
// is caused by the fact that the client itself receives every message from the
|
|
|
|
// server via a single channel. This includes any subscriptions and any replies
|
|
|
|
// to ordinary requests at the same. The client then routes these messages to
|
|
|
|
// channels provided on subscription (passed to Receive*) or to the respective
|
|
|
|
// receivers (API callers) if it's an ordinary JSON-RPC reply. While synchronous
|
|
|
|
// API users are blocked during their calls and wake up on reply, subscription
|
|
|
|
// channels must be read from to avoid blocking the client. Failure to do so
|
|
|
|
// will make WSClient wait for the channel reader to get the event and while
|
|
|
|
// it waits every other messages (subscription-related or request replies)
|
|
|
|
// will be blocked. This also means that subscription channel must be properly
|
2023-04-21 14:14:47 +00:00
|
|
|
// drained after unsubscription. If CloseNotificationChannelIfFull option is on
|
|
|
|
// then the receiver channel will be closed immediately in case if a subsequent
|
|
|
|
// notification can't be sent to it, which means WSClient's operations are
|
|
|
|
// unblocking in this mode. No unsubscription is performed in this case, so it's
|
|
|
|
// still the user responsibility to unsubscribe.
|
2023-03-16 18:27:35 +00:00
|
|
|
//
|
2023-11-29 10:53:45 +00:00
|
|
|
// All Receive* methods provide notifications ordering and persistence guarantees.
|
|
|
|
// See https://github.com/nspcc-dev/neo-go/blob/master/docs/notifications.md#ordering-and-persistence-guarantees
|
|
|
|
// for more details on this topic.
|
|
|
|
//
|
2023-03-16 18:27:35 +00:00
|
|
|
// Any received subscription items (blocks/transactions/nofitications) are passed
|
|
|
|
// via pointers for efficiency, but the actual structures MUST NOT be changed, as
|
|
|
|
// it may affect the functionality of other notification receivers. If multiple
|
|
|
|
// subscriptions share the same receiver channel, then matching notification is
|
|
|
|
// only sent once per channel. The receiver channel will be closed by the WSClient
|
|
|
|
// immediately after MissedEvent is received from the server; no unsubscription
|
2023-04-18 16:36:57 +00:00
|
|
|
// is performed in this case, so it's the user responsibility to unsubscribe. It
|
2023-04-21 14:14:47 +00:00
|
|
|
// will also be closed on disconnection from server or on situation when it's
|
|
|
|
// impossible to send a subsequent notification to the subscriber's channel and
|
|
|
|
// CloseNotificationChannelIfFull option is on.
|
2020-04-29 19:51:43 +00:00
|
|
|
type WSClient struct {
|
|
|
|
Client
|
2020-05-12 08:18:44 +00:00
|
|
|
|
2022-04-05 08:28:26 +00:00
|
|
|
ws *websocket.Conn
|
2023-04-25 08:26:36 +00:00
|
|
|
wsOpts WSOptions
|
2023-09-29 08:48:33 +00:00
|
|
|
readerDone chan struct{}
|
|
|
|
writerDone chan struct{}
|
2022-07-22 16:09:29 +00:00
|
|
|
requests chan *neorpc.Request
|
2022-04-05 08:28:26 +00:00
|
|
|
shutdown chan struct{}
|
|
|
|
closeCalled atomic.Bool
|
2022-02-21 13:40:50 +00:00
|
|
|
|
2022-05-23 10:47:52 +00:00
|
|
|
closeErrLock sync.RWMutex
|
|
|
|
closeErr error
|
|
|
|
|
2022-02-21 13:40:50 +00:00
|
|
|
subscriptionsLock sync.RWMutex
|
2022-10-17 10:31:24 +00:00
|
|
|
subscriptions map[string]notificationReceiver
|
2022-10-25 12:11:24 +00:00
|
|
|
// receivers is a mapping from receiver channel to a set of corresponding subscription IDs.
|
|
|
|
// It must be accessed with subscriptionsLock taken. Its keys must be used to deliver
|
|
|
|
// notifications, if channel is not in the receivers list and corresponding subscription
|
|
|
|
// still exists, notification must not be sent.
|
2023-04-03 10:34:24 +00:00
|
|
|
receivers map[any][]string
|
2022-02-18 17:28:13 +00:00
|
|
|
|
|
|
|
respLock sync.RWMutex
|
2022-07-22 16:09:29 +00:00
|
|
|
respChannels map[uint64]chan *neorpc.Response
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 08:26:36 +00:00
|
|
|
// WSOptions defines options for the web-socket RPC client. It contains a
|
|
|
|
// set of options for the underlying standard RPC client as far as
|
|
|
|
// WSClient-specific options. See Options documentation for more details.
|
|
|
|
type WSOptions struct {
|
|
|
|
Options
|
2023-04-21 14:14:47 +00:00
|
|
|
// CloseNotificationChannelIfFull allows WSClient to close a subscriber's
|
|
|
|
// receive channel in case if the channel isn't read properly and no more
|
|
|
|
// events can be pushed to it. This option, if set, allows to avoid WSClient
|
|
|
|
// blocking on a subsequent notification dispatch. However, if enabled, the
|
|
|
|
// corresponding subscription is kept even after receiver's channel closing,
|
|
|
|
// thus it's still the caller's duty to call Unsubscribe() for this
|
|
|
|
// subscription.
|
|
|
|
CloseNotificationChannelIfFull bool
|
2023-04-25 08:26:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// notificationReceiver is an interface aimed to provide WS subscriber functionality
|
|
|
|
// for different types of subscriptions.
|
|
|
|
type notificationReceiver interface {
|
|
|
|
// Comparator provides notification filtering functionality.
|
|
|
|
rpcevent.Comparator
|
|
|
|
// Receiver returns notification receiver channel.
|
2023-04-03 10:34:24 +00:00
|
|
|
Receiver() any
|
2022-10-25 12:11:24 +00:00
|
|
|
// TrySend checks whether notification passes receiver filter and sends it
|
2023-04-21 14:14:47 +00:00
|
|
|
// to the underlying channel if so. It is performed under subscriptions lock
|
|
|
|
// taken. nonBlocking denotes whether the receiving operation shouldn't block
|
|
|
|
// the client's operation. It returns whether notification matches the filter
|
|
|
|
// and whether the receiver channel is overflown.
|
|
|
|
TrySend(ntf Notification, nonBlocking bool) (bool, bool)
|
2022-10-25 12:11:24 +00:00
|
|
|
// Close closes underlying receiver channel.
|
|
|
|
Close()
|
2022-10-17 10:31:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// blockReceiver stores information about block events subscriber.
|
|
|
|
type blockReceiver struct {
|
|
|
|
filter *neorpc.BlockFilter
|
|
|
|
ch chan<- *block.Block
|
2022-10-17 10:31:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// EventID implements neorpc.Comparator interface.
|
|
|
|
func (r *blockReceiver) EventID() neorpc.EventID {
|
|
|
|
return neorpc.BlockEventID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter implements neorpc.Comparator interface.
|
2023-12-25 11:14:44 +00:00
|
|
|
func (r *blockReceiver) Filter() neorpc.SubscriptionFilter {
|
2022-10-25 12:11:24 +00:00
|
|
|
if r.filter == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return *r.filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receiver implements notificationReceiver interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (r *blockReceiver) Receiver() any {
|
2022-10-25 12:11:24 +00:00
|
|
|
return r.ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrySend implements notificationReceiver interface.
|
2023-04-21 14:14:47 +00:00
|
|
|
func (r *blockReceiver) TrySend(ntf Notification, nonBlocking bool) (bool, bool) {
|
2022-10-25 12:11:24 +00:00
|
|
|
if rpcevent.Matches(r, ntf) {
|
2023-04-21 14:14:47 +00:00
|
|
|
if nonBlocking {
|
|
|
|
select {
|
|
|
|
case r.ch <- ntf.Value.(*block.Block):
|
|
|
|
default:
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r.ch <- ntf.Value.(*block.Block)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, false
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
2023-04-21 14:14:47 +00:00
|
|
|
return false, false
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements notificationReceiver interface.
|
|
|
|
func (r *blockReceiver) Close() {
|
|
|
|
close(r.ch)
|
|
|
|
}
|
|
|
|
|
2023-12-22 08:23:41 +00:00
|
|
|
// headerOfAddedBlockReceiver stores information about header of added block events subscriber.
|
|
|
|
type headerOfAddedBlockReceiver struct {
|
|
|
|
filter *neorpc.BlockFilter
|
|
|
|
ch chan<- *block.Header
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventID implements neorpc.Comparator interface.
|
|
|
|
func (r *headerOfAddedBlockReceiver) EventID() neorpc.EventID {
|
|
|
|
return neorpc.HeaderOfAddedBlockEventID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter implements neorpc.Comparator interface.
|
2023-12-25 11:14:44 +00:00
|
|
|
func (r *headerOfAddedBlockReceiver) Filter() neorpc.SubscriptionFilter {
|
2023-12-22 08:23:41 +00:00
|
|
|
if r.filter == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return *r.filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receiver implements notificationReceiver interface.
|
|
|
|
func (r *headerOfAddedBlockReceiver) Receiver() any {
|
|
|
|
return r.ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrySend implements notificationReceiver interface.
|
|
|
|
func (r *headerOfAddedBlockReceiver) TrySend(ntf Notification, nonBlocking bool) (bool, bool) {
|
|
|
|
if rpcevent.Matches(r, ntf) {
|
|
|
|
if nonBlocking {
|
|
|
|
select {
|
|
|
|
case r.ch <- ntf.Value.(*block.Header):
|
|
|
|
default:
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r.ch <- ntf.Value.(*block.Header)
|
|
|
|
}
|
|
|
|
return true, false
|
|
|
|
}
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements notificationReceiver interface.
|
|
|
|
func (r *headerOfAddedBlockReceiver) Close() {
|
|
|
|
close(r.ch)
|
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// txReceiver stores information about transaction events subscriber.
|
|
|
|
type txReceiver struct {
|
|
|
|
filter *neorpc.TxFilter
|
|
|
|
ch chan<- *transaction.Transaction
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventID implements neorpc.Comparator interface.
|
|
|
|
func (r *txReceiver) EventID() neorpc.EventID {
|
|
|
|
return neorpc.TransactionEventID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter implements neorpc.Comparator interface.
|
2023-12-25 11:14:44 +00:00
|
|
|
func (r *txReceiver) Filter() neorpc.SubscriptionFilter {
|
2022-10-25 12:11:24 +00:00
|
|
|
if r.filter == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return *r.filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receiver implements notificationReceiver interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (r *txReceiver) Receiver() any {
|
2022-10-25 12:11:24 +00:00
|
|
|
return r.ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrySend implements notificationReceiver interface.
|
2023-04-21 14:14:47 +00:00
|
|
|
func (r *txReceiver) TrySend(ntf Notification, nonBlocking bool) (bool, bool) {
|
2022-10-25 12:11:24 +00:00
|
|
|
if rpcevent.Matches(r, ntf) {
|
2023-04-21 14:14:47 +00:00
|
|
|
if nonBlocking {
|
|
|
|
select {
|
|
|
|
case r.ch <- ntf.Value.(*transaction.Transaction):
|
|
|
|
default:
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r.ch <- ntf.Value.(*transaction.Transaction)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, false
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
2023-04-21 14:14:47 +00:00
|
|
|
return false, false
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements notificationReceiver interface.
|
|
|
|
func (r *txReceiver) Close() {
|
|
|
|
close(r.ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// executionNotificationReceiver stores information about execution notifications subscriber.
|
|
|
|
type executionNotificationReceiver struct {
|
|
|
|
filter *neorpc.NotificationFilter
|
|
|
|
ch chan<- *state.ContainedNotificationEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventID implements neorpc.Comparator interface.
|
|
|
|
func (r *executionNotificationReceiver) EventID() neorpc.EventID {
|
|
|
|
return neorpc.NotificationEventID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter implements neorpc.Comparator interface.
|
2023-12-25 11:14:44 +00:00
|
|
|
func (r *executionNotificationReceiver) Filter() neorpc.SubscriptionFilter {
|
2022-10-25 12:11:24 +00:00
|
|
|
if r.filter == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return *r.filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receiver implements notificationReceiver interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (r *executionNotificationReceiver) Receiver() any {
|
2022-10-25 12:11:24 +00:00
|
|
|
return r.ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrySend implements notificationReceiver interface.
|
2023-04-21 14:14:47 +00:00
|
|
|
func (r *executionNotificationReceiver) TrySend(ntf Notification, nonBlocking bool) (bool, bool) {
|
2022-10-25 12:11:24 +00:00
|
|
|
if rpcevent.Matches(r, ntf) {
|
2023-04-21 14:14:47 +00:00
|
|
|
if nonBlocking {
|
|
|
|
select {
|
|
|
|
case r.ch <- ntf.Value.(*state.ContainedNotificationEvent):
|
|
|
|
default:
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r.ch <- ntf.Value.(*state.ContainedNotificationEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, false
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
2023-04-21 14:14:47 +00:00
|
|
|
return false, false
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements notificationReceiver interface.
|
|
|
|
func (r *executionNotificationReceiver) Close() {
|
|
|
|
close(r.ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// executionReceiver stores information about application execution results subscriber.
|
|
|
|
type executionReceiver struct {
|
|
|
|
filter *neorpc.ExecutionFilter
|
|
|
|
ch chan<- *state.AppExecResult
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventID implements neorpc.Comparator interface.
|
|
|
|
func (r *executionReceiver) EventID() neorpc.EventID {
|
|
|
|
return neorpc.ExecutionEventID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter implements neorpc.Comparator interface.
|
2023-12-25 11:14:44 +00:00
|
|
|
func (r *executionReceiver) Filter() neorpc.SubscriptionFilter {
|
2022-10-25 12:11:24 +00:00
|
|
|
if r.filter == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return *r.filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receiver implements notificationReceiver interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (r *executionReceiver) Receiver() any {
|
2022-10-25 12:11:24 +00:00
|
|
|
return r.ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrySend implements notificationReceiver interface.
|
2023-04-21 14:14:47 +00:00
|
|
|
func (r *executionReceiver) TrySend(ntf Notification, nonBlocking bool) (bool, bool) {
|
2022-10-25 12:11:24 +00:00
|
|
|
if rpcevent.Matches(r, ntf) {
|
2023-04-21 14:14:47 +00:00
|
|
|
if nonBlocking {
|
|
|
|
select {
|
|
|
|
case r.ch <- ntf.Value.(*state.AppExecResult):
|
|
|
|
default:
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r.ch <- ntf.Value.(*state.AppExecResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, false
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
2023-04-21 14:14:47 +00:00
|
|
|
return false, false
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements notificationReceiver interface.
|
|
|
|
func (r *executionReceiver) Close() {
|
|
|
|
close(r.ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// notaryRequestReceiver stores information about notary requests subscriber.
|
|
|
|
type notaryRequestReceiver struct {
|
2023-12-09 08:37:25 +00:00
|
|
|
filter *neorpc.NotaryRequestFilter
|
2022-10-25 12:11:24 +00:00
|
|
|
ch chan<- *result.NotaryRequestEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventID implements neorpc.Comparator interface.
|
|
|
|
func (r *notaryRequestReceiver) EventID() neorpc.EventID {
|
|
|
|
return neorpc.NotaryRequestEventID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter implements neorpc.Comparator interface.
|
2023-12-25 11:14:44 +00:00
|
|
|
func (r *notaryRequestReceiver) Filter() neorpc.SubscriptionFilter {
|
2022-10-25 12:11:24 +00:00
|
|
|
if r.filter == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return *r.filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receiver implements notificationReceiver interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (r *notaryRequestReceiver) Receiver() any {
|
2022-10-25 12:11:24 +00:00
|
|
|
return r.ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrySend implements notificationReceiver interface.
|
2023-04-21 14:14:47 +00:00
|
|
|
func (r *notaryRequestReceiver) TrySend(ntf Notification, nonBlocking bool) (bool, bool) {
|
2022-10-25 12:11:24 +00:00
|
|
|
if rpcevent.Matches(r, ntf) {
|
2023-04-21 14:14:47 +00:00
|
|
|
if nonBlocking {
|
|
|
|
select {
|
|
|
|
case r.ch <- ntf.Value.(*result.NotaryRequestEvent):
|
|
|
|
default:
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r.ch <- ntf.Value.(*result.NotaryRequestEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, false
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
2023-04-21 14:14:47 +00:00
|
|
|
return false, false
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements notificationReceiver interface.
|
|
|
|
func (r *notaryRequestReceiver) Close() {
|
|
|
|
close(r.ch)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Notification represents a server-generated notification for client subscriptions.
|
2022-10-17 06:12:33 +00:00
|
|
|
// Value can be one of *block.Block, *state.AppExecResult, *state.ContainedNotificationEvent
|
|
|
|
// *transaction.Transaction or *subscriptions.NotaryRequestEvent based on Type.
|
2020-05-12 08:18:44 +00:00
|
|
|
type Notification struct {
|
2022-07-22 16:09:29 +00:00
|
|
|
Type neorpc.EventID
|
2023-04-03 10:34:24 +00:00
|
|
|
Value any
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 10:31:24 +00:00
|
|
|
// EventID implements Container interface and returns notification ID.
|
|
|
|
func (n Notification) EventID() neorpc.EventID {
|
|
|
|
return n.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventPayload implements Container interface and returns notification
|
|
|
|
// object.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (n Notification) EventPayload() any {
|
2022-10-17 10:31:24 +00:00
|
|
|
return n.Value
|
|
|
|
}
|
|
|
|
|
2020-04-29 19:51:43 +00:00
|
|
|
// requestResponse is a combined type for request and response since we can get
|
|
|
|
// any of them here.
|
|
|
|
type requestResponse struct {
|
2022-07-22 16:09:29 +00:00
|
|
|
neorpc.Response
|
2022-07-07 14:41:01 +00:00
|
|
|
Method string `json:"method"`
|
|
|
|
RawParams []json.RawMessage `json:"params,omitempty"`
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Message limit for receiving side.
|
|
|
|
wsReadLimit = 10 * 1024 * 1024
|
|
|
|
|
|
|
|
// Disconnection timeout.
|
|
|
|
wsPongLimit = 60 * time.Second
|
|
|
|
|
|
|
|
// Ping period for connection liveness check.
|
|
|
|
wsPingPeriod = wsPongLimit / 2
|
|
|
|
|
|
|
|
// Write deadline.
|
|
|
|
wsWriteLimit = wsPingPeriod / 2
|
|
|
|
)
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// ErrNilNotificationReceiver is returned when notification receiver channel is nil.
|
|
|
|
var ErrNilNotificationReceiver = errors.New("nil notification receiver")
|
|
|
|
|
2023-05-03 13:01:46 +00:00
|
|
|
// ErrWSConnLost is a WSClient-specific error that will be returned for any
|
|
|
|
// requests after disconnection (including intentional ones via
|
|
|
|
// (*WSClient).Close).
|
|
|
|
var ErrWSConnLost = errors.New("connection lost")
|
|
|
|
|
2022-06-10 14:20:45 +00:00
|
|
|
// errConnClosedByUser is a WSClient error used iff the user calls (*WSClient).Close method by himself.
|
|
|
|
var errConnClosedByUser = errors.New("connection closed by user")
|
|
|
|
|
2020-04-29 19:51:43 +00:00
|
|
|
// NewWS returns a new WSClient ready to use (with established websocket
|
|
|
|
// connection). You need to use websocket URL for it like `ws://1.2.3.4/ws`.
|
2022-04-20 18:30:09 +00:00
|
|
|
// You should call Init method to initialize the network magic the client is
|
2020-10-14 15:13:20 +00:00
|
|
|
// operating on.
|
2023-04-25 08:26:36 +00:00
|
|
|
func NewWS(ctx context.Context, endpoint string, opts WSOptions) (*WSClient, error) {
|
2020-04-29 19:51:43 +00:00
|
|
|
dialer := websocket.Dialer{HandshakeTimeout: opts.DialTimeout}
|
2022-09-02 09:25:51 +00:00
|
|
|
ws, resp, err := dialer.DialContext(ctx, endpoint, nil)
|
2022-09-02 09:21:24 +00:00
|
|
|
if resp != nil && resp.Body != nil { // Can be non-nil even with error returned.
|
|
|
|
defer resp.Body.Close() // Not exactly required by websocket, but let's do this for bodyclose checker.
|
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
if err != nil {
|
2022-11-23 08:25:05 +00:00
|
|
|
if resp != nil && resp.Body != nil {
|
|
|
|
var srvErr neorpc.HeaderAndError
|
|
|
|
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
decErr := dec.Decode(&srvErr)
|
|
|
|
if decErr == nil && srvErr.Error != nil {
|
|
|
|
err = srvErr.Error
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wsc := &WSClient{
|
2023-10-09 19:43:49 +00:00
|
|
|
Client: Client{},
|
2020-05-12 08:18:44 +00:00
|
|
|
|
|
|
|
ws: ws,
|
2023-04-25 08:26:36 +00:00
|
|
|
wsOpts: opts,
|
2020-05-12 08:18:44 +00:00
|
|
|
shutdown: make(chan struct{}),
|
2023-09-29 08:48:33 +00:00
|
|
|
readerDone: make(chan struct{}),
|
|
|
|
writerDone: make(chan struct{}),
|
2022-07-22 16:09:29 +00:00
|
|
|
respChannels: make(map[uint64]chan *neorpc.Response),
|
|
|
|
requests: make(chan *neorpc.Request),
|
2022-10-17 10:31:24 +00:00
|
|
|
subscriptions: make(map[string]notificationReceiver),
|
2023-04-03 10:34:24 +00:00
|
|
|
receivers: make(map[any][]string),
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
2022-02-21 15:30:07 +00:00
|
|
|
|
2023-04-25 08:26:36 +00:00
|
|
|
err = initClient(ctx, &wsc.Client, endpoint, opts.Options)
|
2022-02-21 15:30:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wsc.Client.cli = nil
|
|
|
|
|
2020-04-29 19:51:43 +00:00
|
|
|
go wsc.wsReader()
|
|
|
|
go wsc.wsWriter()
|
|
|
|
wsc.requestF = wsc.makeWsRequest
|
|
|
|
return wsc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes connection to the remote side rendering this client instance
|
|
|
|
// unusable.
|
|
|
|
func (c *WSClient) Close() {
|
2023-04-27 15:49:19 +00:00
|
|
|
if c.closeCalled.CompareAndSwap(false, true) {
|
2022-06-10 14:20:45 +00:00
|
|
|
c.setCloseErr(errConnClosedByUser)
|
2022-04-20 18:30:09 +00:00
|
|
|
// Closing shutdown channel sends a signal to wsWriter to break out of the
|
2022-04-05 08:28:26 +00:00
|
|
|
// loop. In doing so it does ws.Close() closing the network connection
|
2022-04-20 18:30:09 +00:00
|
|
|
// which in turn makes wsReader receive an err from ws.ReadJSON() and also
|
2022-04-05 08:28:26 +00:00
|
|
|
// break out of the loop closing c.done channel in its shutdown sequence.
|
|
|
|
close(c.shutdown)
|
2022-10-12 12:23:32 +00:00
|
|
|
// Call to cancel will send signal to all users of Context().
|
|
|
|
c.Client.ctxCancel()
|
2022-04-05 08:28:26 +00:00
|
|
|
}
|
2023-09-29 08:48:33 +00:00
|
|
|
<-c.readerDone
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *WSClient) wsReader() {
|
|
|
|
c.ws.SetReadLimit(wsReadLimit)
|
2022-05-23 10:47:52 +00:00
|
|
|
c.ws.SetPongHandler(func(string) error {
|
|
|
|
err := c.ws.SetReadDeadline(time.Now().Add(wsPongLimit))
|
|
|
|
if err != nil {
|
|
|
|
c.setCloseErr(fmt.Errorf("failed to set pong read deadline: %w", err))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
var connCloseErr error
|
2020-05-12 08:18:44 +00:00
|
|
|
readloop:
|
2020-04-29 19:51:43 +00:00
|
|
|
for {
|
|
|
|
rr := new(requestResponse)
|
2021-05-12 18:45:32 +00:00
|
|
|
err := c.ws.SetReadDeadline(time.Now().Add(wsPongLimit))
|
|
|
|
if err != nil {
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to set response read deadline: %w", err)
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop
|
2021-05-12 18:45:32 +00:00
|
|
|
}
|
|
|
|
err = c.ws.ReadJSON(rr)
|
2020-04-29 19:51:43 +00:00
|
|
|
if err != nil {
|
|
|
|
// Timeout/connection loss/malformed response.
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to read JSON response (timeout/connection loss/malformed response): %w", err)
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
2022-07-07 14:41:01 +00:00
|
|
|
if rr.ID == nil && rr.Method != "" {
|
2022-07-22 16:09:29 +00:00
|
|
|
event, err := neorpc.GetEventIDFromString(rr.Method)
|
2020-05-12 08:18:44 +00:00
|
|
|
if err != nil {
|
|
|
|
// Bad event received.
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to perse event ID from string %s: %w", rr.Method, err)
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
2022-07-22 16:09:29 +00:00
|
|
|
if event != neorpc.MissedEventID && len(rr.RawParams) != 1 {
|
2020-05-12 08:18:44 +00:00
|
|
|
// Bad event received.
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("bad event received: %s / %d", event, len(rr.RawParams))
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
2023-02-15 07:09:15 +00:00
|
|
|
ntf := Notification{Type: event}
|
2020-05-12 08:18:44 +00:00
|
|
|
switch event {
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.BlockEventID:
|
2023-10-09 19:34:33 +00:00
|
|
|
sr, err := c.stateRootInHeader()
|
2022-02-21 10:41:14 +00:00
|
|
|
if err != nil {
|
2022-04-20 18:30:09 +00:00
|
|
|
// Client is not initialized.
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to fetch StateRootInHeader: %w", err)
|
|
|
|
break readloop
|
2022-02-21 10:41:14 +00:00
|
|
|
}
|
2023-02-15 07:09:15 +00:00
|
|
|
ntf.Value = block.New(sr)
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.TransactionEventID:
|
2023-02-15 07:09:15 +00:00
|
|
|
ntf.Value = &transaction.Transaction{}
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.NotificationEventID:
|
2023-02-15 07:09:15 +00:00
|
|
|
ntf.Value = new(state.ContainedNotificationEvent)
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.ExecutionEventID:
|
2023-02-15 07:09:15 +00:00
|
|
|
ntf.Value = new(state.AppExecResult)
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.NotaryRequestEventID:
|
2023-02-15 07:09:15 +00:00
|
|
|
ntf.Value = new(result.NotaryRequestEvent)
|
2023-12-22 08:23:41 +00:00
|
|
|
case neorpc.HeaderOfAddedBlockEventID:
|
|
|
|
sr, err := c.stateRootInHeader()
|
|
|
|
if err != nil {
|
|
|
|
// Client is not initialized.
|
|
|
|
connCloseErr = fmt.Errorf("failed to fetch StateRootInHeader: %w", err)
|
|
|
|
break readloop
|
|
|
|
}
|
|
|
|
ntf.Value = &block.New(sr).Header
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.MissedEventID:
|
2020-05-12 19:38:29 +00:00
|
|
|
// No value.
|
2020-05-12 08:18:44 +00:00
|
|
|
default:
|
|
|
|
// Bad event received.
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("unknown event received: %d", event)
|
2020-05-12 08:18:44 +00:00
|
|
|
break readloop
|
|
|
|
}
|
2022-07-22 16:09:29 +00:00
|
|
|
if event != neorpc.MissedEventID {
|
2023-02-15 07:09:15 +00:00
|
|
|
err = json.Unmarshal(rr.RawParams[0], ntf.Value)
|
2020-05-12 19:38:29 +00:00
|
|
|
if err != nil {
|
|
|
|
// Bad event received.
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to unmarshal event of type %s from JSON: %w", event, err)
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop
|
2020-05-12 19:38:29 +00:00
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
2023-02-15 07:09:15 +00:00
|
|
|
c.notifySubscribers(ntf)
|
2022-07-07 14:41:01 +00:00
|
|
|
} else if rr.ID != nil && (rr.Error != nil || rr.Result != nil) {
|
2022-07-07 14:45:33 +00:00
|
|
|
id, err := strconv.ParseUint(string(rr.ID), 10, 64)
|
2022-02-18 17:28:13 +00:00
|
|
|
if err != nil {
|
2022-07-07 14:41:01 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to retrieve response ID from string %s: %w", string(rr.ID), err)
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop // Malformed response (invalid response ID).
|
2022-02-18 17:28:13 +00:00
|
|
|
}
|
2022-07-07 14:45:33 +00:00
|
|
|
ch := c.getResponseChannel(id)
|
2022-02-18 17:28:13 +00:00
|
|
|
if ch == nil {
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("unknown response channel for response %d", id)
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop // Unknown response (unexpected response ID).
|
2022-02-18 17:28:13 +00:00
|
|
|
}
|
2024-07-29 14:39:11 +00:00
|
|
|
select {
|
|
|
|
case <-c.writerDone:
|
|
|
|
break readloop
|
|
|
|
case <-c.shutdown:
|
|
|
|
break readloop
|
|
|
|
case ch <- &rr.Response:
|
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
} else {
|
|
|
|
// Malformed response, neither valid request, nor valid response.
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("malformed response")
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-23 10:47:52 +00:00
|
|
|
if connCloseErr != nil {
|
|
|
|
c.setCloseErr(connCloseErr)
|
|
|
|
}
|
2023-09-29 08:48:33 +00:00
|
|
|
close(c.readerDone)
|
2022-02-18 17:28:13 +00:00
|
|
|
c.respLock.Lock()
|
|
|
|
for _, ch := range c.respChannels {
|
|
|
|
close(ch)
|
|
|
|
}
|
|
|
|
c.respChannels = nil
|
|
|
|
c.respLock.Unlock()
|
2023-04-18 16:36:57 +00:00
|
|
|
c.subscriptionsLock.Lock()
|
|
|
|
for rcvrCh, ids := range c.receivers {
|
2023-04-21 14:14:47 +00:00
|
|
|
c.dropSubCh(rcvrCh, ids[0], true)
|
|
|
|
}
|
|
|
|
c.subscriptionsLock.Unlock()
|
|
|
|
c.Client.ctxCancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
// dropSubCh closes corresponding subscriber's channel and removes it from the
|
2023-10-09 19:43:49 +00:00
|
|
|
// receivers map. The channel is still being kept in
|
2023-04-21 14:14:47 +00:00
|
|
|
// the subscribers map as technically the server-side subscription still exists
|
|
|
|
// and the user is responsible for unsubscription. This method must be called
|
|
|
|
// under subscriptionsLock taken. It's the caller's duty to ensure dropSubCh
|
|
|
|
// will be called once per channel, otherwise panic will occur.
|
|
|
|
func (c *WSClient) dropSubCh(rcvrCh any, id string, ignoreCloseNotificationChannelIfFull bool) {
|
|
|
|
if ignoreCloseNotificationChannelIfFull || c.wsOpts.CloseNotificationChannelIfFull {
|
2023-10-09 19:43:49 +00:00
|
|
|
c.subscriptions[id].Close()
|
2023-04-18 16:36:57 +00:00
|
|
|
delete(c.receivers, rcvrCh)
|
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *WSClient) wsWriter() {
|
|
|
|
pingTicker := time.NewTicker(wsPingPeriod)
|
|
|
|
defer c.ws.Close()
|
|
|
|
defer pingTicker.Stop()
|
2023-09-29 08:48:33 +00:00
|
|
|
defer close(c.writerDone)
|
2022-05-23 10:47:52 +00:00
|
|
|
var connCloseErr error
|
|
|
|
writeloop:
|
2020-04-29 19:51:43 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.shutdown:
|
|
|
|
return
|
2023-09-29 08:48:33 +00:00
|
|
|
case <-c.readerDone:
|
2020-04-29 19:51:43 +00:00
|
|
|
return
|
|
|
|
case req, ok := <-c.requests:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-05-12 18:45:32 +00:00
|
|
|
if err := c.ws.SetWriteDeadline(time.Now().Add(c.opts.RequestTimeout)); err != nil {
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to set request write deadline: %w", err)
|
|
|
|
break writeloop
|
2021-05-12 18:45:32 +00:00
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
if err := c.ws.WriteJSON(req); err != nil {
|
2022-07-07 15:33:23 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to write JSON request (%s / %d): %w", req.Method, len(req.Params), err)
|
2022-05-23 10:47:52 +00:00
|
|
|
break writeloop
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
|
|
|
case <-pingTicker.C:
|
2021-05-12 18:45:32 +00:00
|
|
|
if err := c.ws.SetWriteDeadline(time.Now().Add(wsWriteLimit)); err != nil {
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to set ping write deadline: %w", err)
|
|
|
|
break writeloop
|
2021-05-12 18:45:32 +00:00
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
if err := c.ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to write ping message: %w", err)
|
|
|
|
break writeloop
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-23 10:47:52 +00:00
|
|
|
if connCloseErr != nil {
|
|
|
|
c.setCloseErr(connCloseErr)
|
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 07:09:15 +00:00
|
|
|
func (c *WSClient) notifySubscribers(ntf Notification) {
|
|
|
|
if ntf.Type == neorpc.MissedEventID {
|
|
|
|
c.subscriptionsLock.Lock()
|
|
|
|
for rcvr, ids := range c.receivers {
|
|
|
|
c.subscriptions[ids[0]].Close()
|
|
|
|
delete(c.receivers, rcvr)
|
|
|
|
}
|
|
|
|
c.subscriptionsLock.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2023-04-21 14:14:47 +00:00
|
|
|
c.subscriptionsLock.Lock()
|
|
|
|
for rcvrCh, ids := range c.receivers {
|
2023-02-15 07:09:15 +00:00
|
|
|
for _, id := range ids {
|
2023-04-21 14:14:47 +00:00
|
|
|
ok, dropCh := c.subscriptions[id].TrySend(ntf, c.wsOpts.CloseNotificationChannelIfFull)
|
|
|
|
if dropCh {
|
|
|
|
c.dropSubCh(rcvrCh, id, false)
|
|
|
|
break // strictly single drop per channel
|
|
|
|
}
|
|
|
|
if ok {
|
2023-02-15 07:09:15 +00:00
|
|
|
break // strictly one notification per channel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-21 14:14:47 +00:00
|
|
|
c.subscriptionsLock.Unlock()
|
2023-02-15 07:09:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 17:28:13 +00:00
|
|
|
func (c *WSClient) unregisterRespChannel(id uint64) {
|
|
|
|
c.respLock.Lock()
|
|
|
|
defer c.respLock.Unlock()
|
|
|
|
if ch, ok := c.respChannels[id]; ok {
|
|
|
|
delete(c.respChannels, id)
|
|
|
|
close(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 16:09:29 +00:00
|
|
|
func (c *WSClient) getResponseChannel(id uint64) chan *neorpc.Response {
|
2022-02-18 17:28:13 +00:00
|
|
|
c.respLock.RLock()
|
|
|
|
defer c.respLock.RUnlock()
|
|
|
|
return c.respChannels[id]
|
|
|
|
}
|
|
|
|
|
2023-09-29 08:48:33 +00:00
|
|
|
// closeErrOrConnLost returns the error that may occur either in wsReader or wsWriter.
|
|
|
|
// If wsReader or wsWriter do not set the error, it returns ErrWSConnLost.
|
|
|
|
func (c *WSClient) closeErrOrConnLost() (err error) {
|
|
|
|
err = ErrWSConnLost
|
|
|
|
if closeErr := c.GetError(); closeErr != nil {
|
|
|
|
err = closeErr
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-22 16:09:29 +00:00
|
|
|
func (c *WSClient) makeWsRequest(r *neorpc.Request) (*neorpc.Response, error) {
|
|
|
|
ch := make(chan *neorpc.Response)
|
rpc: avoid panic during request after WS connection is closed
Fix the following panic:
```
panic: assignment to entry in nil map
goroutine 131 [running]:
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*WSClient).registerRespChannel(0xc00033c240, 0x0, 0xc00003e2a0)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/wsclient.go:244 +0x96
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*WSClient).makeWsRequest(0xc00033c240, 0xc002080000)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/wsclient.go:264 +0x69
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).performRequest(0xc00033c240, {0xc9f173, 0xd}, {{0x13d09d0, 0x0, 0x0}}, {0xb44120, 0xc00147a000})
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/client.go:186 +0x15d
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).GetBlockCount(0xc001fb5440)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/rpc.go:73 +0x69
...
```
2022-04-26 14:19:51 +00:00
|
|
|
c.respLock.Lock()
|
|
|
|
select {
|
2023-09-29 08:48:33 +00:00
|
|
|
case <-c.readerDone:
|
rpc: avoid panic during request after WS connection is closed
Fix the following panic:
```
panic: assignment to entry in nil map
goroutine 131 [running]:
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*WSClient).registerRespChannel(0xc00033c240, 0x0, 0xc00003e2a0)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/wsclient.go:244 +0x96
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*WSClient).makeWsRequest(0xc00033c240, 0xc002080000)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/wsclient.go:264 +0x69
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).performRequest(0xc00033c240, {0xc9f173, 0xd}, {{0x13d09d0, 0x0, 0x0}}, {0xb44120, 0xc00147a000})
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/client.go:186 +0x15d
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).GetBlockCount(0xc001fb5440)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/rpc.go:73 +0x69
...
```
2022-04-26 14:19:51 +00:00
|
|
|
c.respLock.Unlock()
|
2023-09-29 08:48:33 +00:00
|
|
|
return nil, fmt.Errorf("%w: before registering response channel", c.closeErrOrConnLost())
|
rpc: avoid panic during request after WS connection is closed
Fix the following panic:
```
panic: assignment to entry in nil map
goroutine 131 [running]:
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*WSClient).registerRespChannel(0xc00033c240, 0x0, 0xc00003e2a0)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/wsclient.go:244 +0x96
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*WSClient).makeWsRequest(0xc00033c240, 0xc002080000)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/wsclient.go:264 +0x69
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).performRequest(0xc00033c240, {0xc9f173, 0xd}, {{0x13d09d0, 0x0, 0x0}}, {0xb44120, 0xc00147a000})
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/client.go:186 +0x15d
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).GetBlockCount(0xc001fb5440)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/rpc.go:73 +0x69
...
```
2022-04-26 14:19:51 +00:00
|
|
|
default:
|
|
|
|
c.respChannels[r.ID] = ch
|
|
|
|
c.respLock.Unlock()
|
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
select {
|
2023-09-29 08:48:33 +00:00
|
|
|
case <-c.readerDone:
|
|
|
|
return nil, fmt.Errorf("%w: before sending the request", c.closeErrOrConnLost())
|
|
|
|
case <-c.writerDone:
|
|
|
|
return nil, fmt.Errorf("%w: before sending the request", c.closeErrOrConnLost())
|
2020-04-29 19:51:43 +00:00
|
|
|
case c.requests <- r:
|
|
|
|
}
|
|
|
|
select {
|
2023-09-29 08:48:33 +00:00
|
|
|
case <-c.readerDone:
|
|
|
|
return nil, fmt.Errorf("%w: while waiting for the response", c.closeErrOrConnLost())
|
|
|
|
case <-c.writerDone:
|
|
|
|
return nil, fmt.Errorf("%w: while waiting for the response", c.closeErrOrConnLost())
|
2023-04-18 16:34:38 +00:00
|
|
|
case resp, ok := <-ch:
|
|
|
|
if !ok {
|
2023-09-29 08:48:33 +00:00
|
|
|
return nil, fmt.Errorf("%w: while waiting for the response", c.closeErrOrConnLost())
|
2023-04-18 16:34:38 +00:00
|
|
|
}
|
2022-02-18 17:28:13 +00:00
|
|
|
c.unregisterRespChannel(r.ID)
|
2020-04-29 19:51:43 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
|
2023-04-03 10:34:24 +00:00
|
|
|
func (c *WSClient) performSubscription(params []any, rcvr notificationReceiver) (string, error) {
|
2020-05-12 08:18:44 +00:00
|
|
|
var resp string
|
|
|
|
|
2023-12-25 11:14:44 +00:00
|
|
|
if flt := rcvr.Filter(); flt != nil {
|
|
|
|
if err := flt.IsValid(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
if err := c.performRequest("subscribe", params, &resp); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-02-21 13:40:50 +00:00
|
|
|
|
|
|
|
c.subscriptionsLock.Lock()
|
|
|
|
defer c.subscriptionsLock.Unlock()
|
|
|
|
|
2022-10-17 10:31:24 +00:00
|
|
|
c.subscriptions[resp] = rcvr
|
2022-10-25 12:11:24 +00:00
|
|
|
ch := rcvr.Receiver()
|
|
|
|
c.receivers[ch] = append(c.receivers[ch], resp)
|
2020-05-12 08:18:44 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// ReceiveBlocks registers provided channel as a receiver for the new block events.
|
|
|
|
// Events can be filtered by the given BlockFilter, nil value doesn't add any filter.
|
2023-03-16 18:27:35 +00:00
|
|
|
// See WSClient comments for generic Receive* behaviour details.
|
2022-10-25 12:11:24 +00:00
|
|
|
func (c *WSClient) ReceiveBlocks(flt *neorpc.BlockFilter, rcvr chan<- *block.Block) (string, error) {
|
|
|
|
if rcvr == nil {
|
|
|
|
return "", ErrNilNotificationReceiver
|
2022-10-19 09:46:53 +00:00
|
|
|
}
|
2023-12-08 18:56:09 +00:00
|
|
|
if !c.cache.initDone {
|
|
|
|
return "", errNetworkNotInitialized
|
|
|
|
}
|
2023-04-03 10:34:24 +00:00
|
|
|
params := []any{"block_added"}
|
2022-10-25 12:11:24 +00:00
|
|
|
if flt != nil {
|
2023-03-16 20:50:56 +00:00
|
|
|
flt = flt.Copy()
|
2022-10-25 12:11:24 +00:00
|
|
|
params = append(params, *flt)
|
2020-05-13 10:16:42 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
r := &blockReceiver{
|
2022-10-17 10:31:24 +00:00
|
|
|
filter: flt,
|
2022-10-25 12:11:24 +00:00
|
|
|
ch: rcvr,
|
2022-10-17 10:31:24 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
return c.performSubscription(params, r)
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 08:23:41 +00:00
|
|
|
// ReceiveHeadersOfAddedBlocks registers provided channel as a receiver for new
|
|
|
|
// block's header events. Events can be filtered by the given [neorpc.BlockFilter],
|
|
|
|
// nil value doesn't add any filter. See WSClient comments for generic
|
|
|
|
// Receive* behaviour details.
|
|
|
|
func (c *WSClient) ReceiveHeadersOfAddedBlocks(flt *neorpc.BlockFilter, rcvr chan<- *block.Header) (string, error) {
|
|
|
|
if rcvr == nil {
|
|
|
|
return "", ErrNilNotificationReceiver
|
|
|
|
}
|
|
|
|
if !c.cache.initDone {
|
|
|
|
return "", errNetworkNotInitialized
|
|
|
|
}
|
|
|
|
params := []any{"header_of_added_block"}
|
|
|
|
if flt != nil {
|
|
|
|
flt = flt.Copy()
|
|
|
|
params = append(params, *flt)
|
|
|
|
}
|
|
|
|
r := &headerOfAddedBlockReceiver{
|
|
|
|
filter: flt,
|
|
|
|
ch: rcvr,
|
|
|
|
}
|
|
|
|
return c.performSubscription(params, r)
|
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// ReceiveTransactions registers provided channel as a receiver for new transaction
|
|
|
|
// events. Events can be filtered by the given TxFilter, nil value doesn't add any
|
2023-03-16 18:27:35 +00:00
|
|
|
// filter. See WSClient comments for generic Receive* behaviour details.
|
2022-10-25 12:11:24 +00:00
|
|
|
func (c *WSClient) ReceiveTransactions(flt *neorpc.TxFilter, rcvr chan<- *transaction.Transaction) (string, error) {
|
|
|
|
if rcvr == nil {
|
|
|
|
return "", ErrNilNotificationReceiver
|
2022-10-19 09:46:53 +00:00
|
|
|
}
|
2023-04-03 10:34:24 +00:00
|
|
|
params := []any{"transaction_added"}
|
2022-10-25 12:11:24 +00:00
|
|
|
if flt != nil {
|
2023-03-16 20:50:56 +00:00
|
|
|
flt = flt.Copy()
|
2022-10-17 10:31:24 +00:00
|
|
|
params = append(params, *flt)
|
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
r := &txReceiver{
|
2022-10-17 10:31:24 +00:00
|
|
|
filter: flt,
|
2022-10-25 12:11:24 +00:00
|
|
|
ch: rcvr,
|
2020-05-13 10:16:42 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
return c.performSubscription(params, r)
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// ReceiveExecutionNotifications registers provided channel as a receiver for execution
|
|
|
|
// events. Events can be filtered by the given NotificationFilter, nil value doesn't add
|
2023-03-16 18:27:35 +00:00
|
|
|
// any filter. See WSClient comments for generic Receive* behaviour details.
|
2022-10-25 12:11:24 +00:00
|
|
|
func (c *WSClient) ReceiveExecutionNotifications(flt *neorpc.NotificationFilter, rcvr chan<- *state.ContainedNotificationEvent) (string, error) {
|
|
|
|
if rcvr == nil {
|
|
|
|
return "", ErrNilNotificationReceiver
|
2022-10-19 09:46:53 +00:00
|
|
|
}
|
2023-04-03 10:34:24 +00:00
|
|
|
params := []any{"notification_from_execution"}
|
2022-10-25 12:11:24 +00:00
|
|
|
if flt != nil {
|
2023-03-16 20:50:56 +00:00
|
|
|
flt = flt.Copy()
|
2022-10-17 10:31:24 +00:00
|
|
|
params = append(params, *flt)
|
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
r := &executionNotificationReceiver{
|
2022-10-17 10:31:24 +00:00
|
|
|
filter: flt,
|
2022-10-25 12:11:24 +00:00
|
|
|
ch: rcvr,
|
2020-05-13 10:16:42 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
return c.performSubscription(params, r)
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// ReceiveExecutions registers provided channel as a receiver for
|
|
|
|
// application execution result events generated during transaction execution.
|
|
|
|
// Events can be filtered by the given ExecutionFilter, nil value doesn't add any filter.
|
2023-03-16 18:27:35 +00:00
|
|
|
// See WSClient comments for generic Receive* behaviour details.
|
2022-10-25 12:11:24 +00:00
|
|
|
func (c *WSClient) ReceiveExecutions(flt *neorpc.ExecutionFilter, rcvr chan<- *state.AppExecResult) (string, error) {
|
|
|
|
if rcvr == nil {
|
|
|
|
return "", ErrNilNotificationReceiver
|
|
|
|
}
|
2023-04-03 10:34:24 +00:00
|
|
|
params := []any{"transaction_executed"}
|
2022-10-25 12:11:24 +00:00
|
|
|
if flt != nil {
|
2023-03-16 20:50:56 +00:00
|
|
|
flt = flt.Copy()
|
2022-10-25 12:11:24 +00:00
|
|
|
params = append(params, *flt)
|
|
|
|
}
|
|
|
|
r := &executionReceiver{
|
2022-10-17 10:31:24 +00:00
|
|
|
filter: flt,
|
2022-10-25 12:11:24 +00:00
|
|
|
ch: rcvr,
|
2020-05-13 10:16:42 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
return c.performSubscription(params, r)
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// ReceiveNotaryRequests registers provided channel as a receiver for notary request
|
2023-12-09 08:37:25 +00:00
|
|
|
// payload addition or removal events. Events can be filtered by the given NotaryRequestFilter
|
2022-10-25 12:11:24 +00:00
|
|
|
// where sender corresponds to notary request sender (the second fallback transaction
|
2023-12-09 08:37:25 +00:00
|
|
|
// signer), signer corresponds to main transaction signers and type corresponds to the
|
|
|
|
// [mempoolevent.Type] and denotes whether notary request was added to or removed from
|
|
|
|
// the notary request pool. nil value doesn't add any filter. See WSClient comments
|
|
|
|
// for generic Receive* behaviour details.
|
|
|
|
func (c *WSClient) ReceiveNotaryRequests(flt *neorpc.NotaryRequestFilter, rcvr chan<- *result.NotaryRequestEvent) (string, error) {
|
2022-10-25 12:11:24 +00:00
|
|
|
if rcvr == nil {
|
|
|
|
return "", ErrNilNotificationReceiver
|
2022-10-19 09:46:53 +00:00
|
|
|
}
|
2023-04-03 10:34:24 +00:00
|
|
|
params := []any{"notary_request_event"}
|
2022-10-25 12:11:24 +00:00
|
|
|
if flt != nil {
|
2023-03-16 20:50:56 +00:00
|
|
|
flt = flt.Copy()
|
2022-10-17 10:31:24 +00:00
|
|
|
params = append(params, *flt)
|
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
r := ¬aryRequestReceiver{
|
2022-10-17 10:31:24 +00:00
|
|
|
filter: flt,
|
2022-10-25 12:11:24 +00:00
|
|
|
ch: rcvr,
|
2021-05-28 11:55:06 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
return c.performSubscription(params, r)
|
2021-05-28 11:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 09:35:26 +00:00
|
|
|
// Unsubscribe removes subscription for the given event stream. It will return an
|
|
|
|
// error in case if there's no subscription with the provided ID. Call to Unsubscribe
|
|
|
|
// doesn't block notifications receive process for given subscriber, thus, ensure
|
|
|
|
// that subscriber channel is properly drained while unsubscription is being
|
2023-01-30 11:35:10 +00:00
|
|
|
// performed. Failing to do so will cause WSClient to block even regular requests.
|
|
|
|
// You may probably need to run unsubscription process in a separate
|
2022-11-16 09:35:26 +00:00
|
|
|
// routine (in parallel with notification receiver routine) to avoid Client's
|
|
|
|
// notification dispatcher blocking.
|
2020-05-12 08:18:44 +00:00
|
|
|
func (c *WSClient) Unsubscribe(id string) error {
|
|
|
|
return c.performUnsubscription(id)
|
|
|
|
}
|
|
|
|
|
2022-11-16 09:35:26 +00:00
|
|
|
// UnsubscribeAll removes all active subscriptions of the current client. It copies
|
|
|
|
// the list of subscribers in order not to hold the lock for the whole execution
|
|
|
|
// time and tries to unsubscribe from us many feeds as possible returning the
|
|
|
|
// chunk of unsubscription errors afterwards. Call to UnsubscribeAll doesn't block
|
|
|
|
// notifications receive process for given subscribers, thus, ensure that subscribers
|
2023-01-30 11:35:10 +00:00
|
|
|
// channels are properly drained while unsubscription is being performed. Failing to
|
|
|
|
// do so will cause WSClient to block even regular requests. You may probably need
|
|
|
|
// to run unsubscription process in a separate routine (in parallel with notification
|
|
|
|
// receiver routines) to avoid Client's notification dispatcher blocking.
|
2020-05-12 08:18:44 +00:00
|
|
|
func (c *WSClient) UnsubscribeAll() error {
|
2022-02-21 13:40:50 +00:00
|
|
|
c.subscriptionsLock.Lock()
|
2022-11-16 09:35:26 +00:00
|
|
|
subs := make([]string, 0, len(c.subscriptions))
|
2020-05-12 08:18:44 +00:00
|
|
|
for id := range c.subscriptions {
|
2022-11-16 09:35:26 +00:00
|
|
|
subs = append(subs, id)
|
|
|
|
}
|
|
|
|
c.subscriptionsLock.Unlock()
|
|
|
|
|
|
|
|
var resErr error
|
|
|
|
for _, id := range subs {
|
|
|
|
err := c.performUnsubscription(id)
|
2022-10-25 12:11:24 +00:00
|
|
|
if err != nil {
|
2024-03-04 14:59:04 +00:00
|
|
|
errFmt := "failed to unsubscribe from feed %d: %w"
|
2023-04-03 10:34:24 +00:00
|
|
|
errArgs := []any{err}
|
2022-11-16 09:35:26 +00:00
|
|
|
if resErr != nil {
|
|
|
|
errFmt = "%w; " + errFmt
|
2023-04-03 10:34:24 +00:00
|
|
|
errArgs = append([]any{resErr}, errArgs...)
|
2022-11-16 09:35:26 +00:00
|
|
|
}
|
|
|
|
resErr = fmt.Errorf(errFmt, errArgs...)
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
2022-11-16 09:35:26 +00:00
|
|
|
return resErr
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 09:35:26 +00:00
|
|
|
// performUnsubscription is internal method that removes subscription with the given
|
|
|
|
// ID from the list of subscriptions and receivers. It takes the subscriptions lock
|
|
|
|
// after WS RPC unsubscription request is completed. Until then the subscriber channel
|
|
|
|
// may still receive WS notifications.
|
|
|
|
func (c *WSClient) performUnsubscription(id string) error {
|
2024-07-24 14:15:50 +00:00
|
|
|
c.subscriptionsLock.RLock()
|
|
|
|
rcvrWas, ok := c.subscriptions[id]
|
|
|
|
c.subscriptionsLock.RUnlock()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no subscription with this ID")
|
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
var resp bool
|
2023-04-03 10:34:24 +00:00
|
|
|
if err := c.performRequest("unsubscribe", []any{id}, &resp); err != nil {
|
2022-10-25 12:11:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !resp {
|
|
|
|
return errors.New("unsubscribe method returned false result")
|
|
|
|
}
|
2022-11-16 09:35:26 +00:00
|
|
|
|
|
|
|
c.subscriptionsLock.Lock()
|
|
|
|
defer c.subscriptionsLock.Unlock()
|
|
|
|
|
|
|
|
rcvr, ok := c.subscriptions[id]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no subscription with this ID")
|
|
|
|
}
|
2024-07-24 14:15:50 +00:00
|
|
|
|
|
|
|
cleanUpSubscriptions := true
|
|
|
|
if rcvrWas.Receiver() != rcvr.Receiver() {
|
|
|
|
// concurrent subscription has been done and been overwritten; this
|
|
|
|
// is not this routine's subscription, cleanup only receivers map
|
|
|
|
rcvr = rcvrWas
|
|
|
|
cleanUpSubscriptions = false
|
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
ch := rcvr.Receiver()
|
|
|
|
ids := c.receivers[ch]
|
|
|
|
for i, rcvrID := range ids {
|
|
|
|
if rcvrID == id {
|
|
|
|
ids = append(ids[:i], ids[i+1:]...)
|
|
|
|
break
|
2022-02-21 13:40:50 +00:00
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
if len(ids) == 0 {
|
|
|
|
delete(c.receivers, ch)
|
|
|
|
} else {
|
|
|
|
c.receivers[ch] = ids
|
|
|
|
}
|
2024-07-24 14:15:50 +00:00
|
|
|
if cleanUpSubscriptions {
|
|
|
|
delete(c.subscriptions, id)
|
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-05-23 10:47:52 +00:00
|
|
|
|
|
|
|
// setCloseErr is a thread-safe method setting closeErr in case if it's not yet set.
|
|
|
|
func (c *WSClient) setCloseErr(err error) {
|
|
|
|
c.closeErrLock.Lock()
|
|
|
|
defer c.closeErrLock.Unlock()
|
|
|
|
|
|
|
|
if c.closeErr == nil {
|
|
|
|
c.closeErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 14:20:45 +00:00
|
|
|
// GetError returns the reason of WS connection closing. It returns nil in case if connection
|
|
|
|
// was closed by the use via Close() method calling.
|
2022-05-23 10:47:52 +00:00
|
|
|
func (c *WSClient) GetError() error {
|
|
|
|
c.closeErrLock.RLock()
|
|
|
|
defer c.closeErrLock.RUnlock()
|
|
|
|
|
2022-06-10 14:20:45 +00:00
|
|
|
if c.closeErr != nil && errors.Is(c.closeErr, errConnClosedByUser) {
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-23 10:47:52 +00:00
|
|
|
return c.closeErr
|
|
|
|
}
|
2022-10-12 12:23:32 +00:00
|
|
|
|
|
|
|
// Context returns WSClient Cancel context that will be terminated on Client shutdown.
|
|
|
|
func (c *WSClient) Context() context.Context {
|
|
|
|
return c.Client.ctx
|
|
|
|
}
|