mirror of
https://github.com/tendermint/tendermint.git
synced 2026-08-18 21:26:20 +00:00
service: change stop interface (#7816)
This commit is contained in:
@@ -2,7 +2,6 @@ package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"syscall"
|
||||
@@ -67,7 +66,7 @@ type multiAppConn struct {
|
||||
// of reasonable lifecycle witout needing an explicit stop method.
|
||||
type stoppableClient interface {
|
||||
abciclient.Client
|
||||
Stop() error
|
||||
Stop()
|
||||
}
|
||||
|
||||
// NewMultiAppConn makes all necessary abci connections to the application.
|
||||
@@ -81,53 +80,42 @@ func NewMultiAppConn(clientCreator abciclient.Creator, logger log.Logger, metric
|
||||
return multiAppConn
|
||||
}
|
||||
|
||||
func (app *multiAppConn) Mempool() AppConnMempool {
|
||||
return app.mempoolConn
|
||||
}
|
||||
|
||||
func (app *multiAppConn) Consensus() AppConnConsensus {
|
||||
return app.consensusConn
|
||||
}
|
||||
|
||||
func (app *multiAppConn) Query() AppConnQuery {
|
||||
return app.queryConn
|
||||
}
|
||||
|
||||
func (app *multiAppConn) Snapshot() AppConnSnapshot {
|
||||
return app.snapshotConn
|
||||
}
|
||||
func (app *multiAppConn) Mempool() AppConnMempool { return app.mempoolConn }
|
||||
func (app *multiAppConn) Consensus() AppConnConsensus { return app.consensusConn }
|
||||
func (app *multiAppConn) Query() AppConnQuery { return app.queryConn }
|
||||
func (app *multiAppConn) Snapshot() AppConnSnapshot { return app.snapshotConn }
|
||||
|
||||
func (app *multiAppConn) OnStart(ctx context.Context) error {
|
||||
c, err := app.abciClientFor(ctx, connQuery)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
app.queryConnClient = c.(stoppableClient)
|
||||
app.queryConn = NewAppConnQuery(c, app.metrics)
|
||||
var err error
|
||||
defer func() {
|
||||
if err != nil {
|
||||
app.stopAllClients()
|
||||
}
|
||||
}()
|
||||
|
||||
c, err = app.abciClientFor(ctx, connSnapshot)
|
||||
app.queryConnClient, err = app.abciClientFor(ctx, connQuery)
|
||||
if err != nil {
|
||||
app.stopAllClients()
|
||||
return err
|
||||
}
|
||||
app.snapshotConnClient = c.(stoppableClient)
|
||||
app.snapshotConn = NewAppConnSnapshot(c, app.metrics)
|
||||
app.queryConn = NewAppConnQuery(app.queryConnClient, app.metrics)
|
||||
|
||||
c, err = app.abciClientFor(ctx, connMempool)
|
||||
app.snapshotConnClient, err = app.abciClientFor(ctx, connSnapshot)
|
||||
if err != nil {
|
||||
app.stopAllClients()
|
||||
return err
|
||||
}
|
||||
app.mempoolConnClient = c.(stoppableClient)
|
||||
app.mempoolConn = NewAppConnMempool(c, app.metrics)
|
||||
app.snapshotConn = NewAppConnSnapshot(app.snapshotConnClient, app.metrics)
|
||||
|
||||
c, err = app.abciClientFor(ctx, connConsensus)
|
||||
app.mempoolConnClient, err = app.abciClientFor(ctx, connMempool)
|
||||
if err != nil {
|
||||
app.stopAllClients()
|
||||
return err
|
||||
}
|
||||
app.consensusConnClient = c.(stoppableClient)
|
||||
app.consensusConn = NewAppConnConsensus(c, app.metrics)
|
||||
app.mempoolConn = NewAppConnMempool(app.mempoolConnClient, app.metrics)
|
||||
|
||||
app.consensusConnClient, err = app.abciClientFor(ctx, connConsensus)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
app.consensusConn = NewAppConnConsensus(app.consensusConnClient, app.metrics)
|
||||
|
||||
// Kill Tendermint if the ABCI application crashes.
|
||||
app.startWatchersForClientErrorToKillTendermint(ctx)
|
||||
@@ -135,9 +123,7 @@ func (app *multiAppConn) OnStart(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *multiAppConn) OnStop() {
|
||||
app.stopAllClients()
|
||||
}
|
||||
func (app *multiAppConn) OnStop() { app.stopAllClients() }
|
||||
|
||||
func (app *multiAppConn) startWatchersForClientErrorToKillTendermint(ctx context.Context) {
|
||||
// this function starts a number of threads (per abci client)
|
||||
@@ -154,12 +140,10 @@ func (app *multiAppConn) startWatchersForClientErrorToKillTendermint(ctx context
|
||||
}
|
||||
}
|
||||
|
||||
type op struct {
|
||||
for _, client := range []struct {
|
||||
connClient stoppableClient
|
||||
name string
|
||||
}
|
||||
|
||||
for _, client := range []op{
|
||||
}{
|
||||
{
|
||||
connClient: app.consensusConnClient,
|
||||
name: connConsensus,
|
||||
@@ -190,47 +174,36 @@ func (app *multiAppConn) startWatchersForClientErrorToKillTendermint(ctx context
|
||||
}
|
||||
|
||||
func (app *multiAppConn) stopAllClients() {
|
||||
if app.consensusConnClient != nil {
|
||||
if err := app.consensusConnClient.Stop(); err != nil {
|
||||
if !errors.Is(err, service.ErrAlreadyStopped) {
|
||||
app.logger.Error("error while stopping consensus client", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if app.mempoolConnClient != nil {
|
||||
if err := app.mempoolConnClient.Stop(); err != nil {
|
||||
if !errors.Is(err, service.ErrAlreadyStopped) {
|
||||
app.logger.Error("error while stopping mempool client", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if app.queryConnClient != nil {
|
||||
if err := app.queryConnClient.Stop(); err != nil {
|
||||
if !errors.Is(err, service.ErrAlreadyStopped) {
|
||||
app.logger.Error("error while stopping query client", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if app.snapshotConnClient != nil {
|
||||
if err := app.snapshotConnClient.Stop(); err != nil {
|
||||
if !errors.Is(err, service.ErrAlreadyStopped) {
|
||||
app.logger.Error("error while stopping snapshot client", "error", err)
|
||||
}
|
||||
for _, client := range []stoppableClient{
|
||||
app.consensusConnClient,
|
||||
app.mempoolConnClient,
|
||||
app.queryConnClient,
|
||||
app.snapshotConnClient,
|
||||
} {
|
||||
if client != nil {
|
||||
client.Stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (app *multiAppConn) abciClientFor(ctx context.Context, conn string) (abciclient.Client, error) {
|
||||
func (app *multiAppConn) abciClientFor(ctx context.Context, conn string) (stoppableClient, error) {
|
||||
c, err := app.clientCreator(app.logger.With(
|
||||
"module", "abci-client",
|
||||
"connection", conn))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating ABCI client (%s connection): %w", conn, err)
|
||||
}
|
||||
|
||||
if err := c.Start(ctx); err != nil {
|
||||
return nil, fmt.Errorf("error starting ABCI client (%s connection): %w", conn, err)
|
||||
}
|
||||
return c, nil
|
||||
|
||||
client, ok := c.(stoppableClient)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%T is not a stoppable client", c)
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func kill() error {
|
||||
|
||||
@@ -23,7 +23,7 @@ type noopStoppableClientImpl struct {
|
||||
count int
|
||||
}
|
||||
|
||||
func (c *noopStoppableClientImpl) Stop() error { c.count++; return nil }
|
||||
func (c *noopStoppableClientImpl) Stop() { c.count++ }
|
||||
|
||||
func TestAppConns_Start_Stop(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
Reference in New Issue
Block a user