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

Ability to limit maximum number of clients #379

Merged
merged 4 commits into from
Apr 16, 2024
Merged
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
12 changes: 12 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var (

// Capabilities indicates the capabilities and features provided by the server.
type Capabilities struct {
MaximumClients int64 `yaml:"maximum_clients" json:"maximum_clients"` // maximum number of connected clients
MaximumMessageExpiryInterval int64 `yaml:"maximum_message_expiry_interval" json:"maximum_message_expiry_interval"` // maximum message expiry if message expiry is 0 or over
MaximumClientWritesPending int32 `yaml:"maximum_client_writes_pending" json:"maximum_client_writes_pending"` // maximum number of pending message writes for a client
MaximumSessionExpiryInterval uint32 `yaml:"maximum_session_expiry_interval" json:"maximum_session_expiry_interval"` // maximum number of seconds to keep disconnected sessions
Expand All @@ -65,6 +66,7 @@ type Capabilities struct {
// NewDefaultServerCapabilities defines the default features and capabilities provided by the server.
func NewDefaultServerCapabilities() *Capabilities {
return &Capabilities{
MaximumClients: math.MaxInt64, // maximum number of connected clients
MaximumMessageExpiryInterval: 60 * 60 * 24, // maximum message expiry if message expiry is 0 or over
MaximumClientWritesPending: 1024 * 8, // maximum number of pending message writes for a client
MaximumSessionExpiryInterval: math.MaxUint32, // maximum number of seconds to keep disconnected sessions
Expand Down Expand Up @@ -414,6 +416,16 @@ func (s *Server) attachClient(cl *Client, listener string) error {
}

cl.ParseConnect(listener, pk)
if atomic.LoadInt64(&s.Info.ClientsConnected) >= s.Options.Capabilities.MaximumClients {
if cl.Properties.ProtocolVersion < 5 {
s.SendConnack(cl, packets.ErrServerUnavailable, false, nil)
} else {
s.SendConnack(cl, packets.ErrServerBusy, false, nil)
}

return packets.ErrServerBusy
}

code := s.validateConnect(cl, pk) // [MQTT-3.1.4-1] [MQTT-3.1.4-2]
if code != packets.CodeSuccess {
if err := s.SendConnack(cl, code, false, nil); err != nil {
Expand Down
35 changes: 35 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,41 @@ func TestServerEstablishConnectionInvalidConnect(t *testing.T) {
_ = r.Close()
}

func TestEstablishConnectionMaximumClientsReached(t *testing.T) {
cc := NewDefaultServerCapabilities()
cc.MaximumClients = 0
s := New(&Options{
Logger: logger,
Capabilities: cc,
})
_ = s.AddHook(new(AllowHook), nil)
defer s.Close()

r, w := net.Pipe()
o := make(chan error)
go func() {
o <- s.EstablishConnection("tcp", r)
}()

go func() {
_, _ = w.Write(packets.TPacketData[packets.Connect].Get(packets.TConnectClean).RawBytes)
}()

// receive the connack
recv := make(chan []byte)
go func() {
buf, err := io.ReadAll(w)
require.NoError(t, err)
recv <- buf
}()

err := <-o
require.Error(t, err)
require.ErrorIs(t, err, packets.ErrServerBusy)

_ = r.Close()
}

// See https://github.com/mochi-mqtt/server/issues/178
func TestServerEstablishConnectionZeroByteUsernameIsValid(t *testing.T) {
s := newServer()
Expand Down
Loading