Skip to content

Commit

Permalink
feat: add option to disable websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo committed Jun 7, 2024
1 parent 78b52ae commit 0bcd3fd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
11 changes: 10 additions & 1 deletion pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,16 @@ func createNodePool(ctx context.Context, nodes []string) (*rpc.Pool, error) {
return nil, fmt.Errorf("failed to create client: %w", err)
}

rpcNodes[i] = rpc.NewNode(client)
opts := []rpc.NodeOption{}

// Check is query string websocket is present in the endpoint
if u, err := url.Parse(endpoint); err == nil {
if u.Query().Get("__websocket") == "0" {
opts = append(opts, rpc.DisableWebsocket())
}
}

rpcNodes[i] = rpc.NewNode(client, opts...)

status, err := rpcNodes[i].Status(ctx)
if err != nil {
Expand Down
18 changes: 16 additions & 2 deletions pkg/rpc/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@ type OnNodeStatus func(ctx context.Context, n *Node, status *ctypes.ResultStatus

type NodeOption func(*Node)

func DisableWebsocket() NodeOption {
return func(n *Node) {
n.disableWebsocket = true
}
}

type Node struct {
Client *http.HTTP

// Save endpoint url for redacted logging
endpoint *url.URL

disableWebsocket bool

onStart []OnNodeStart
onStatus []OnNodeStatus
onEvent map[string][]OnNodeEvent
Expand Down Expand Up @@ -143,8 +151,10 @@ func (n *Node) Start(ctx context.Context) error {
initTicker.Stop()

// Start the websocket process
if err := n.Client.Start(); err != nil {
return fmt.Errorf("failed to start client: %w", err)
if !n.disableWebsocket {
if err := n.Client.Start(); err != nil {
return fmt.Errorf("failed to start client: %w", err)
}
}

blocksEvents, err := n.Subscribe(ctx, EventNewBlock)
Expand Down Expand Up @@ -351,6 +361,10 @@ func (n *Node) Stop(ctx context.Context) error {
}

func (n *Node) Subscribe(ctx context.Context, eventType string) (<-chan ctypes.ResultEvent, error) {
if n.disableWebsocket {
return make(chan ctypes.ResultEvent), nil
}

if res, ok := n.subscriptions[eventType]; ok {
return res, nil
}
Expand Down

0 comments on commit 0bcd3fd

Please sign in to comment.