Skip to content

Commit 6872638

Browse files
authored
feat: WithValidatorData publishing option (#603)
Micro optimization that helps preventing deserialization of published messages in local subscribtions
1 parent f486808 commit 6872638

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

topic.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,10 @@ type RouterReady func(rt PubSubRouter, topic string) (bool, error)
213213
type ProvideKey func() (crypto.PrivKey, peer.ID)
214214

215215
type PublishOptions struct {
216-
ready RouterReady
217-
customKey ProvideKey
218-
local bool
216+
ready RouterReady
217+
customKey ProvideKey
218+
local bool
219+
validatorData any
219220
}
220221

221222
type PubOpt func(pub *PublishOptions) error
@@ -308,7 +309,7 @@ func (t *Topic) Publish(ctx context.Context, data []byte, opts ...PubOpt) error
308309
}
309310
}
310311

311-
return t.p.val.PushLocal(&Message{m, "", t.p.host.ID(), nil, pub.local})
312+
return t.p.val.PushLocal(&Message{m, "", t.p.host.ID(), pub.validatorData, pub.local})
312313
}
313314

314315
// WithReadiness returns a publishing option for only publishing when the router is ready.
@@ -332,6 +333,15 @@ func WithLocalPublication(local bool) PubOpt {
332333
}
333334
}
334335

336+
// WithValidatorData returns a publishing option to set custom validator data for the message.
337+
// This allows users to avoid deserialization of the message data when validating the message locally.
338+
func WithValidatorData(data any) PubOpt {
339+
return func(pub *PublishOptions) error {
340+
pub.validatorData = data
341+
return nil
342+
}
343+
}
344+
335345
// WithSecretKeyAndPeerId returns a publishing option for providing a custom private key and its corresponding peer ID
336346
// This option is useful when we want to send messages from "virtual", never-connectable peers in the network
337347
func WithSecretKeyAndPeerId(key crypto.PrivKey, pid peer.ID) PubOpt {

0 commit comments

Comments
 (0)