fix concurrency problem with captureID
This commit is contained in:
parent
b92002c15d
commit
5d369f8971
4
main.go
4
main.go
|
@ -12,6 +12,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/googollee/go-socket.io"
|
"github.com/googollee/go-socket.io"
|
||||||
)
|
)
|
||||||
|
@ -88,6 +89,7 @@ func dashboardItemInfoHandler() http.Handler {
|
||||||
func proxyHandler(config Config) http.Handler {
|
func proxyHandler(config Config) http.Handler {
|
||||||
url, _ := url.Parse(config.TargetURL)
|
url, _ := url.Parse(config.TargetURL)
|
||||||
captureID := 0
|
captureID := 0
|
||||||
|
mux := sync.Mutex{}
|
||||||
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||||
req.Host = url.Host
|
req.Host = url.Host
|
||||||
req.URL.Host = url.Host
|
req.URL.Host = url.Host
|
||||||
|
@ -104,6 +106,7 @@ func proxyHandler(config Config) http.Handler {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not dump response: %v", err)
|
return fmt.Errorf("could not dump response: %v", err)
|
||||||
}
|
}
|
||||||
|
mux.Lock()
|
||||||
captureID++
|
captureID++
|
||||||
capture := Capture{
|
capture := Capture{
|
||||||
ID: captureID,
|
ID: captureID,
|
||||||
|
@ -115,6 +118,7 @@ func proxyHandler(config Config) http.Handler {
|
||||||
}
|
}
|
||||||
captures.Add(capture)
|
captures.Add(capture)
|
||||||
captures.RemoveLastAfterReaching(config.MaxCaptures)
|
captures.RemoveLastAfterReaching(config.MaxCaptures)
|
||||||
|
mux.Unlock()
|
||||||
emitToDashboard(captures)
|
emitToDashboard(captures)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
46
main_test.go
46
main_test.go
|
@ -6,10 +6,14 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Test the reverse proxy handler
|
// Test the reverse proxy handler
|
||||||
|
@ -152,6 +156,48 @@ func TestDumpResponseGzip(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCaptureIDConcurrence(t *testing.T) {
|
||||||
|
|
||||||
|
// This test bothers me
|
||||||
|
|
||||||
|
interactions := 1000
|
||||||
|
|
||||||
|
// Startup servers
|
||||||
|
service := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
|
||||||
|
rw.WriteHeader(http.StatusOK)
|
||||||
|
}))
|
||||||
|
capture := httptest.NewServer(proxyHandler(Config{TargetURL: service.URL, MaxCaptures: interactions}))
|
||||||
|
defer service.Close()
|
||||||
|
defer capture.Close()
|
||||||
|
|
||||||
|
// Starts go routines so that captureID is incremented concurrently within proxyHandler()
|
||||||
|
wg := &sync.WaitGroup{}
|
||||||
|
wg.Add(interactions)
|
||||||
|
for i := 0; i < interactions; i++ {
|
||||||
|
go func() {
|
||||||
|
_, err := http.Get(capture.URL)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Request Failed: %v", err)
|
||||||
|
}
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
// Tests if captures IDs are sequential
|
||||||
|
ids := make([]int, len(captures))
|
||||||
|
for i := 0; i < len(captures); i++ {
|
||||||
|
ids[i] = captures[i].ID
|
||||||
|
}
|
||||||
|
sort.Ints(ids)
|
||||||
|
for i := 0; i < len(captures); i++ {
|
||||||
|
if ids[i] != i+1 {
|
||||||
|
t.Fatalf("Capture IDs are not sequential")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func gzipStr(str string) string {
|
func gzipStr(str string) string {
|
||||||
var buff bytes.Buffer
|
var buff bytes.Buffer
|
||||||
g := gzip.NewWriter(&buff)
|
g := gzip.NewWriter(&buff)
|
||||||
|
|
Loading…
Reference in a new issue