Skip to content

Commit

Permalink
feat(core): bridge create params do not accept bridge limits
Browse files Browse the repository at this point in the history
  • Loading branch information
shivanshkc committed Jul 13, 2022
1 parent 5cb3d6b commit 72f7af3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
4 changes: 4 additions & 0 deletions configs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ auth:
internal_username: dev
internal_password: dev

bridges:
max_bridge_limit: 10000
max_bridge_limit_per_client: 10

discovery:
max_addr_resolution_attempts: 10
addr_resolution_period_sec: 3
Expand Down
8 changes: 8 additions & 0 deletions src/configs/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ type Model struct {
InternalPassword string `mapstructure:"internal_password"`
} `mapstructure:"auth"`

// Bridges is the model of bridge-related configs.
Bridges struct {
// MaxBridgeLimit is the max number of bridges this node can host.
MaxBridgeLimit int `mapstructure:"max_bridge_limit"`
// MaxBridgeLimitPerClient is the max number of bridges this node can host per client.
MaxBridgeLimitPerClient int `mapstructure:"max_bridge_limit_per_client"`
} `mapstructure:"bridges"`

// Discovery is the model of the discovery address related configs.
Discovery struct {
// MaxAddrResolutionAttempts is max number of times we will attempt to resolve the discovery address.
Expand Down
5 changes: 0 additions & 5 deletions src/core/models/bridges.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,4 @@ type BridgeCreateParams struct {
Writer http.ResponseWriter
// Request is required to upgrade the connection to websocket (if the websocket protocol is being used).
Request *http.Request

// MaxBridgeCount is the max number of bridges allowed. It is optional.
MaxBridgeCount *int
// MaxBridgeCountPerClient is the max number of bridges allowed per client. It is optional.
MaxBridgeCountPerClient *int
}
9 changes: 5 additions & 4 deletions src/impl/bridges/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"sync"

"github.com/shivanshkc/rosenbridge/src/configs"
"github.com/shivanshkc/rosenbridge/src/core/deps"
"github.com/shivanshkc/rosenbridge/src/core/models"
"github.com/shivanshkc/rosenbridge/src/logger"
Expand Down Expand Up @@ -42,20 +43,20 @@ func NewManager() *Manager {
}

func (m *Manager) CreateBridge(ctx context.Context, params *models.BridgeCreateParams) (deps.Bridge, error) {
log := logger.Get()
// Prerequisites.
conf, log := configs.Get(), logger.Get()

// Locking the bridges map and count for read-write operations.
m.bridgesMutex.Lock()
defer m.bridgesMutex.Unlock()

// Checking if the bridge limit is reached.
if params.MaxBridgeCount != nil && m.bridgeCount >= *params.MaxBridgeCount {
if m.bridgeCount >= conf.Bridges.MaxBridgeLimit {
log.Warn(ctx, &logger.Entry{Payload: fmt.Sprintf("node has reached its bridge limit: %d", m.bridgeCount)})
return nil, errutils.TooManyBridges()
}
// Checking if the bridge limit per client has reached.
if params.MaxBridgeCountPerClient != nil &&
m.bridgeCountPerClient[params.ClientID] >= *params.MaxBridgeCountPerClient {
if m.bridgeCountPerClient[params.ClientID] >= conf.Bridges.MaxBridgeLimitPerClient {
log.Warn(ctx, &logger.Entry{Payload: fmt.Sprintf("node has reached its bridge limit for client: %s: %d",
params.ClientID, m.bridgeCount)})
return nil, errutils.TooManyBridgesForClient()
Expand Down

0 comments on commit 72f7af3

Please sign in to comment.