Skip to content

Commit

Permalink
[FIXED] Server panic when poll for Varz and others concurrently
Browse files Browse the repository at this point in the history
Resolves #327
  • Loading branch information
kozlovic committed Aug 16, 2016
1 parent 638e249 commit e6039e0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
7 changes: 6 additions & 1 deletion server/monitor.go
Expand Up @@ -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, "", " ")
Expand Down
37 changes: 37 additions & 0 deletions server/monitor_test.go
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/nats-io/nats"
"sync"
)

const CLIENT_PORT = 11224
Expand Down Expand Up @@ -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()
}

0 comments on commit e6039e0

Please sign in to comment.