From c82d664e8ff004eb72d367170695b4a06acaccbe Mon Sep 17 00:00:00 2001 From: vyzo Date: Wed, 9 Sep 2020 15:15:41 +0300 Subject: [PATCH] prettify things --- score.go | 6 ++++-- score_test.go | 2 +- topic.go | 21 ++++++++++++--------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/score.go b/score.go index c9688ee..d2e5452 100644 --- a/score.go +++ b/score.go @@ -182,9 +182,11 @@ func newPeerScore(params *PeerScoreParams) *peerScore { } } -// update interface +// SetTopicScoreParams sets new score parameters for a topic. +// If the topic previously had parameters and the parameters are lowering delivery caps, +// then the score counters are recapped appropriately. +// Note: assumes that the topic score parameters have already been validated func (ps *peerScore) SetTopicScoreParams(topic string, p *TopicScoreParams) error { - // Note: assumes that the topic score parameters have already been validated ps.Lock() defer ps.Unlock() diff --git a/score_test.go b/score_test.go index 8e04a20..9b406c0 100644 --- a/score_test.go +++ b/score_test.go @@ -842,7 +842,7 @@ func TestScoreRetention(t *testing.T) { } } -func TestScoreResetTopicParams(t *testing.T) { +func TestScoreRecapTopicParams(t *testing.T) { // Create parameters with reasonable default values mytopic := "mytopic" params := &PeerScoreParams{ diff --git a/topic.go b/topic.go index cfdf088..72208f0 100644 --- a/topic.go +++ b/topic.go @@ -34,6 +34,11 @@ func (t *Topic) String() string { // SetScoreParams sets the topic score parameters if the pubsub router supports peer // scoring func (t *Topic) SetScoreParams(p *TopicScoreParams) error { + err := p.validate() + if err != nil { + return fmt.Errorf("invalid topic score parameters: %w", err) + } + t.mux.Lock() defer t.mux.Unlock() @@ -41,14 +46,8 @@ func (t *Topic) SetScoreParams(p *TopicScoreParams) error { return ErrTopicClosed } - err := p.validate() - if err != nil { - return fmt.Errorf("invalid topic score parameters: %w", err) - } - result := make(chan error, 1) - select { - case t.p.eval <- func() { + update := func() { gs, ok := t.p.rt.(*GossipSubRouter) if !ok { result <- fmt.Errorf("pubsub router is not gossipsub") @@ -60,8 +59,12 @@ func (t *Topic) SetScoreParams(p *TopicScoreParams) error { return } - result <- gs.score.SetTopicScoreParams(t.topic, p) - }: + err := gs.score.SetTopicScoreParams(t.topic, p) + result <- err + } + + select { + case t.p.eval <- update: err = <-result return err