mirror of
https://github.com/logos-messaging/go-libp2p-pubsub.git
synced 2026-01-07 15:23:08 +00:00
feat: WithValidatorData publishing option (#603)
Micro optimization that helps preventing deserialization of published messages in local subscribtions
This commit is contained in:
parent
f486808fbe
commit
68726389f2
12
topic.go
12
topic.go
@ -216,6 +216,7 @@ type PublishOptions struct {
|
|||||||
ready RouterReady
|
ready RouterReady
|
||||||
customKey ProvideKey
|
customKey ProvideKey
|
||||||
local bool
|
local bool
|
||||||
|
validatorData any
|
||||||
}
|
}
|
||||||
|
|
||||||
type PubOpt func(pub *PublishOptions) error
|
type PubOpt func(pub *PublishOptions) error
|
||||||
@ -308,7 +309,7 @@ func (t *Topic) Publish(ctx context.Context, data []byte, opts ...PubOpt) error
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return t.p.val.PushLocal(&Message{m, "", t.p.host.ID(), nil, pub.local})
|
return t.p.val.PushLocal(&Message{m, "", t.p.host.ID(), pub.validatorData, pub.local})
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithReadiness returns a publishing option for only publishing when the router is ready.
|
// WithReadiness returns a publishing option for only publishing when the router is ready.
|
||||||
@ -332,6 +333,15 @@ func WithLocalPublication(local bool) PubOpt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithValidatorData returns a publishing option to set custom validator data for the message.
|
||||||
|
// This allows users to avoid deserialization of the message data when validating the message locally.
|
||||||
|
func WithValidatorData(data any) PubOpt {
|
||||||
|
return func(pub *PublishOptions) error {
|
||||||
|
pub.validatorData = data
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithSecretKeyAndPeerId returns a publishing option for providing a custom private key and its corresponding peer ID
|
// WithSecretKeyAndPeerId returns a publishing option for providing a custom private key and its corresponding peer ID
|
||||||
// This option is useful when we want to send messages from "virtual", never-connectable peers in the network
|
// This option is useful when we want to send messages from "virtual", never-connectable peers in the network
|
||||||
func WithSecretKeyAndPeerId(key crypto.PrivKey, pid peer.ID) PubOpt {
|
func WithSecretKeyAndPeerId(key crypto.PrivKey, pid peer.ID) PubOpt {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user