Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add experimental rate limiter on non-broadcast messages #1404

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/config/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ type clientConfiguration struct {
}

type kadcastConfiguration struct {
Enabled bool
Enabled bool
Limit string

Address string
BootstrapAddr []string

Expand Down
5 changes: 3 additions & 2 deletions pkg/config/samples/default.dusk.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ address="monitor.dusk.network:1337"

# Kadcast peer settings
[kadcast]
# if disabled, gossip protocol is active
enabled=false
enabled=true
# Back pressure on one-to-one messaging
limit = "50ms"

# grpc client connection config
[kadcast.grpc]
Expand Down
36 changes: 36 additions & 0 deletions pkg/p2p/kadcast/writer/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (
"bytes"
"context"
"encoding/binary"
"time"

"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/protocol"
"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/topics"
"github.com/dusk-network/dusk-blockchain/pkg/util/nativeutils/eventbus"
"github.com/dusk-network/dusk-protobuf/autogen/go/rusk"
"github.com/sirupsen/logrus"
"golang.org/x/time/rate"

crypto "github.com/dusk-network/dusk-crypto/hash"
)
Expand All @@ -36,6 +38,34 @@ type Base struct {
ctx context.Context

topic topics.Topic

limiter *rate.Limiter
}

func newBase(ctx context.Context, s eventbus.Subscriber, g *protocol.Gossip, rusk rusk.NetworkClient, t topics.Topic) Base {
return Base{
subscriber: s,
gossip: g,
client: rusk,
ctx: ctx,
topic: t,
limiter: nil,
}
}

func newBaseWithLimiter(ctx context.Context, s eventbus.Subscriber, g *protocol.Gossip, rusk rusk.NetworkClient, t topics.Topic, limit string) Base {
b := newBase(ctx, s, g, rusk, t)

if len(limit) > 0 {
timeout, err := time.ParseDuration(limit)
if err != nil {
log.WithError(err).Error("could not parse kadcast limit")
}

b.limiter = rate.NewLimiter(rate.Every(timeout), 1)
}

return b
}

// Send is a wrapper of rusk.NetworkClient Send method.
Expand All @@ -58,6 +88,12 @@ func (b *Base) Send(data []byte, addr string) error {
Message: blob.Bytes(),
}

if b.limiter != nil {
if err := b.limiter.WaitN(b.ctx, 1); err != nil {
return err
}
}

// send message
if _, err := b.client.Send(b.ctx, m); err != nil {
log.WithError(err).Warn("failed to send message")
Expand Down
8 changes: 1 addition & 7 deletions pkg/p2p/kadcast/writer/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ type Broadcast struct {
// NewBroadcast ...
func NewBroadcast(ctx context.Context, s eventbus.Subscriber, g *protocol.Gossip, rusk rusk.NetworkClient) ring.Writer {
b := &Broadcast{
Base: Base{
subscriber: s,
gossip: g,
client: rusk,
ctx: ctx,
topic: topics.Kadcast,
},
Base: newBase(ctx, s, g, rusk, topics.Kadcast),
}

b.Subscribe()
Expand Down
11 changes: 4 additions & 7 deletions pkg/p2p/kadcast/writer/sendmany.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"context"
"errors"

"github.com/dusk-network/dusk-blockchain/pkg/config"
"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/protocol"
"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/topics"
"github.com/dusk-network/dusk-blockchain/pkg/util/container/ring"
Expand All @@ -25,14 +26,10 @@ type SendToMany struct {

// NewSendToMany ...
func NewSendToMany(ctx context.Context, s eventbus.Subscriber, g *protocol.Gossip, rusk rusk.NetworkClient) ring.Writer {
l := config.Get().Kadcast.Limit

w := &SendToMany{
Base: Base{
subscriber: s,
gossip: g,
client: rusk,
ctx: ctx,
topic: topics.KadcastSendToMany,
},
Base: newBaseWithLimiter(ctx, s, g, rusk, topics.KadcastSendToMany, l),
}

w.Subscribe()
Expand Down
11 changes: 4 additions & 7 deletions pkg/p2p/kadcast/writer/sendone.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"context"
"errors"

"github.com/dusk-network/dusk-blockchain/pkg/config"
"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/protocol"
"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/topics"
"github.com/dusk-network/dusk-blockchain/pkg/util/container/ring"
Expand All @@ -25,14 +26,10 @@ type SendToOne struct {

// NewSendToOne ...
func NewSendToOne(ctx context.Context, s eventbus.Subscriber, g *protocol.Gossip, rusk rusk.NetworkClient) ring.Writer {
l := config.Get().Kadcast.Limit

w := &SendToOne{
Base: Base{
subscriber: s,
gossip: g,
client: rusk,
ctx: ctx,
topic: topics.KadcastSendToOne,
},
Base: newBaseWithLimiter(ctx, s, g, rusk, topics.KadcastSendToOne, l),
}

w.Subscribe()
Expand Down