move client creators

This commit is contained in:
tycho garen
2021-09-17 14:29:05 -04:00
parent bf38bef554
commit ef9b9e3f53
3 changed files with 70 additions and 64 deletions
-8
View File
@@ -64,14 +64,6 @@ type Client interface {
ApplySnapshotChunkSync(context.Context, types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error)
}
//go:generate ../scripts/mockery_generate.sh ClientCreator
// ClientCreator creates new ABCI clients.
type ClientCreator interface {
// NewABCIClient returns a new ABCI client.
NewABCIClient() (Client, error)
}
//----------------------------------------
// NewClient returns a new ABCI client of the specified transport type.
+66
View File
@@ -0,0 +1,66 @@
package abcicli
import (
"fmt"
"github.com/tendermint/tendermint/abci/types"
tmsync "github.com/tendermint/tendermint/internal/libs/sync"
)
//go:generate ../scripts/mockery_generate.sh ClientCreator
// ClientCreator creates new ABCI clients.
type ClientCreator interface {
// NewABCIClient returns a new ABCI client.
NewABCIClient() (Client, error)
}
//----------------------------------------------------
// local proxy uses a mutex on an in-proc app
type localClientCreator struct {
mtx *tmsync.RWMutex
app types.Application
}
// NewLocalClientCreator returns a ClientCreator for the given app,
// which will be running locally.
func NewLocalClientCreator(app types.Application) ClientCreator {
return &localClientCreator{
mtx: new(tmsync.RWMutex),
app: app,
}
}
func (l *localClientCreator) NewABCIClient() (Client, error) {
return NewLocalClient(l.mtx, l.app), nil
}
//---------------------------------------------------------------
// remote proxy opens new connections to an external app process
type remoteClientCreator struct {
addr string
transport string
mustConnect bool
}
// NewRemoteClientCreator returns a ClientCreator for the given address (e.g.
// "192.168.0.1") and transport (e.g. "tcp"). Set mustConnect to true if you
// want the client to connect before reporting success.
func NewRemoteClientCreator(addr, transport string, mustConnect bool) ClientCreator {
return &remoteClientCreator{
addr: addr,
transport: transport,
mustConnect: mustConnect,
}
}
func (r *remoteClientCreator) NewABCIClient() (Client, error) {
remoteApp, err := NewClient(r.addr, r.transport, r.mustConnect)
if err != nil {
return nil, fmt.Errorf("failed to connect to proxy: %w", err)
}
return remoteApp, nil
}