[FIXED] Server panic when poll for Varz and others concurrently

Resolves #327
This commit is contained in:
Ivan Kozlovic
2016-08-16 10:50:49 -06:00
parent 638e249d2a
commit e6039e0a8b
2 changed files with 43 additions and 1 deletions

View File

@@ -480,7 +480,12 @@ func (s *Server) HandleVarz(w http.ResponseWriter, r *http.Request) {
v.SlowConsumers = s.slowConsumers
v.Subscriptions = s.sl.Count()
s.httpReqStats[VarzPath]++
v.HTTPReqStats = s.httpReqStats
// Need a copy here since s.httpReqStas can change while doing
// the marshaling down below.
v.HTTPReqStats = make(map[string]uint64, len(s.httpReqStats))
for key, val := range s.httpReqStats {
v.HTTPReqStats[key] = val
}
s.mu.Unlock()
b, err := json.MarshalIndent(v, "", " ")

View File

@@ -13,6 +13,7 @@ import (
"time"
"github.com/nats-io/nats"
"sync"
)
const CLIENT_PORT = 11224
@@ -1283,3 +1284,39 @@ func TestStacksz(t *testing.T) {
}
defer respj.Body.Close()
}
func TestConcurrentMonitoring(t *testing.T) {
s := runMonitorServer()
defer s.Shutdown()
url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT)
// Get some endpoints. Make sure we have at least varz,
// and the more the merrier.
endpoints := []string{"varz", "varz", "varz", "connz", "connz", "subsz", "subsz", "routez", "routez"}
wg := &sync.WaitGroup{}
wg.Add(len(endpoints))
for _, e := range endpoints {
go func(endpoint string) {
defer wg.Done()
for i := 0; i < 150; i++ {
resp, err := http.Get(url + endpoint)
if err != nil {
t.Fatalf("Expected no error: Got %v\n", err)
}
if resp.StatusCode != 200 {
t.Fatalf("Expected a 200 response, got %d\n", resp.StatusCode)
}
ct := resp.Header.Get("Content-Type")
if ct != "application/json" {
t.Fatalf("Expected application/json content-type, got %s\n", ct)
}
defer resp.Body.Close()
if _, err := ioutil.ReadAll(resp.Body); err != nil {
t.Fatalf("Got an error reading the body: %v\n", err)
}
resp.Body.Close()
}
}(e)
}
wg.Wait()
}