mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-15 10:40:41 -07:00
[FIXED] Server panic when poll for Varz and others concurrently
Resolves #327
This commit is contained in:
@@ -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, "", " ")
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user