From 776d8592b871718e6f63228f5ce8f9c919f941ea Mon Sep 17 00:00:00 2001 From: Yahya Hassanzadeh Date: Mon, 28 Nov 2022 15:31:27 -0800 Subject: [PATCH] refactors app specific to return an error --- pubsub.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pubsub.go b/pubsub.go index 067a2051..cf4644a9 100644 --- a/pubsub.go +++ b/pubsub.go @@ -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. @@ -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 @@ -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 } }