mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-24 16:22:44 +00:00
Add retryconfig and forward user ip
This commit is contained in:
@@ -25,6 +25,15 @@ if(!config.get('service_account')){
|
||||
|
||||
const defaultAuth = serviceAuth || config.get('google_api_key');
|
||||
|
||||
const retryConfig = {
|
||||
retry: 3, // Number of retry attempts
|
||||
retryDelay: 100, // Initial delay in milliseconds
|
||||
retryDelayMultiplier: 2, // Multiplier for exponential backoff
|
||||
maxRetryDelay: 32000, // Maximum delay in milliseconds
|
||||
httpMethodsToRetry: ['PATCH'], // Only retry PATCH requests
|
||||
statusCodesToRetry: [[429, 429]], // Only retry on 429 status code
|
||||
};
|
||||
|
||||
const GoogleActions = {
|
||||
|
||||
authCheck : (account, res, updateTokens=true)=>{
|
||||
@@ -147,9 +156,8 @@ const GoogleActions = {
|
||||
return brews;
|
||||
},
|
||||
|
||||
updateGoogleBrew : async (brew, auth = defaultAuth)=>{
|
||||
updateGoogleBrew : async (brew, auth = defaultAuth, userIp)=>{
|
||||
const drive = googleDrive.drive({ version: 'v3', auth: auth });
|
||||
|
||||
console.log(auth == defaultAuth ? 'UPDATE w SERVICEACC' : 'UPDATE w USERACC')
|
||||
|
||||
await drive.files.update({
|
||||
@@ -170,7 +178,11 @@ const GoogleActions = {
|
||||
media : {
|
||||
mimeType : 'text/plain',
|
||||
body : brew.text
|
||||
}
|
||||
},
|
||||
headers: {
|
||||
'X-Forwarded-For': userIp, // Set the X-Forwarded-For header
|
||||
},
|
||||
retryConfig
|
||||
})
|
||||
.catch((err)=>{
|
||||
console.log('Error saving to google');
|
||||
|
||||
@@ -373,7 +373,7 @@ const api = {
|
||||
if(!brew.googleId) return;
|
||||
} else if(brew.googleId) {
|
||||
// If the google id exists and no other actions are being performed, update the google brew
|
||||
const updated = await api.updateGoogleBrew(req.account, api.excludeGoogleProps(brew), res);
|
||||
const updated = await api.updateGoogleBrew(req.account, api.excludeGoogleProps(brew), res, req);
|
||||
|
||||
if(!updated) return;
|
||||
}
|
||||
@@ -418,12 +418,12 @@ const api = {
|
||||
res.status(200).send(saved);
|
||||
},
|
||||
|
||||
updateGoogleBrew : async (account, brew, res)=>{
|
||||
updateGoogleBrew : async (account, brew, res, req)=>{
|
||||
let oAuth2Client;
|
||||
if(account.googleId)
|
||||
oAuth2Client = GoogleActions.authCheck(account, res);
|
||||
|
||||
return await GoogleActions.updateGoogleBrew(brew, oAuth2Client);
|
||||
return await GoogleActions.updateGoogleBrew(brew, oAuth2Client, req.ip);
|
||||
},
|
||||
|
||||
deleteGoogleBrew : async (account, id, editId, res)=>{
|
||||
|
||||
Reference in New Issue
Block a user