Skip to content

Commit 8290529

Browse files
authored
Merge pull request #30 from metrik-tech/feature/ENG-176
feature/eng 176: Address all to-do comments in the codebase
2 parents 9d63712 + 4f4fa02 commit 8290529

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

Src/API/ActionBuilder.luau

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Action.Prototype = {}
3131
ActionBuilder.Public = {}
3232
ActionBuilder.Prototype = {}
3333

34-
function Action.Prototype:CanRun(...: unknown) -- todo: figure out how the 'middleware' impl is going to look.
34+
function Action.Prototype:CanRun(...: unknown)
3535
return true
3636
end
3737

Src/Services/ApiService.luau

+33-17
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ local ApiPaths = require(script.Parent.Parent.Data.ApiPaths)
1111
local ServerType = require(script.Parent.Parent.Enums.ServerType)
1212

1313
local HEARTBEAT_UPDATE_SECONDS = 60 * 10 -- send server heartbeat every 10 minutes.
14+
local MAX_ATTEMPTS_AT_REQUESTS = 5
15+
local DELAY_BETWEEN_ATTEMPTS = 1.5
1416

1517
local ApiService = {}
1618

@@ -30,8 +32,6 @@ ApiService.OnAuthenticated = Signal.new()
3032

3133
ApiService.Authenticated = State.new(false)
3234

33-
-- todo: re-queue HTTP requests if they fail!
34-
3535
function ApiService._QueryTraceAsync(self: ApiService)
3636
return self:RawRequestAsync({
3737
Url = `https://{ApiPaths.TraceUrl}`,
@@ -103,24 +103,40 @@ end
103103

104104
function ApiService.RawRequestAsync(self: ApiService, data: { [any]: any })
105105
return Promise.new(function(resolve, reject)
106-
local success, response = pcall(HttpService.RequestAsync, HttpService, data)
106+
local attempts = 0
107+
local rejectionResponse
107108

108-
if not success or not response.Success then
109-
local responseIsTable = typeof(response) == "table"
110-
111-
return reject({
112-
Success = responseIsTable and response.Success or success,
113-
StatusCode = responseIsTable and response.StatusCode or 0,
114-
StatusMessage = responseIsTable and response.StatusMessage or response,
115-
Headers = responseIsTable and response.Headers or {},
116-
Body = responseIsTable and response.Body or HttpService:JSONEncode({
117-
code = 0,
118-
message = response
119-
})
120-
})
109+
while attempts < MAX_ATTEMPTS_AT_REQUESTS do
110+
local success, response = pcall(HttpService.RequestAsync, HttpService, data)
111+
112+
if not success or not response.Success then
113+
local responseIsTable = typeof(response) == "table"
114+
115+
rejectionResponse = {
116+
Success = responseIsTable and response.Success or success,
117+
StatusCode = responseIsTable and response.StatusCode or 0,
118+
StatusMessage = responseIsTable and response.StatusMessage or response,
119+
Headers = responseIsTable and response.Headers or {},
120+
Body = responseIsTable and response.Body or HttpService:JSONEncode({
121+
code = 0,
122+
message = response
123+
})
124+
}
125+
126+
self.Reporter:Warn(`Attempt #{attempts} to make a request to '{data.Url}' failed;`)
127+
self.Reporter:Warn(rejectionResponse)
128+
129+
task.wait(DELAY_BETWEEN_ATTEMPTS)
130+
131+
attempts += 1
132+
133+
continue
134+
end
135+
136+
return resolve(response)
121137
end
122138

123-
return resolve(response)
139+
return reject(rejectionResponse)
124140
end)
125141
end
126142

0 commit comments

Comments
 (0)