Skip to content

Commit

Permalink
chore: add baseapp circuit breaker setter (#16289)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
tac0turtle and julienrbrt authored May 26, 2023
1 parent a9abe1c commit 59f6714
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

## Features

* (baseapp) [#16290](https://github.com/cosmos/cosmos-sdk/pull/16290) Add circuit breaker setter in baseapp.
* [#16060](https://github.com/cosmos/cosmos-sdk/pull/16060) Support saving restoring snapshot locally.

### Improvements
Expand Down
9 changes: 9 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ func (app *BaseApp) SetMsgServiceRouter(msgServiceRouter *MsgServiceRouter) {
app.msgServiceRouter = msgServiceRouter
}

// SetCircuitBreaker sets the circuit breaker for the BaseApp.
// The circuit breaker is checked on every message execution to verify if a transaction should be executed or not.
func (app *BaseApp) SetCircuitBreaker(cb CircuitBreaker) {
if app.msgServiceRouter == nil {
panic("cannot set circuit breaker with no msg service router set")
}
app.msgServiceRouter.SetCircuit(cb)
}

// MountStores mounts all IAVL or DB stores to the provided keys in the BaseApp
// multistore.
func (app *BaseApp) MountStores(keys ...storetypes.StoreKey) {
Expand Down
10 changes: 10 additions & 0 deletions baseapp/circuit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package baseapp

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// CircuitBreaker is an interface that defines the methods for a circuit breaker.
type CircuitBreaker interface {
IsAllowed(ctx sdk.Context, typeURL string) bool
}
12 changes: 12 additions & 0 deletions baseapp/msg_service_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
type MsgServiceRouter struct {
interfaceRegistry codectypes.InterfaceRegistry
routes map[string]MsgServiceHandler
circuitBreaker CircuitBreaker
}

var _ gogogrpc.Server = &MsgServiceRouter{}
Expand All @@ -31,6 +32,10 @@ func NewMsgServiceRouter() *MsgServiceRouter {
// MsgServiceHandler defines a function type which handles Msg service message.
type MsgServiceHandler = func(ctx sdk.Context, req sdk.Msg) (*sdk.Result, error)

func (msr *MsgServiceRouter) SetCircuit(cb CircuitBreaker) {
msr.circuitBreaker = cb
}

// Handler returns the MsgServiceHandler for a given msg or nil if not found.
func (msr *MsgServiceRouter) Handler(msg sdk.Msg) MsgServiceHandler {
return msr.routes[sdk.MsgTypeURL(msg)]
Expand Down Expand Up @@ -120,6 +125,13 @@ func (msr *MsgServiceRouter) RegisterService(sd *grpc.ServiceDesc, handler inter
} else {
return nil, err
}

if msr.circuitBreaker != nil {
msgURL := sdk.MsgTypeURL(req)
if !msr.circuitBreaker.IsAllowed(ctx, msgURL) {
return nil, fmt.Errorf("circuit breaker disables execution of this message: %s", msgURL)
}
}
}
// Call the method handler from the service description with the handler object.
// We don't do any decoding here because the decoding was already done.
Expand Down

0 comments on commit 59f6714

Please sign in to comment.