Skip to content

Commit

Permalink
refactors app specific to return an error
Browse files Browse the repository at this point in the history
  • Loading branch information
yhassanzadeh13 committed Nov 28, 2022
1 parent e23f9ea commit 776d859
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ type PubSub struct {

// appSpecificRpcInspector is an auxiliary that may be set by the application to inspect incoming RPCs prior to
// processing them. The inspector is invoked on an accepted RPC right prior to handling it.
// The return value of the inspector function is a boolean indicating whether the RPC should be processed or not.
appSpecificRpcInspector func(peer.ID, *RPC) bool
// The return value of the inspector function is an error indicating whether the RPC should be processed or not.
// If the error is nil, the RPC is processed as usual. If the error is non-nil, the RPC is dropped.
appSpecificRpcInspector func(peer.ID, *RPC) error
}

// PubSubRouter is the message router component of PubSub.
Expand Down Expand Up @@ -532,7 +533,7 @@ func WithSeenMessagesTTL(ttl time.Duration) Option {
}
}

func WithAppSpecificRpcInspector(inspector func(peer.ID, *RPC) bool) Option {
func WithAppSpecificRpcInspector(inspector func(peer.ID, *RPC) error) Option {
return func(ps *PubSub) error {
ps.appSpecificRpcInspector = inspector
return nil
Expand Down Expand Up @@ -1020,7 +1021,8 @@ func (p *PubSub) handleIncomingRPC(rpc *RPC) {
// pass the rpc through app specific validation (if any available).
if p.appSpecificRpcInspector != nil {
// check if the RPC is allowed by the external inspector
if accept := p.appSpecificRpcInspector(rpc.from, rpc); !accept {
if err := p.appSpecificRpcInspector(rpc.from, rpc); err != nil {
log.Debugf("application-specific inspection failed, rejecting incoming rpc: %s", err)
return // reject the RPC
}
}
Expand Down

0 comments on commit 776d859

Please sign in to comment.