Skip to content

Commit

Permalink
prettify things
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Sep 9, 2020
1 parent 7388060 commit c82d664
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
6 changes: 4 additions & 2 deletions score.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion score_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
21 changes: 12 additions & 9 deletions topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,20 @@ 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()

if t.closed {
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")
Expand All @@ -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

Expand Down

0 comments on commit c82d664

Please sign in to comment.