Skip to content

Commit

Permalink
improve 429 prevention (async limit), add ua
Browse files Browse the repository at this point in the history
  • Loading branch information
lartsch committed Dec 29, 2022
1 parent 95ec910 commit f23e7be
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,19 @@ async function generalRequest(data) {
controller.abort()
}, timeout)
if (data[3]) {
// json body provided, post as body to target
data[2]["User-Agent"] = "FediAct Service"
data[2]["Content-Type"] = "application/json"
var res = await fetch(data[1], {
method: data[0],
signal: controller.signal,
// if json body is provided, there is also header data
headers: data[2],
body: JSON.stringify(data[3])
})
} else if (data[2]) {
// header data provided
data[2]["User-Agent"] = "FediAct Service"
var res = await fetch(data[1], {
method: data[0],
signal: controller.signal,
Expand All @@ -69,7 +74,8 @@ async function generalRequest(data) {
} else {
var res = await fetch(data[1], {
method: data[0],
signal: controller.signal
signal: controller.signal,
headers: {"User-Agent": "FediAct Service"}
})
}
clearTimeout(timeoutId)
Expand Down
2 changes: 1 addition & 1 deletion src/background.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 26 additions & 8 deletions src/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ const mutesApi = "/api/v1/mutes"
const blocksApi = "/api/v1/blocks"
const domainBlocksApi = "/api/v1/domain_blocks"
const pollsApi = "/api/v1/polls"
const apiDelay = 650
const apiDelay = 600
const maxTootCache = 200
const modalHtml = '<div class="fediactmodal"><div class="fediactmodalinner"><ul class="fediactmodallist"></ul></div></div>'
const maxAsyncRequests = 10

// settings keys with defauls
var settings = {}
Expand Down Expand Up @@ -124,13 +125,27 @@ var getUrlParameter = function getUrlParameter(sParam) {
return false
}

const asyncLimit = (fn, n) => {
let pendingPromises = []
return async function (...args) {
while (pendingPromises.length >= n) {
await Promise.race(pendingPromises).catch(() => {})
}
const p = fn.apply(this, args)
pendingPromises.push(p)
await p.catch(() => {})
pendingPromises = pendingPromises.filter(pending => pending !== p)
return p
}
}

// promisified xhr for api calls
function makeRequest(method, url, extraheaders, jsonbody) {
return new Promise(async function(resolve) {
// get current time
var currenttime = Date.now()
// try to prevent error 429 too many request by delaying home instance requests
if (~url.indexOf(settings.fediact_homeinstance) && settings.fediact_enabledelay) {
// get current time
var currenttime = Date.now()
// get difference of current time and time of last request
var difference = currenttime - tmpSettings.lasthomerequest
// if difference is smaller than our set api delay value...
Expand Down Expand Up @@ -162,6 +177,9 @@ function makeRequest(method, url, extraheaders, jsonbody) {
})
}

// wrap so there are never more than 10 concurrent requests
const requestAsyncLimited = asyncLimit(makeRequest, maxAsyncRequests)

// Escape characters used for regex
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
Expand Down Expand Up @@ -283,7 +301,7 @@ async function executeAction(data, action, polldata) {
return
}
if (requestUrl) {
var response = await makeRequest(method, requestUrl, tmpSettings.tokenheader, jsonbody)
var response = await requestAsyncLimited(method, requestUrl, tmpSettings.tokenheader, jsonbody)
if (response) {
// convert to json object
response = JSON.parse(response)
Expand All @@ -307,7 +325,7 @@ async function isFollowingHomeInstance(ids) {
requestUrl += "id[]=" + id.toString() + "&"
}
// make the request
var responseFollowing = await makeRequest("GET", requestUrl, tmpSettings.tokenheader, null)
var responseFollowing = await requestAsyncLimited("GET", requestUrl, tmpSettings.tokenheader, null)
// fill response array according to id amount with false
const follows = Array(ids.length).fill(false)
// parse the response
Expand Down Expand Up @@ -379,7 +397,7 @@ function checkAllMutedBlocked(handle) {
// Return the user id on the users home instance
async function resolveHandleToHome(handle) {
var requestUrl = 'https://' + settings.fediact_homeinstance + accountsApi + "/search?q=" + handle + "&resolve=true&limit=1&exclude_unreviewed=false"
var searchResponse = await makeRequest("GET", requestUrl, tmpSettings.tokenheader, null)
var searchResponse = await requestAsyncLimited("GET", requestUrl, tmpSettings.tokenheader, null)
if (searchResponse) {
searchResponse = JSON.parse(searchResponse)
if (searchResponse[0].id) {
Expand All @@ -393,7 +411,7 @@ async function resolveHandleToHome(handle) {
// resolve a toot to the users home instance
async function resolveTootToHome(searchstring) {
var requestUrl = 'https://' + settings.fediact_homeinstance + searchApi + "/?q=" + searchstring + "&resolve=true&limit=1&exclude_unreviewed=false"
var response = await makeRequest("GET", requestUrl, tmpSettings.tokenheader, null)
var response = await requestAsyncLimited("GET", requestUrl, tmpSettings.tokenheader, null)
if (response) {
response = JSON.parse(response)
// do we have a status as result?
Expand Down Expand Up @@ -1471,7 +1489,7 @@ async function checkSite() {
// last check - and probably the most accurate to determine if it actually is mastadon
var requestUrl = location.protocol + '//' + location.hostname + instanceApi
// call instance api to confirm its mastodon and get normalized handle uri
var response = await makeRequest("GET", requestUrl, null, null)
var response = await requestAsyncLimited("GET", requestUrl, null, null)
// todo: add basic check for "mastodon" string in response
if (response) {
var uri = JSON.parse(response).uri
Expand Down
Loading

0 comments on commit f23e7be

Please sign in to comment.