Skip to content

Commit 184a017

Browse files
authored
Improved task queue (#37)
Improve task queue
1 parent e866f0d commit 184a017

10 files changed

+220
-127
lines changed

docs/openapi.json

+15
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
}
3131
},
3232
"responses": {
33+
"202": {
34+
"description": "Data is being fetched. Please try again later."
35+
},
3336
"400": {
3437
"description": "Bad Request. A required query parameter is either missing or in a wrong format."
3538
},
@@ -310,6 +313,9 @@
310313
}
311314
}
312315
},
316+
"202": {
317+
"$ref": "#/components/responses/202"
318+
},
313319
"400": {
314320
"$ref": "#/components/responses/400"
315321
},
@@ -435,6 +441,9 @@
435441
}
436442
}
437443
},
444+
"202": {
445+
"$ref": "#/components/responses/202"
446+
},
438447
"400": {
439448
"$ref": "#/components/responses/400"
440449
},
@@ -539,6 +548,9 @@
539548
}
540549
}
541550
},
551+
"202": {
552+
"$ref": "#/components/responses/202"
553+
},
542554
"400": {
543555
"$ref": "#/components/responses/400"
544556
},
@@ -633,6 +645,9 @@
633645
}
634646
}
635647
},
648+
"202": {
649+
"$ref": "#/components/responses/202"
650+
},
636651
"400": {
637652
"$ref": "#/components/responses/400"
638653
},

