mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-02 03:38:42 -07:00
Reduce startup memory for gateways
Similar to #956 but for gateways code. Also fixing route test TestLargeClusterMem. Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
This commit is contained in:
@@ -673,7 +673,7 @@ func (s *Server) createGateway(cfg *gatewayCfg, url *url.URL, conn net.Conn) {
|
||||
}
|
||||
|
||||
// Do final client initialization
|
||||
c.in.pacache = make(map[string]*perAccountCache, maxPerAccountCacheSize)
|
||||
c.in.pacache = make(map[string]*perAccountCache)
|
||||
if solicit {
|
||||
// This is an outbound gateway connection
|
||||
c.gw.outsim = &sync.Map{}
|
||||
|
||||
@@ -4433,3 +4433,47 @@ func TestGatewayServiceExportWithWildcards(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGatewayMemUsage(t *testing.T) {
|
||||
// Try to clean up.
|
||||
runtime.GC()
|
||||
var m runtime.MemStats
|
||||
runtime.ReadMemStats(&m)
|
||||
pta := m.TotalAlloc
|
||||
|
||||
o := testDefaultOptionsForGateway("A")
|
||||
s := runGatewayServer(o)
|
||||
defer s.Shutdown()
|
||||
|
||||
var servers []*Server
|
||||
servers = append(servers, s)
|
||||
|
||||
numServers := 10
|
||||
for i := 0; i < numServers; i++ {
|
||||
rn := fmt.Sprintf("RG_%d", i+1)
|
||||
o := testGatewayOptionsFromToWithServers(t, rn, "A", s)
|
||||
s := runGatewayServer(o)
|
||||
defer s.Shutdown()
|
||||
servers = append(servers, s)
|
||||
}
|
||||
|
||||
// Each server should have an outbound
|
||||
for _, s := range servers {
|
||||
waitForOutboundGateways(t, s, numServers, 2*time.Second)
|
||||
}
|
||||
// The first started server should have numServers inbounds (since
|
||||
// they all connect to it).
|
||||
waitForInboundGateways(t, s, numServers, 2*time.Second)
|
||||
|
||||
// Calculate in MB what we are using now.
|
||||
const max = 50 * 1024 * 1024 // 50MB
|
||||
runtime.ReadMemStats(&m)
|
||||
used := m.TotalAlloc - pta
|
||||
if used > max {
|
||||
t.Fatalf("Cluster using too much memory, expect < 50MB, got %dMB", used/(1024*1024))
|
||||
}
|
||||
|
||||
for _, s := range servers {
|
||||
s.Shutdown()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1629,7 +1629,7 @@ func TestLargeClusterMem(t *testing.T) {
|
||||
rurl, _ := url.Parse(routeAddr)
|
||||
routes := []*url.URL{rurl}
|
||||
|
||||
numServers := 25
|
||||
numServers := 15
|
||||
|
||||
for i := 1; i < numServers; i++ {
|
||||
o := opts()
|
||||
@@ -1642,9 +1642,9 @@ func TestLargeClusterMem(t *testing.T) {
|
||||
// Calculate in MB what we are using now.
|
||||
const max = 50 * 1024 * 1024 // 50MB
|
||||
runtime.ReadMemStats(&m)
|
||||
used := (m.TotalAlloc - pta) / (1024 * 1024)
|
||||
used := m.TotalAlloc - pta
|
||||
if used > max {
|
||||
t.Fatalf("Cluster using too much memory, expect < 50MB, got %dMB", used)
|
||||
t.Fatalf("Cluster using too much memory, expect < 50MB, got %dMB", used/(1024*1024))
|
||||
}
|
||||
|
||||
for _, s := range servers {
|
||||
|
||||
Reference in New Issue
Block a user