@@ -25,24 +25,34 @@ func getAdventurerSearch(w http.ResponseWriter, r *http.Request) {
25
25
searchTypeQueryParam := r .URL .Query ()["searchType" ]
26
26
searchType := validators .ValidateSearchTypeQueryParam (searchTypeQueryParam )
27
27
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 )
35
36
}
36
37
37
- data , status , date , expires = cache . ProfileSearch . WaitForRecord ( taskId )
38
+ return
38
39
}
39
40
40
- w .Header ().Set ("Date" , date )
41
- w .Header ().Set ("Expires" , expires )
41
+ if ok := giveMaintenanceResponse (w , region ); ok {
42
+ return
43
+ }
42
44
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
47
52
}
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
+ })
48
58
}
0 commit comments