Files
tendermint/rpc/core/events.go
Thane Thomson eed27addec docs: Update v0.34.x to prepare for v0.37 (#9244)
* Ignore generated/copied RPC docs

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Sync vuepress config with main

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Sync docs package-lock.json with main

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Sync docs redirects with main

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Sync docs versions with main

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Update OpenAPI version to v0.34

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Sync DOCS_README with main

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Update all v0.34.x docs references from master to main

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Update v0.34 OpenAPI references from master to main

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Update repo doc links from master to main

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Update code comment references from master to main

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Update repo root doc links from master to main

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Update repo root doc links for docs.tendermint.com from master to main

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Build v0.34.x as "latest"

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Explicitly mark v0.34 docs as latest in version selector

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Add nav link to main and clearly mark as unstable

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Direct all docs.tendermint.com links to v0.34 on v0.34.x

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Update all relevant links on v0.34.x branch to be v0.34-specific

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Update changelog refs to docs.tendermint.com

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Update remaining GH master link to main

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Sync docs build and nav config with main

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Migrate spec links to GitHub repo from docs site

Signed-off-by: Thane Thomson <connect@thanethomson.com>

Signed-off-by: Thane Thomson <connect@thanethomson.com>
2022-08-19 07:20:32 -04:00

131 lines
4.2 KiB
Go

package core
import (
"context"
"errors"
"fmt"
"time"
tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
)
const (
// maxQueryLength is the maximum length of a query string that will be
// accepted. This is just a safety check to avoid outlandish queries.
maxQueryLength = 512
)
// Subscribe for events via WebSocket.
// More: https://docs.tendermint.com/v0.34/rpc/#/Websocket/subscribe
func Subscribe(ctx *rpctypes.Context, query string) (*ctypes.ResultSubscribe, error) {
addr := ctx.RemoteAddr()
if env.EventBus.NumClients() >= env.Config.MaxSubscriptionClients {
return nil, fmt.Errorf("max_subscription_clients %d reached", env.Config.MaxSubscriptionClients)
} else if env.EventBus.NumClientSubscriptions(addr) >= env.Config.MaxSubscriptionsPerClient {
return nil, fmt.Errorf("max_subscriptions_per_client %d reached", env.Config.MaxSubscriptionsPerClient)
} else if len(query) > maxQueryLength {
return nil, errors.New("maximum query length exceeded")
}
env.Logger.Info("Subscribe to query", "remote", addr, "query", query)
q, err := tmquery.New(query)
if err != nil {
return nil, fmt.Errorf("failed to parse query: %w", err)
}
subCtx, cancel := context.WithTimeout(ctx.Context(), SubscribeTimeout)
defer cancel()
sub, err := env.EventBus.Subscribe(subCtx, addr, q, env.Config.SubscriptionBufferSize)
if err != nil {
return nil, err
}
closeIfSlow := env.Config.CloseOnSlowClient
// Capture the current ID, since it can change in the future.
subscriptionID := ctx.JSONReq.ID
go func() {
for {
select {
case msg := <-sub.Out():
var (
resultEvent = &ctypes.ResultEvent{Query: query, Data: msg.Data(), Events: msg.Events()}
resp = rpctypes.NewRPCSuccessResponse(subscriptionID, resultEvent)
)
writeCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := ctx.WSConn.WriteRPCResponse(writeCtx, resp); err != nil {
env.Logger.Info("Can't write response (slow client)",
"to", addr, "subscriptionID", subscriptionID, "err", err)
if closeIfSlow {
var (
err = errors.New("subscription was cancelled (reason: slow client)")
resp = rpctypes.RPCServerError(subscriptionID, err)
)
if !ctx.WSConn.TryWriteRPCResponse(resp) {
env.Logger.Info("Can't write response (slow client)",
"to", addr, "subscriptionID", subscriptionID, "err", err)
}
return
}
}
case <-sub.Cancelled():
if sub.Err() != tmpubsub.ErrUnsubscribed {
var reason string
if sub.Err() == nil {
reason = "Tendermint exited"
} else {
reason = sub.Err().Error()
}
var (
err = fmt.Errorf("subscription was cancelled (reason: %s)", reason)
resp = rpctypes.RPCServerError(subscriptionID, err)
)
if !ctx.WSConn.TryWriteRPCResponse(resp) {
env.Logger.Info("Can't write response (slow client)",
"to", addr, "subscriptionID", subscriptionID, "err", err)
}
}
return
}
}
}()
return &ctypes.ResultSubscribe{}, nil
}
// Unsubscribe from events via WebSocket.
// More: https://docs.tendermint.com/v0.34/rpc/#/Websocket/unsubscribe
func Unsubscribe(ctx *rpctypes.Context, query string) (*ctypes.ResultUnsubscribe, error) {
addr := ctx.RemoteAddr()
env.Logger.Info("Unsubscribe from query", "remote", addr, "query", query)
q, err := tmquery.New(query)
if err != nil {
return nil, fmt.Errorf("failed to parse query: %w", err)
}
err = env.EventBus.Unsubscribe(context.Background(), addr, q)
if err != nil {
return nil, err
}
return &ctypes.ResultUnsubscribe{}, nil
}
// UnsubscribeAll from all events via WebSocket.
// More: https://docs.tendermint.com/v0.34/rpc/#/Websocket/unsubscribe_all
func UnsubscribeAll(ctx *rpctypes.Context) (*ctypes.ResultUnsubscribe, error) {
addr := ctx.RemoteAddr()
env.Logger.Info("Unsubscribe from all", "remote", addr)
err := env.EventBus.UnsubscribeAll(context.Background(), addr)
if err != nil {
return nil, err
}
return &ctypes.ResultUnsubscribe{}, nil
}