Skip to content

Commit

Permalink
Handling of unexpectedly terminated sessions (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchrabas authored Oct 17, 2024
1 parent 8d55a3a commit 905ce4a
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,20 @@ func (client *Client) Do(req Req) (Res, error) {
} else if httpRes.StatusCode == 429 || (httpRes.StatusCode >= 500 && httpRes.StatusCode <= 599) {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v, Retries: %v", httpRes.StatusCode, attempts)
continue
} else if httpRes.StatusCode == 401 {
// There are bugs in FMC, where the sessions are invalidated out of the blue
// In case such a situation is detected, new authentication is forced
log.Printf("[DEBUG] Invalid session detected. Forcing reauthentication")
// Clear AuthToken (which is invalid anyways). This also ensures that Authenticate does full authentication
client.AuthToken = ""
// Force reauthentication, client.Authenticate() takes care of mutexes, hence not calling Login() directly
err := client.Authenticate()
if err != nil {
log.Printf("[DEBUG] HTTP Request failed: StatusCode 401: Forced reauthentication failed: %s", err.Error())
return res, fmt.Errorf("HTTP Request failed: StatusCode 401: Forced reauthentication failed: %s", err.Error())
}
req.HttpReq.Header.Set("X-auth-access-token", client.AuthToken)
continue
} else {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v", httpRes.StatusCode)
log.Printf("[DEBUG] Exit from Do method")
Expand Down

0 comments on commit 905ce4a

Please sign in to comment.