-
Notifications
You must be signed in to change notification settings - Fork 64
Add fixes against race conditions #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mjeshtri
wants to merge
8
commits into
penglongli:master
Choose a base branch
from
mjeshtri:singleton
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
22b9f43
protect global monitor from race conditions
mjeshtri 20ede88
add github actions to automatically run tests on PR or when pushing …
mjeshtri e2af5de
add tests status reporting badge
mjeshtri f1a43d5
setup go
mjeshtri 19a6f34
wait until all goroutines are finished
mjeshtri 86c4d6d
make BloomFilter thread safe
mjeshtri f784a81
add tests for detecting data racing
mjeshtri 5dea4b6
Merge branch 'master' into singleton
mjeshtri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Run tests | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master ] | ||
|
||
jobs: | ||
|
||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
- uses: actions/setup-go@v2 | ||
with: | ||
go-version: '^1.14' | ||
- name: Test | ||
run: make test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
test: | ||
go test -v -race ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package ginmetrics_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
"github.com/penglongli/gin-metrics/ginmetrics" | ||
) | ||
|
||
func init() { | ||
gin.SetMode(gin.ReleaseMode) | ||
} | ||
|
||
func TestMiddlewareSamePort(t *testing.T) { | ||
r := newRouter() | ||
|
||
ts := httptest.NewServer(r) | ||
defer ts.Close() | ||
|
||
var wg sync.WaitGroup | ||
nLoops := 1000 | ||
wg.Add(nLoops) | ||
|
||
for i := 0; i < nLoops; i++ { | ||
go func(t *testing.T, i int) { | ||
res, err := http.Get(ts.URL + fmt.Sprintf("/product/%d", i)) | ||
if err != nil { | ||
t.Errorf("Expected nil, received %s", err.Error()) | ||
} | ||
|
||
if res.StatusCode != http.StatusOK { | ||
t.Errorf("Expected %d, received %d", http.StatusOK, res.StatusCode) | ||
} | ||
wg.Done() | ||
}(t, i) | ||
} | ||
|
||
wg.Wait() | ||
} | ||
|
||
func TestMiddlewareDifferentPort(t *testing.T) { | ||
appRouter, metricsRouter := newRouterSeparateMetrics() | ||
|
||
ts := httptest.NewServer(appRouter) | ||
tms := httptest.NewServer(metricsRouter) | ||
|
||
defer ts.Close() | ||
defer tms.Close() | ||
|
||
var wg sync.WaitGroup | ||
nLoops := 1000 | ||
wg.Add(nLoops) | ||
|
||
for i := 0; i < nLoops; i++ { | ||
go func(t *testing.T, i int) { | ||
res, err := http.Get(ts.URL + fmt.Sprintf("/product/%d", i)) | ||
if err != nil { | ||
t.Errorf("Expected nil, received %s", err.Error()) | ||
} | ||
|
||
if res.StatusCode != http.StatusOK { | ||
t.Errorf("Expected %d, received %d", http.StatusOK, res.StatusCode) | ||
} | ||
wg.Done() | ||
}(t, i) | ||
} | ||
|
||
wg.Wait() | ||
} | ||
|
||
func newRouterSeparateMetrics() (*gin.Engine, *gin.Engine) { | ||
appRouter := gin.New() | ||
metricRouter := gin.Default() | ||
|
||
m := ginmetrics.GetMonitor() | ||
m.UseWithoutExposingEndpoint(appRouter) | ||
m.Expose(metricRouter) | ||
|
||
appRouter.GET("/product/:id", func(ctx *gin.Context) { | ||
ctx.JSON(200, map[string]string{ | ||
"productId": ctx.Param("id"), | ||
}) | ||
}) | ||
|
||
return appRouter, metricRouter | ||
} | ||
|
||
func newRouter() *gin.Engine { | ||
r := gin.New() | ||
|
||
m := ginmetrics.GetMonitor() | ||
m.SetMetricPath("/metrics") | ||
m.SetSlowTime(10) | ||
m.SetDuration([]float64{0.1, 0.3, 1.2, 5, 10}) | ||
m.Use(r) | ||
|
||
r.GET("/product/:id", func(ctx *gin.Context) { | ||
ctx.JSON(200, map[string]string{ | ||
"productId": ctx.Param("id"), | ||
}) | ||
}) | ||
|
||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package ginmetrics | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestSingletonRacing(t *testing.T) { | ||
var wg sync.WaitGroup | ||
nLoops := 1000 | ||
wg.Add(nLoops) | ||
for i := 0; i < nLoops; i++ { | ||
go func() { | ||
GetMonitor() | ||
wg.Done() | ||
}() | ||
} | ||
|
||
wg.Wait() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can use RWMutex, then
bf.Contains
can usebf.mu.RLock()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran some benchmarks, and these are the results:
sync.Mutex
):sync.RWMutex
, wherebf.Contains()
is protected bybf.mu.RLock()
):Benchtest comparison:
As it can be seen the results are statistically indistinguishable. Increased the number of times benchmarks were run up to 25, and the result is still the same.
I suggest to keep it the way it is.