Fixed data races

This commit is contained in:
Eduard Urbach 2019-06-02 00:28:22 +09:00
parent 28db818c37
commit 4d919ef752
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
6 changed files with 46 additions and 41 deletions

6
go.mod
View File

@ -5,12 +5,12 @@ go 1.12
require (
cloud.google.com/go v0.39.0 // indirect
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/aerogo/aero v1.3.2
github.com/aerogo/aero v1.3.5
github.com/aerogo/api v0.2.0
github.com/aerogo/crawler v0.2.5
github.com/aerogo/graphql v0.4.0
github.com/aerogo/http v1.0.6
github.com/aerogo/layout v0.3.0
github.com/aerogo/layout v0.3.1
github.com/aerogo/log v0.2.5
github.com/aerogo/manifest v0.1.4
github.com/aerogo/markdown v0.1.8
@ -53,6 +53,4 @@ require (
gopkg.in/yaml.v2 v2.2.2 // indirect
)
replace github.com/aerogo/layout => /home/eduard/projects/aerogo/layout
exclude github.com/logpacker/PayPal-Go-SDK v2.0.0+incompatible

8
go.sum
View File

@ -11,8 +11,10 @@ github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUW
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/aerogo/aero v1.3.1 h1:P4XS4ePrJWSJwHLXKkX5YjFsoywhWuXf3dPIX5XVAW0=
github.com/aerogo/aero v1.3.1/go.mod h1:5rPhXo2DNMFQ7XhDsuZ3L7Zr6TH/349+WczUbrOUZvM=
github.com/aerogo/aero v1.3.2 h1:Sj/2U860HLJG5iq5het6QFgo/ERQ7cS4Zdz38xPWSwQ=
github.com/aerogo/aero v1.3.2/go.mod h1:5rPhXo2DNMFQ7XhDsuZ3L7Zr6TH/349+WczUbrOUZvM=
github.com/aerogo/aero v1.3.3 h1:7pBFEirK/iUpCG4ts6ljL9bu1PXBbybSIZII3VETGSU=
github.com/aerogo/aero v1.3.3/go.mod h1:5rPhXo2DNMFQ7XhDsuZ3L7Zr6TH/349+WczUbrOUZvM=
github.com/aerogo/aero v1.3.5 h1:XTf9p+caxq9IYL7G4MOg9aGZBKpPq2RaS/BvaKMIeIE=
github.com/aerogo/aero v1.3.5/go.mod h1:5rPhXo2DNMFQ7XhDsuZ3L7Zr6TH/349+WczUbrOUZvM=
github.com/aerogo/api v0.2.0 h1:mIc/y381e+Qc85eSc2cKPdpDDOmT0hlnEeCw2Dcf7no=
github.com/aerogo/api v0.2.0/go.mod h1:6objJn5XiKpYpywQUPrFjxZIXD4NVI2LwcBNYCEcS3Y=
github.com/aerogo/cluster v0.1.6 h1:9HYjJwo19uuh9thIc80T3caap9t9b4BXZ1iN8aztjlU=
@ -33,6 +35,8 @@ github.com/aerogo/http v1.0.3 h1:vf6A+Igme5OHQPaP3a00uPDS0oxsx3puMA23d1NsWDM=
github.com/aerogo/http v1.0.3/go.mod h1:B1igUmMLpE6KabMpc9reHCJJNUOJ2U/PR9s1fF3TpPQ=
github.com/aerogo/http v1.0.6 h1:+aswlcWlUxjVcokF8hUjNJmGIEZuhbFbHi8uSadEvtc=
github.com/aerogo/http v1.0.6/go.mod h1:LwJ7b+LjrHj60FhYQ586K3/O7aNGxkE2dy/exEkQ6rA=
github.com/aerogo/layout v0.3.1 h1:XLXYCU8vgifSZMIpM0m1Qk8UffyKg4KvhF/naHJI3mg=
github.com/aerogo/layout v0.3.1/go.mod h1:n+/yJdPnYWaNzBZccNiNn3WiRzoYgXYJPaowqhYftcM=
github.com/aerogo/linter-performance v1.0.3 h1:pYsmUd8jp6CVrFx+YNo9Gfdf222CKG2gCVjp8cljZNY=
github.com/aerogo/linter-performance v1.0.3/go.mod h1:po6XSSbSgR30lazzqSRGV++a2omxYr2qjqFvcvUCH40=
github.com/aerogo/log v0.2.5 h1:LGeElbLqyaD8r8Ls9HuG7tYF6YV4kP56IxJWl/b4cZQ=

View File

@ -15,6 +15,7 @@ import (
func TestRoutes(t *testing.T) {
t.Parallel()
app := configure(aero.New())
app.BindMiddleware()
// Iterate through every route
for _, examples := range routetests.All() {
@ -28,6 +29,7 @@ func TestRoutes(t *testing.T) {
func TestAnimePages(t *testing.T) {
t.Parallel()
app := configure(aero.New())
app.BindMiddleware()
for anime := range arn.StreamAnime() {
testRoute(t, app, anime.Link())
@ -37,6 +39,7 @@ func TestAnimePages(t *testing.T) {
func TestSoundTrackPages(t *testing.T) {
t.Parallel()
app := configure(aero.New())
app.BindMiddleware()
for soundtrack := range arn.StreamSoundTracks() {
testRoute(t, app, soundtrack.Link())
@ -47,6 +50,7 @@ func TestSoundTrackPages(t *testing.T) {
func TestAMVPages(t *testing.T) {
t.Parallel()
app := configure(aero.New())
app.BindMiddleware()
for amv := range arn.StreamAMVs() {
testRoute(t, app, amv.Link())
@ -57,6 +61,7 @@ func TestAMVPages(t *testing.T) {
func TestCompanyPages(t *testing.T) {
t.Parallel()
app := configure(aero.New())
app.BindMiddleware()
for company := range arn.StreamCompanies() {
testRoute(t, app, company.Link())
@ -66,6 +71,7 @@ func TestCompanyPages(t *testing.T) {
func TestThreadPages(t *testing.T) {
t.Parallel()
app := configure(aero.New())
app.BindMiddleware()
for thread := range arn.StreamThreads() {
testRoute(t, app, thread.Link())
@ -76,6 +82,7 @@ func TestThreadPages(t *testing.T) {
func TestPostPages(t *testing.T) {
t.Parallel()
app := configure(aero.New())
app.BindMiddleware()
for post := range arn.StreamPosts() {
testRoute(t, app, post.Link())
@ -86,6 +93,7 @@ func TestPostPages(t *testing.T) {
func TestQuotePages(t *testing.T) {
t.Parallel()
app := configure(aero.New())
app.BindMiddleware()
for quote := range arn.StreamQuotes() {
testRoute(t, app, quote.Link())
@ -103,17 +111,10 @@ func TestQuotePages(t *testing.T) {
// }
func testRoute(t *testing.T, app *aero.Application, route string) {
// Create a new HTTP request
request, err := http.NewRequest("GET", route, nil)
if err != nil {
t.Fatal(err)
}
// Record the response
responseRecorder := httptest.NewRecorder()
app.ServeHTTP(responseRecorder, request)
status := responseRecorder.Code
request := httptest.NewRequest("GET", route, nil)
response := httptest.NewRecorder()
app.ServeHTTP(response, request)
status := response.Code
switch status {
case 200, 302:

View File

@ -38,7 +38,7 @@ func Log(next aero.Handler) aero.Handler {
err := next(ctx)
responseTime := time.Since(start)
go logRequest(ctx, responseTime)
logRequest(ctx, responseTime)
return err
}
}

View File

@ -14,11 +14,11 @@ type OpenGraphContext struct {
// OpenGraph middleware modifies the context to be an OpenGraphContext.
func OpenGraph(next aero.Handler) aero.Handler {
return func(ctx aero.Context) error {
ctx = &OpenGraphContext{
openGraphCtx := &OpenGraphContext{
Context: ctx,
OpenGraph: nil,
}
return next(ctx)
return next(openGraphCtx)
}
}

View File

@ -32,9 +32,7 @@ func UserInfo(next aero.Handler) aero.Handler {
return nil
}
// This works asynchronously so it doesn't block the response
go updateUserInfo(ctx, user)
updateUserInfo(ctx, user)
return err
}
}
@ -59,12 +57,12 @@ func updateUserInfo(ctx aero.Context, user *arn.User) {
user.OS.Version = os.Version
}
if user.IP != newIP {
updateUserLocation(user, newIP)
}
user.LastSeen = arn.DateTimeUTC()
user.Save()
if user.IP != newIP {
go updateUserLocation(user, newIP)
}
}
// Updates the location of the user.
@ -100,19 +98,23 @@ func updateUserLocation(user *arn.User, newIP string) {
return
}
if newLocation.CountryName != "-" {
user.Location.CountryName = newLocation.CountryName
user.Location.CountryCode = newLocation.CountryCode
user.Location.Latitude, _ = strconv.ParseFloat(newLocation.Latitude, 64)
user.Location.Longitude, _ = strconv.ParseFloat(newLocation.Longitude, 64)
user.Location.CityName = newLocation.CityName
user.Location.RegionName = newLocation.RegionName
user.Location.TimeZone = newLocation.TimeZone
user.Location.ZipCode = newLocation.ZipCode
// Make South Korea easier to read
if user.Location.CountryName == "Korea, Republic of" {
user.Location.CountryName = "South Korea"
}
if newLocation.CountryName == "" || newLocation.CountryName == "-" {
return
}
user.Location.CountryName = newLocation.CountryName
user.Location.CountryCode = newLocation.CountryCode
user.Location.Latitude, _ = strconv.ParseFloat(newLocation.Latitude, 64)
user.Location.Longitude, _ = strconv.ParseFloat(newLocation.Longitude, 64)
user.Location.CityName = newLocation.CityName
user.Location.RegionName = newLocation.RegionName
user.Location.TimeZone = newLocation.TimeZone
user.Location.ZipCode = newLocation.ZipCode
// Make South Korea easier to read
if user.Location.CountryName == "Korea, Republic of" {
user.Location.CountryName = "South Korea"
}
user.Save()
}