handlers/ListenAndServe.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ func ListenAndServe() {
2929
srv := &http.Server{
3030
Addr: fmt.Sprintf("0.0.0.0:%v", config.GetPort()),
3131
Handler: middlewareStack(mux),
32-
IdleTimeout: 60 * time.Second,
33-
ReadTimeout: 15 * time.Second,
34-
WriteTimeout: 15 * time.Second,
32+
IdleTimeout: 30 * time.Second,
33+
ReadTimeout: 10 * time.Second,
34+
WriteTimeout: 10 * time.Second,
3535
}
3636

3737
logger.Error(srv.ListenAndServe().Error())

handlers/getAdventurer.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,34 @@ func getAdventurer(w http.ResponseWriter, r *http.Request) {
2222
return
2323
}
2424

25-
data, status, date, expires, found := cache.Profiles.GetRecord([]string{region, profileTarget})
26-
if !found {
27-
taskId, maintenance := scraper.EnqueueAdventurer(region, profileTarget)
28-
29-
if maintenance {
30-
w.WriteHeader(http.StatusServiceUnavailable)
31-
return
25+
if data, status, date, expires, ok := cache.Profiles.GetRecord([]string{region, profileTarget}); ok {
26+
w.Header().Set("Date", date)
27+
w.Header().Set("Expires", expires)
28+
29+
if status == http.StatusOK {
30+
json.NewEncoder(w).Encode(data)
31+
} else {
32+
w.WriteHeader(status)
3233
}
3334

34-
data, status, date, expires = cache.Profiles.WaitForRecord(taskId)
35+
return
3536
}
3637

37-
w.Header().Set("Date", date)
38-
w.Header().Set("Expires", expires)
38+
if ok := giveMaintenanceResponse(w, region); ok {
39+
return
40+
}
3941

40-
if status == http.StatusOK {
41-
json.NewEncoder(w).Encode(data)
42-
} else {
43-
w.WriteHeader(status)
42+
if tasksQuantityExceeded := scraper.EnqueueAdventurer(r.Header.Get("CF-Connecting-IP"), region, profileTarget); tasksQuantityExceeded {
43+
w.WriteHeader(http.StatusTooManyRequests)
44+
json.NewEncoder(w).Encode(map[string]string{
45+
"message": "You have exceeded the maximum number of concurrent tasks.",
46+
})
47+
48+
return
4449
}
50+
51+
w.WriteHeader(http.StatusAccepted)
52+
json.NewEncoder(w).Encode(map[string]string{
53+
"message": "Player profile is being fetched. Please try again later.",
54+
})
4555
}

handlers/getAdventurerSearch.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,34 @@ func getAdventurerSearch(w http.ResponseWriter, r *http.Request) {
2525
searchTypeQueryParam := r.URL.Query()["searchType"]
2626
searchType := validators.ValidateSearchTypeQueryParam(searchTypeQueryParam)
2727

28-
data, status, date, expires, found := cache.ProfileSearch.GetRecord([]string{region, query, searchType})
29-
if !found {
30-
taskId, maintenance := scraper.EnqueueAdventurerSearch(region, query, searchType)
31-
32-
if maintenance {
33-
w.WriteHeader(http.StatusServiceUnavailable)
34-
return
28+
if data, status, date, expires, ok := cache.ProfileSearch.GetRecord([]string{region, query, searchType}); ok {
29+
w.Header().Set("Date", date)
30+
w.Header().Set("Expires", expires)
31+
32+
if status == http.StatusOK {
33+
json.NewEncoder(w).Encode(data)
34+
} else {
35+
w.WriteHeader(status)
3536
}
3637

37-
data, status, date, expires = cache.ProfileSearch.WaitForRecord(taskId)
38+
return
3839
}
3940

40-
w.Header().Set("Date", date)
41-
w.Header().Set("Expires", expires)
41+
if ok := giveMaintenanceResponse(w, region); ok {
42+
return
43+
}
4244

43-
if status == http.StatusOK {
44-
json.NewEncoder(w).Encode(data)
45-
} else {
46-
w.WriteHeader(status)
45+
if tasksQuantityExceeded := scraper.EnqueueAdventurerSearch(r.Header.Get("CF-Connecting-IP"), region, query, searchType); tasksQuantityExceeded {
46+
w.WriteHeader(http.StatusTooManyRequests)
47+
json.NewEncoder(w).Encode(map[string]string{
48+
"message": "You have exceeded the maximum number of concurrent tasks.",
49+
})
50+
51+
return
4752
}
53+
54+
w.WriteHeader(http.StatusAccepted)
55+
json.NewEncoder(w).Encode(map[string]string{
56+
"message": "Player search is being fetched. Please try again later.",
57+
})
4858
}

handlers/getGuild.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,34 @@ func getGuild(w http.ResponseWriter, r *http.Request) {
2222
return
2323
}
2424

25-
data, status, date, expires, found := cache.GuildProfiles.GetRecord([]string{region, name})
26-
if !found {
27-
taskId, maintenance := scraper.EnqueueGuild(region, name)
28-
29-
if maintenance {
30-
w.WriteHeader(http.StatusServiceUnavailable)
31-
return
25+
if data, status, date, expires, ok := cache.GuildProfiles.GetRecord([]string{region, name}); ok {
26+
w.Header().Set("Date", date)
27+
w.Header().Set("Expires", expires)
28+
29+
if status == http.StatusOK {
30+
json.NewEncoder(w).Encode(data)
31+
} else {
32+
w.WriteHeader(status)
3233
}
3334

34-
data, status, date, expires = cache.GuildProfiles.WaitForRecord(taskId)
35+
return
3536
}
3637

37-
w.Header().Set("Date", date)
38-
w.Header().Set("Expires", expires)
38+
if ok := giveMaintenanceResponse(w, region); ok {
39+
return
40+
}
3941

40-
if status == http.StatusOK {
41-
json.NewEncoder(w).Encode(data)
42-
} else {
43-
w.WriteHeader(status)
42+
if tasksQuantityExceeded := scraper.EnqueueGuild(r.Header.Get("CF-Connecting-IP"), region, name); tasksQuantityExceeded {
43+
w.WriteHeader(http.StatusTooManyRequests)
44+
json.NewEncoder(w).Encode(map[string]string{
45+
"message": "You have exceeded the maximum number of concurrent tasks.",
46+
})
47+
48+
return
4449
}
50+
51+
w.WriteHeader(http.StatusAccepted)
52+
json.NewEncoder(w).Encode(map[string]string{
53+
"message": "Guild profile is being fetched. Please try again later.",
54+
})
4555
}

handlers/getGuildSearch.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,34 @@ func getGuildSearch(w http.ResponseWriter, r *http.Request) {
2222
return
2323
}
2424

25-
data, status, date, expires, found := cache.GuildSearch.GetRecord([]string{region, name})
26-
if !found {
27-
taskId, maintenance := scraper.EnqueueGuildSearch(region, name)
28-
29-
if maintenance {
30-
w.WriteHeader(http.StatusServiceUnavailable)
31-
return
25+
if data, status, date, expires, ok := cache.GuildSearch.GetRecord([]string{region, name}); ok {
26+
w.Header().Set("Date", date)
27+
w.Header().Set("Expires", expires)
28+
29+
if status == http.StatusOK {
30+
json.NewEncoder(w).Encode(data)
31+
} else {
32+
w.WriteHeader(status)
3233
}
3334

34-
data, status, date, expires = cache.GuildSearch.WaitForRecord(taskId)
35+
return
3536
}
3637

37-
w.Header().Set("Date", date)
38-
w.Header().Set("Expires", expires)
38+
if ok := giveMaintenanceResponse(w, region); ok {
39+
return
40+
}
3941

40-
if status == http.StatusOK {
41-
json.NewEncoder(w).Encode(data)
42-
} else {
43-
w.WriteHeader(status)
42+
if tasksQuantityExceeded := scraper.EnqueueGuildSearch(r.Header.Get("CF-Connecting-IP"), region, name); tasksQuantityExceeded {
43+
w.WriteHeader(http.StatusTooManyRequests)
44+
json.NewEncoder(w).Encode(map[string]string{
45+
"message": "You have exceeded the maximum number of concurrent tasks.",
46+
})
47+
48+
return
4449
}
50+
51+
w.WriteHeader(http.StatusAccepted)
52+
json.NewEncoder(w).Encode(map[string]string{
53+
"message": "Guild search is being fetched. Please try again later.",
54+
})
4555
}

handlers/getStatus.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
var initTime = time.Now()
14-
var version = "1.10.0"
14+
var version = "1.10.1"
1515

1616
func getStatus(w http.ResponseWriter, r *http.Request) {
1717
json.NewEncoder(w).Encode(map[string]interface{}{

handlers/giveMaintenanceResponse.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package handlers
2+
3+
import (
4+
"net/http"
5+
"time"
6+
7+
"bdo-rest-api/scraper"
8+
"bdo-rest-api/utils"
9+
)
10+
11+
func giveMaintenanceResponse(w http.ResponseWriter, region string) (ok bool) {
12+
isCloseTime, expires := scraper.GetCloseTime(region)
13+
14+
if !isCloseTime {
15+
return false
16+
}
17+
18+
w.Header().Set("Date", utils.FormatDateForHeaders(time.Now()))
19+
w.Header().Set("Expires", utils.FormatDateForHeaders(expires))
20+
w.WriteHeader(http.StatusServiceUnavailable)
21+
return true
22+
}

0 commit comments

Comments
 (0)