Skip to content

Commit

Permalink
Merge pull request #898 from nats-io/reload_mcl
Browse files Browse the repository at this point in the history
Support reload of max_control_line by updating connected clients
  • Loading branch information
derekcollison committed Feb 6, 2019
2 parents 46a2661 + 007c98d commit ce538b5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
11 changes: 8 additions & 3 deletions server/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,15 @@ type maxControlLineOption struct {
newValue int32
}

// Apply is a no-op because the max control line will be reloaded after options
// are applied
// Apply the setting by updating each client.
func (m *maxControlLineOption) Apply(server *Server) {
server.Noticef("Reloaded: max_control_line = %d", m.newValue)
mcl := int32(m.newValue)
server.mu.Lock()
for _, client := range server.clients {
atomic.StoreInt32(&client.mcl, mcl)
}
server.mu.Unlock()
server.Noticef("Reloaded: max_control_line = %d", mcl)
}

// maxPayloadOption implements the option interface for the `max_payload`
Expand Down
45 changes: 45 additions & 0 deletions server/reload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3593,3 +3593,48 @@ func TestConfigReloadBoolFlags(t *testing.T) {
})
}
}

func TestConfigReloadMaxControlLineWithClients(t *testing.T) {
server, opts, config := runReloadServerWithConfig(t, "./configs/reload/basic.conf")
defer os.Remove(config)
defer server.Shutdown()

// Ensure we can connect as a sanity check.
addr := fmt.Sprintf("nats://%s:%d", opts.Host, server.Addr().(*net.TCPAddr).Port)
nc, err := nats.Connect(addr)
if err != nil {
t.Fatalf("Error creating client: %v", err)
}
defer nc.Close()

// Now grab server's internal client that matches.
cid, _ := nc.GetClientID()
c := server.getClient(cid)
if c == nil {
t.Fatalf("Could not look up internal client")
}

// Check that we have the correct mcl snapshotted into the connected client.
getMcl := func(c *client) int32 {
c.mu.Lock()
defer c.mu.Unlock()
return c.mcl
}
if mcl := getMcl(c); mcl != opts.MaxControlLine {
t.Fatalf("Expected snapshot in client for mcl to be same as opts.MaxControlLine, got %d vs %d",
mcl, opts.MaxControlLine)
}

changeCurrentConfigContentWithNewContent(t, config, []byte("listen: 127.0.0.1:-1; max_control_line: 222"))
if err := server.Reload(); err != nil {
t.Fatalf("Expected Reload to succeed, got %v", err)
}

// Refresh properly.
opts = server.getOpts()

if mcl := getMcl(c); mcl != opts.MaxControlLine {
t.Fatalf("Expected snapshot in client for mcl to be same as new opts.MaxControlLine, got %d vs %d",
mcl, opts.MaxControlLine)
}
}

0 comments on commit ce538b5

Please sign in to comment.