From a3445b756fdb68f372dadd45de65a723d0237ac8 Mon Sep 17 00:00:00 2001 From: vyzo Date: Sat, 5 Sep 2020 23:35:13 +0300 Subject: [PATCH] add support for priority topic delivery weights --- peer_gater.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/peer_gater.go b/peer_gater.go index 37a7f5d..4f9b255 100644 --- a/peer_gater.go +++ b/peer_gater.go @@ -49,6 +49,9 @@ type PeerGaterParams struct { IgnoreWeight float64 // weight of rejected messages RejectWeight float64 + + // priority topic delivery weights + TopicDeliveryWeights map[string]float64 } func (p *PeerGaterParams) validate() error { @@ -84,6 +87,12 @@ func (p *PeerGaterParams) validate() error { return nil } +// WithTopicDeliveryWeights is a fluid setter for the priority topic delivery weights +func (p *PeerGaterParams) WithTopicDeliveryWeights(w map[string]float64) *PeerGaterParams { + p.TopicDeliveryWeights = w + return p +} + // NewPeerGaterParams creates a new PeerGaterParams struct, using the specified threshold and decay // parameters and default values for all other parameters. func NewPeerGaterParams(threshold, globalDecay, sourceDecay float64) *PeerGaterParams { @@ -386,7 +395,17 @@ func (pg *peerGater) DeliverMessage(msg *Message) { defer pg.Unlock() st := pg.getPeerStats(msg.ReceivedFrom) - st.deliver++ + + weight := 0.0 + for _, topic := range msg.GetTopicIDs() { + weight += pg.params.TopicDeliveryWeights[topic] + } + + if weight == 0 { + weight = 1 + } + + st.deliver += weight } func (pg *peerGater) RejectMessage(msg *Message, reason string) {