Skip to content

Commit 83f3161

Browse files
authored
Add a header for the number of queued tasks for client (#63)
1 parent 5a32108 commit 83f3161

5 files changed

Lines changed: 30 additions & 17 deletions

File tree

handlers/getAdventurer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package handlers
33
import (
44
"encoding/json"
55
"net/http"
6+
"strconv"
67

78
"bdo-rest-api/cache"
89
"bdo-rest-api/scraper"
@@ -43,7 +44,9 @@ func getAdventurer(w http.ResponseWriter, r *http.Request) {
4344
return
4445
}
4546

46-
if taskAdded, tasksQuantityExceeded := scraper.EnqueueAdventurer(r.Header.Get("CF-Connecting-IP"), region, profileTarget); tasksQuantityExceeded {
47+
ok, tasksExceeded, tasksNumber := scraper.EnqueueAdventurer(r.Header.Get("CF-Connecting-IP"), region, profileTarget)
48+
w.Header().Set("X-Tasks-Number", strconv.Itoa(tasksNumber))
49+
if tasksExceeded {
4750
w.WriteHeader(http.StatusTooManyRequests)
4851
json.NewEncoder(w).Encode(map[string]string{
4952
"message": "You have exceeded the maximum number of concurrent tasks.",
@@ -56,7 +59,7 @@ func getAdventurer(w http.ResponseWriter, r *http.Request) {
5659
"status": map[bool]string{
5760
true: "started",
5861
false: "pending",
59-
}[taskAdded],
62+
}[ok],
6063
})
6164
}
6265
}

handlers/getAdventurerSearch.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package handlers
33
import (
44
"encoding/json"
55
"net/http"
6+
"strconv"
67

78
"bdo-rest-api/cache"
89
"bdo-rest-api/scraper"
@@ -46,7 +47,9 @@ func getAdventurerSearch(w http.ResponseWriter, r *http.Request) {
4647
return
4748
}
4849

49-
if taskAdded, tasksQuantityExceeded := scraper.EnqueueAdventurerSearch(r.Header.Get("CF-Connecting-IP"), region, query, searchType); tasksQuantityExceeded {
50+
ok, tasksExceeded, tasksNumber := scraper.EnqueueAdventurerSearch(r.Header.Get("CF-Connecting-IP"), region, query, searchType)
51+
w.Header().Set("X-Tasks-Number", strconv.Itoa(tasksNumber))
52+
if tasksExceeded {
5053
w.WriteHeader(http.StatusTooManyRequests)
5154
json.NewEncoder(w).Encode(map[string]string{
5255
"message": "You have exceeded the maximum number of concurrent tasks.",
@@ -59,7 +62,7 @@ func getAdventurerSearch(w http.ResponseWriter, r *http.Request) {
5962
"status": map[bool]string{
6063
true: "started",
6164
false: "pending",
62-
}[taskAdded],
65+
}[ok],
6366
})
6467
}
6568
}

handlers/getGuild.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package handlers
33
import (
44
"encoding/json"
55
"net/http"
6+
"strconv"
67

78
"bdo-rest-api/cache"
89
"bdo-rest-api/scraper"
@@ -43,7 +44,9 @@ func getGuild(w http.ResponseWriter, r *http.Request) {
4344
return
4445
}
4546

46-
if taskAdded, tasksQuantityExceeded := scraper.EnqueueGuild(r.Header.Get("CF-Connecting-IP"), region, name); tasksQuantityExceeded {
47+
ok, tasksExceeded, tasksNumber := scraper.EnqueueGuild(r.Header.Get("CF-Connecting-IP"), region, name)
48+
w.Header().Set("X-Tasks-Number", strconv.Itoa(tasksNumber))
49+
if tasksExceeded {
4750
w.WriteHeader(http.StatusTooManyRequests)
4851
json.NewEncoder(w).Encode(map[string]string{
4952
"message": "You have exceeded the maximum number of concurrent tasks.",
@@ -56,7 +59,7 @@ func getGuild(w http.ResponseWriter, r *http.Request) {
5659
"status": map[bool]string{
5760
true: "started",
5861
false: "pending",
59-
}[taskAdded],
62+
}[ok],
6063
})
6164
}
6265
}

handlers/getGuildSearch.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package handlers
33
import (
44
"encoding/json"
55
"net/http"
6+
"strconv"
67

78
"bdo-rest-api/cache"
89
"bdo-rest-api/scraper"
@@ -43,7 +44,9 @@ func getGuildSearch(w http.ResponseWriter, r *http.Request) {
4344
return
4445
}
4546

46-
if taskAdded, tasksQuantityExceeded := scraper.EnqueueGuildSearch(r.Header.Get("CF-Connecting-IP"), region, name); tasksQuantityExceeded {
47+
ok, tasksExceeded, tasksNumber := scraper.EnqueueGuildSearch(r.Header.Get("CF-Connecting-IP"), region, name)
48+
w.Header().Set("X-Tasks-Number", strconv.Itoa(tasksNumber))
49+
if tasksExceeded {
4750
w.WriteHeader(http.StatusTooManyRequests)
4851
json.NewEncoder(w).Encode(map[string]string{
4952
"message": "You have exceeded the maximum number of concurrent tasks.",
@@ -56,7 +59,7 @@ func getGuildSearch(w http.ResponseWriter, r *http.Request) {
5659
"status": map[bool]string{
5760
true: "started",
5861
false: "pending",
59-
}[taskAdded],
62+
}[ok],
6063
})
6164
}
6265
}

scraper/scraper.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,14 @@ func InitScraper() {
116116
})
117117
}
118118

119-
func createTask(clientIP, region, taskType string, query map[string]string) (taskAdded, tasksQuantityExceeded bool) {
119+
func createTask(clientIP, region, taskType string, query map[string]string) (ok, tasksExceeded bool, tasksNumber int) {
120120
crc32 := crc32.NewIEEE()
121121
crc32.Write([]byte(strings.Join(append(slices.Sorted(maps.Values(query)), region, taskType), "")))
122122
hashString := strconv.Itoa(int(crc32.Sum32()))
123123

124-
if taskQueue.CountQueuedTasksForClient(clientIP) >= viper.GetInt("maxtasksperclient") {
125-
return false, true
124+
tasksN := taskQueue.CountQueuedTasksForClient(clientIP)
125+
if tasksN >= viper.GetInt("maxtasksperclient") {
126+
return false, true, tasksN
126127
}
127128

128129
url := fmt.Sprintf(
@@ -147,17 +148,17 @@ func createTask(clientIP, region, taskType string, query map[string]string) (tas
147148
"taskType": taskType,
148149
})
149150

150-
added := taskQueue.AddTask(clientIP, hashString, utils.BuildRequest(url, query))
151-
return added, false
151+
ok = taskQueue.AddTask(clientIP, hashString, utils.BuildRequest(url, query))
152+
return ok, false, map[bool]int{true: tasksN + 1, false: tasksN}[ok]
152153
}
153154

154-
func EnqueueAdventurer(clientIP, region, profileTarget string) (taskAdded, tasksQuantityExceeded bool) {
155+
func EnqueueAdventurer(clientIP, region, profileTarget string) (ok, tasksExceeded bool, tasksNumber int) {
155156
return createTask(clientIP, region, "player", map[string]string{
156157
"profileTarget": profileTarget,
157158
})
158159
}
159160

160-
func EnqueueAdventurerSearch(clientIP, region, query, searchType string) (taskAdded, tasksQuantityExceeded bool) {
161+
func EnqueueAdventurerSearch(clientIP, region, query, searchType string) (ok, tasksExceeded bool, tasksNumber int) {
161162
return createTask(clientIP, region, "playerSearch", map[string]string{
162163
"Page": "1",
163164
"region": region,
@@ -166,14 +167,14 @@ func EnqueueAdventurerSearch(clientIP, region, query, searchType string) (taskAd
166167
})
167168
}
168169

169-
func EnqueueGuild(clientIP, region, name string) (taskAdded, tasksQuantityExceeded bool) {
170+
func EnqueueGuild(clientIP, region, name string) (ok, tasksExceeded bool, tasksNumber int) {
170171
return createTask(clientIP, region, "guild", map[string]string{
171172
"guildName": name,
172173
"region": region,
173174
})
174175
}
175176

176-
func EnqueueGuildSearch(clientIP, region, query string) (taskAdded, tasksQuantityExceeded bool) {
177+
func EnqueueGuildSearch(clientIP, region, query string) (ok, tasksExceeded bool, tasksNumber int) {
177178
return createTask(clientIP, region, "guildSearch", map[string]string{
178179
"page": "1",
179180
"region": region,

0 commit comments

Comments
 (0)