diff --git a/main.go b/main.go index f762b183..82c60626 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,4 @@ -// Copyright 2012-2019 The NATS Authors +// Copyright 2012-2021 The NATS Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at diff --git a/server/accounts.go b/server/accounts.go index 73479dfa..db5d6e52 100644 --- a/server/accounts.go +++ b/server/accounts.go @@ -278,6 +278,8 @@ func (a *Account) shallowCopy() *Account { } // JetStream na.jsLimits = a.jsLimits + // Server config account limits. + na.limits = a.limits return na } diff --git a/server/accounts_test.go b/server/accounts_test.go index 0b952fe6..8b150e36 100644 --- a/server/accounts_test.go +++ b/server/accounts_test.go @@ -3377,3 +3377,44 @@ func TestImportSubscriptionPartialOverlapWithTransform(t *testing.T) { }) } } + +func TestAccountLimitsServerConfig(t *testing.T) { + cf := createConfFile(t, []byte(` + max_connections: 10 + accounts { + MAXC { + users = [{user: derek, password: foo}] + limits { + max_connections: 5 + max_subs: 10 + max_payload: 32k + max_leafnodes: 1 + } + } + } + `)) + defer removeFile(t, cf) + + s, _ := RunServerWithConfig(cf) + defer s.Shutdown() + + acc, err := s.lookupAccount("MAXC") + require_NoError(t, err) + + if mc := acc.MaxActiveConnections(); mc != 5 { + t.Fatalf("Did not set MaxActiveConnections properly, expected 5, got %d", mc) + } + if mlc := acc.MaxActiveLeafNodes(); mlc != 1 { + t.Fatalf("Did not set MaxActiveLeafNodes properly, expected 1, got %d", mlc) + } + + // Do quick test on connections, but if they are registered above should be good. + for i := 0; i < 5; i++ { + _, err := nats.Connect(s.ClientURL(), nats.UserInfo("derek", "foo")) + require_NoError(t, err) + } + + // Should fail. + _, err = nats.Connect(s.ClientURL(), nats.UserInfo("derek", "foo")) + require_Error(t, err) +} diff --git a/server/opts.go b/server/opts.go index 4489af23..7e05afe2 100644 --- a/server/opts.go +++ b/server/opts.go @@ -2315,6 +2315,39 @@ func parseAccountMappings(v interface{}, acc *Account, errors *[]error, warnings return nil } +// parseAccountLimits is called to parse account limits in a server config. +func parseAccountLimits(mv interface{}, acc *Account, errors *[]error, warnings *[]error) error { + var lt token + defer convertPanicToErrorList(<, errors) + + tk, v := unwrapValue(mv, <) + am, ok := v.(map[string]interface{}) + if !ok { + return &configErr{tk, fmt.Sprintf("Expected account limits to be a map/struct, got %+v", v)} + } + + for k, v := range am { + tk, mv = unwrapValue(v, <) + switch strings.ToLower(k) { + case "max_connections", "max_conn": + acc.mconns = int32(mv.(int64)) + case "max_subscriptions", "max_subs": + acc.msubs = int32(mv.(int64)) + case "max_payload", "max_pay": + acc.mpay = int32(mv.(int64)) + case "max_leafnodes", "max_leafs": + acc.mleafs = int32(mv.(int64)) + default: + if !tk.IsUsedVariable() { + err := &configErr{tk, fmt.Sprintf("Unknown field %q parsing account limits", k)} + *errors = append(*errors, err) + } + } + } + + return nil +} + // parseAccounts will parse the different accounts syntax. func parseAccounts(v interface{}, opts *Options, errors *[]error, warnings *[]error) error { var ( @@ -2435,6 +2468,12 @@ func parseAccounts(v interface{}, opts *Options, errors *[]error, warnings *[]er *errors = append(*errors, err) continue } + case "limits": + err := parseAccountLimits(tk, acc, errors, warnings) + if err != nil { + *errors = append(*errors, err) + continue + } default: if !tk.IsUsedVariable() { err := &unknownConfigFieldErr{ @@ -2574,7 +2613,7 @@ func parseAccounts(v interface{}, opts *Options, errors *[]error, warnings *[]er *errors = append(*errors, &configErr{tk, msg}) continue } - if service.to == "" { + if service.to == _EMPTY_ { service.to = service.sub } if err := service.acc.AddServiceImport(ta, service.to, service.sub); err != nil { diff --git a/server/server.go b/server/server.go index c3f7ff49..2987774a 100644 --- a/server/server.go +++ b/server/server.go @@ -1,4 +1,4 @@ -// Copyright 2012-2020 The NATS Authors +// Copyright 2012-2021 The NATS Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at