pubsub

package
v7.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2022 License: ISC Imports: 22 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// PingInterval is how frequently the server will ping all clients. The
	// clients should set their read deadlines to more than this.
	PingInterval = 30 * time.Second

	// NewTxBufferSize is the maximum length of the new transaction slice sent
	// to websocket clients.
	NewTxBufferSize = 5
)

Variables

This section is empty.

Functions

func DisableLog

func DisableLog()

DisableLog disables all library log output. Logging output is disabled by default until UseLogger is called.

func UseLogger

func UseLogger(logger slog.Logger)

UseLogger uses a specified Logger to output package logging info.

func Version

func Version() semver.Semver

Version indicates the semantic version of the pubsub module.

Types

type DataSource

type DataSource interface {
	GetExplorerBlock(hash string) *exptypes.BlockInfo
	DecodeRawTransaction(txhex string) (*chainjson.TxRawResult, error)
	SendRawTransaction(txhex string) (string, error)
	GetChainParams() *chaincfg.Params
	BlockSubsidy(height int64, voters uint16) *chainjson.GetBlockSubsidyResult
	Difficulty(timestamp int64) float64
}

DataSource defines the interface for collecting required data.

type PubSubHub

type PubSubHub struct {
	// contains filtered or unexported fields
}

PubSubHub manages the collection and distribution of block chain and mempool data to WebSocket clients.

func NewPubSubHub

func NewPubSubHub(dataSource DataSource) (*PubSubHub, error)

NewPubSubHub constructs a PubSubHub given a data source. The WebSocketHub is automatically started.

func (*PubSubHub) HubRelay

func (psh *PubSubHub) HubRelay() chan pstypes.HubMessage

HubRelay returns the channel used to signal to the WebSocketHub. See pstypes.HubSignal for valid signals.

func (*PubSubHub) MempoolInventory

func (psh *PubSubHub) MempoolInventory() *exptypes.MempoolInfo

MempoolInventory safely retrieves the current mempool inventory.

func (*PubSubHub) Ready

func (psh *PubSubHub) Ready() bool

Ready checks if the WebSocketHub is ready.

func (*PubSubHub) SetReady

func (psh *PubSubHub) SetReady(ready bool)

SetReady updates the ready status of the WebSocketHub.

func (*PubSubHub) StopWebsocketHub

func (psh *PubSubHub) StopWebsocketHub()

StopWebsocketHub stops the websocket hub.

func (*PubSubHub) Store

func (psh *PubSubHub) Store(blockData *blockdata.BlockData, msgBlock *wire.MsgBlock) error

Store processes and stores new block data, then signals to the WebSocketHub that the new data is available.

func (*PubSubHub) StoreMPData

func (psh *PubSubHub) StoreMPData(_ *mempool.StakeData, _ []exptypes.MempoolTx, inv *exptypes.MempoolInfo)

StoreMPData stores mempool data. It is advisable to pass a copy of the []exptypes.MempoolTx so that it may be modified (e.g. sorted) without affecting other MempoolDataSavers. The struct pointed to may be shared, so it should not be modified.

func (*PubSubHub) WebSocketHandler

func (psh *PubSubHub) WebSocketHandler(w http.ResponseWriter, r *http.Request)

WebSocketHandler is the http.HandlerFunc for new websocket connections. The connection is registered with the WebSocketHub, and the send/receive loops are launched.

type State

type State struct {

	// GeneralInfo contains a variety of high level status information. Much of
	// GeneralInfo is constant, set in the constructor, while many fields are
	// set when Store provides new block details.
	GeneralInfo *exptypes.HomeInfo

	// BlockInfo contains details on the most recent block. It is updated when
	// Store provides new block details.
	BlockInfo *exptypes.BlockInfo

	// BlockchainInfo contains the result of the getblockchaininfo RPC. It is
	// updated when Store provides new block details.
	BlockchainInfo *chainjson.GetBlockChainInfoResult
	// contains filtered or unexported fields
}

State represents the current state of block chain.

type WebsocketHub

type WebsocketHub struct {
	Register   chan *clientHubSpoke
	Unregister chan *hubSpoke
	HubRelay   chan pstypes.HubMessage
	// contains filtered or unexported fields
}

WebsocketHub and its event loop manage all websocket client connections. WebsocketHub is responsible for closing all connections registered with it. If the event loop is running, calling (*WebsocketHub).Stop() will handle it.

func NewWebsocketHub

func NewWebsocketHub() *WebsocketHub

NewWebsocketHub creates a new WebsocketHub.

func (*WebsocketHub) NewClientHubSpoke

func (wsh *WebsocketHub) NewClientHubSpoke() *clientHubSpoke

NewClientHubSpoke registers a connection with the hub, and returns a pointer to the new client data object. Use UnregisterClient on this object to stop signaling messages, and close the signal channel.

func (*WebsocketHub) NumClients

func (wsh *WebsocketHub) NumClients() int

NumClients returns the number of clients connected to the websocket hub.

func (*WebsocketHub) Ready

func (wsh *WebsocketHub) Ready() bool

Ready is a thread-safe way to fetch the boolean in ready.

func (*WebsocketHub) Run

func (wsh *WebsocketHub) Run()

Run starts the main event loop, which handles the following: 1. receiving signals on the WebsocketHub's HubRelay and broadcasting them to all registered clients, 2. registering clients, 3. unregistering clients, 4. periodically sending client's new transaction buffers, and 5. handling the shutdown signal from Stop.

func (*WebsocketHub) SetReady

func (wsh *WebsocketHub) SetReady(ready bool)

SetReady is a thread-safe way to update the ready.

func (*WebsocketHub) SetTimeToSendTxBuffer

func (wsh *WebsocketHub) SetTimeToSendTxBuffer(ready bool)

func (*WebsocketHub) Stop

func (wsh *WebsocketHub) Stop()

Stop kills the run() loop and unregisters all clients (connections).

func (*WebsocketHub) TimeToSendTxBuffer

func (wsh *WebsocketHub) TimeToSendTxBuffer() bool

func (*WebsocketHub) UnregisterClient

func (wsh *WebsocketHub) UnregisterClient(ch *clientHubSpoke)

UnregisterClient unregisters the client with the hub and closes the client's update signal channel. The client is unregistered via the main run() loop, so this call will block if the run() loop is not running.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL