From 5c1d1f7a6819a79b429daf8553dfeef178f936e1 Mon Sep 17 00:00:00 2001
From: man90 <man90@tuta.io>
Date: Mon, 19 Jan 2026 21:26:40 +0100
Subject: [PATCH] Add a header for the number of queued tasks

---
 handlers/getAdventurer.go       |  7 +++++--
 handlers/getAdventurerSearch.go |  7 +++++--
 handlers/getGuild.go            |  7 +++++--
 handlers/getGuildSearch.go      |  7 +++++--
 scraper/scraper.go              | 19 ++++++++++---------
 5 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/handlers/getAdventurer.go b/handlers/getAdventurer.go
index b8a4dcb..084c48d 100644
--- a/handlers/getAdventurer.go
+++ b/handlers/getAdventurer.go
@@ -3,6 +3,7 @@ package handlers
 import (
 	"encoding/json"
 	"net/http"
+	"strconv"
 
 	"bdo-rest-api/cache"
 	"bdo-rest-api/scraper"
@@ -43,7 +44,9 @@ func getAdventurer(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	if taskAdded, tasksQuantityExceeded := scraper.EnqueueAdventurer(r.Header.Get("CF-Connecting-IP"), region, profileTarget); tasksQuantityExceeded {
+	ok, tasksExceeded, tasksNumber := scraper.EnqueueAdventurer(r.Header.Get("CF-Connecting-IP"), region, profileTarget)
+	w.Header().Set("X-Tasks-Number", strconv.Itoa(tasksNumber))
+	if tasksExceeded {
 		w.WriteHeader(http.StatusTooManyRequests)
 		json.NewEncoder(w).Encode(map[string]string{
 			"message": "You have exceeded the maximum number of concurrent tasks.",
@@ -56,7 +59,7 @@ func getAdventurer(w http.ResponseWriter, r *http.Request) {
 			"status": map[bool]string{
 				true:  "started",
 				false: "pending",
-			}[taskAdded],
+			}[ok],
 		})
 	}
 }
diff --git a/handlers/getAdventurerSearch.go b/handlers/getAdventurerSearch.go
index fe28d93..5f107b6 100644
--- a/handlers/getAdventurerSearch.go
+++ b/handlers/getAdventurerSearch.go
@@ -3,6 +3,7 @@ package handlers
 import (
 	"encoding/json"
 	"net/http"
+	"strconv"
 
 	"bdo-rest-api/cache"
 	"bdo-rest-api/scraper"
@@ -46,7 +47,9 @@ func getAdventurerSearch(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	if taskAdded, tasksQuantityExceeded := scraper.EnqueueAdventurerSearch(r.Header.Get("CF-Connecting-IP"), region, query, searchType); tasksQuantityExceeded {
+	ok, tasksExceeded, tasksNumber := scraper.EnqueueAdventurerSearch(r.Header.Get("CF-Connecting-IP"), region, query, searchType)
+	w.Header().Set("X-Tasks-Number", strconv.Itoa(tasksNumber))
+	if tasksExceeded {
 		w.WriteHeader(http.StatusTooManyRequests)
 		json.NewEncoder(w).Encode(map[string]string{
 			"message": "You have exceeded the maximum number of concurrent tasks.",
@@ -59,7 +62,7 @@ func getAdventurerSearch(w http.ResponseWriter, r *http.Request) {
 			"status": map[bool]string{
 				true:  "started",
 				false: "pending",
-			}[taskAdded],
+			}[ok],
 		})
 	}
 }
diff --git a/handlers/getGuild.go b/handlers/getGuild.go
index a84c984..a969d0a 100644
--- a/handlers/getGuild.go
+++ b/handlers/getGuild.go
@@ -3,6 +3,7 @@ package handlers
 import (
 	"encoding/json"
 	"net/http"
+	"strconv"
 
 	"bdo-rest-api/cache"
 	"bdo-rest-api/scraper"
@@ -43,7 +44,9 @@ func getGuild(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	if taskAdded, tasksQuantityExceeded := scraper.EnqueueGuild(r.Header.Get("CF-Connecting-IP"), region, name); tasksQuantityExceeded {
+	ok, tasksExceeded, tasksNumber := scraper.EnqueueGuild(r.Header.Get("CF-Connecting-IP"), region, name)
+	w.Header().Set("X-Tasks-Number", strconv.Itoa(tasksNumber))
+	if tasksExceeded {
 		w.WriteHeader(http.StatusTooManyRequests)
 		json.NewEncoder(w).Encode(map[string]string{
 			"message": "You have exceeded the maximum number of concurrent tasks.",
@@ -56,7 +59,7 @@ func getGuild(w http.ResponseWriter, r *http.Request) {
 			"status": map[bool]string{
 				true:  "started",
 				false: "pending",
-			}[taskAdded],
+			}[ok],
 		})
 	}
 }
diff --git a/handlers/getGuildSearch.go b/handlers/getGuildSearch.go
index 197e51f..935a3e3 100644
--- a/handlers/getGuildSearch.go
+++ b/handlers/getGuildSearch.go
@@ -3,6 +3,7 @@ package handlers
 import (
 	"encoding/json"
 	"net/http"
+	"strconv"
 
 	"bdo-rest-api/cache"
 	"bdo-rest-api/scraper"
@@ -43,7 +44,9 @@ func getGuildSearch(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	if taskAdded, tasksQuantityExceeded := scraper.EnqueueGuildSearch(r.Header.Get("CF-Connecting-IP"), region, name); tasksQuantityExceeded {
+	ok, tasksExceeded, tasksNumber := scraper.EnqueueGuildSearch(r.Header.Get("CF-Connecting-IP"), region, name)
+	w.Header().Set("X-Tasks-Number", strconv.Itoa(tasksNumber))
+	if tasksExceeded {
 		w.WriteHeader(http.StatusTooManyRequests)
 		json.NewEncoder(w).Encode(map[string]string{
 			"message": "You have exceeded the maximum number of concurrent tasks.",
@@ -56,7 +59,7 @@ func getGuildSearch(w http.ResponseWriter, r *http.Request) {
 			"status": map[bool]string{
 				true:  "started",
 				false: "pending",
-			}[taskAdded],
+			}[ok],
 		})
 	}
 }
diff --git a/scraper/scraper.go b/scraper/scraper.go
index 2b6c632..800d638 100644
--- a/scraper/scraper.go
+++ b/scraper/scraper.go
@@ -116,13 +116,14 @@ func InitScraper() {
 	})
 }
 
-func createTask(clientIP, region, taskType string, query map[string]string) (taskAdded, tasksQuantityExceeded bool) {
+func createTask(clientIP, region, taskType string, query map[string]string) (ok, tasksExceeded bool, tasksNumber int) {
 	crc32 := crc32.NewIEEE()
 	crc32.Write([]byte(strings.Join(append(slices.Sorted(maps.Values(query)), region, taskType), "")))
 	hashString := strconv.Itoa(int(crc32.Sum32()))
 
-	if taskQueue.CountQueuedTasksForClient(clientIP) >= viper.GetInt("maxtasksperclient") {
-		return false, true
+	tasksN := taskQueue.CountQueuedTasksForClient(clientIP)
+	if tasksN >= viper.GetInt("maxtasksperclient") {
+		return false, true, tasksN
 	}
 
 	url := fmt.Sprintf(
@@ -147,17 +148,17 @@ func createTask(clientIP, region, taskType string, query map[string]string) (tas
 		"taskType":    taskType,
 	})
 
-	added := taskQueue.AddTask(clientIP, hashString, utils.BuildRequest(url, query))
-	return added, false
+	ok = taskQueue.AddTask(clientIP, hashString, utils.BuildRequest(url, query))
+	return ok, false, map[bool]int{true: tasksN + 1, false: tasksN}[ok]
 }
 
-func EnqueueAdventurer(clientIP, region, profileTarget string) (taskAdded, tasksQuantityExceeded bool) {
+func EnqueueAdventurer(clientIP, region, profileTarget string) (ok, tasksExceeded bool, tasksNumber int) {
 	return createTask(clientIP, region, "player", map[string]string{
 		"profileTarget": profileTarget,
 	})
 }
 
-func EnqueueAdventurerSearch(clientIP, region, query, searchType string) (taskAdded, tasksQuantityExceeded bool) {
+func EnqueueAdventurerSearch(clientIP, region, query, searchType string) (ok, tasksExceeded bool, tasksNumber int) {
 	return createTask(clientIP, region, "playerSearch", map[string]string{
 		"Page":          "1",
 		"region":        region,
@@ -166,14 +167,14 @@ func EnqueueAdventurerSearch(clientIP, region, query, searchType string) (taskAd
 	})
 }
 
-func EnqueueGuild(clientIP, region, name string) (taskAdded, tasksQuantityExceeded bool) {
+func EnqueueGuild(clientIP, region, name string) (ok, tasksExceeded bool, tasksNumber int) {
 	return createTask(clientIP, region, "guild", map[string]string{
 		"guildName": name,
 		"region":    region,
 	})
 }
 
-func EnqueueGuildSearch(clientIP, region, query string) (taskAdded, tasksQuantityExceeded bool) {
+func EnqueueGuildSearch(clientIP, region, query string) (ok, tasksExceeded bool, tasksNumber int) {
 	return createTask(clientIP, region, "guildSearch", map[string]string{
 		"page":       "1",
 		"region":     region,
