Skip to content

Commit

Permalink
abort requests after synonym group has been aborted
Browse files Browse the repository at this point in the history
should imprive perfomance after an abort – not clogging up future requests
  • Loading branch information
nleanba committed Feb 12, 2024
1 parent 396c6f4 commit beec196
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 58 deletions.
69 changes: 49 additions & 20 deletions SynonymGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,11 @@ export class SparqlEndpoint {
}
async getSparqlResultSet(
query: string,
fetchOptions: { headers?: Record<string, string> } = {},
fetchOptions: RequestInit = {},
) {
fetchOptions.headers = fetchOptions.headers || {};
fetchOptions.headers["Accept"] = "application/sparql-results+json";
(fetchOptions.headers as Record<string, string>)["Accept"] =
"application/sparql-results+json";
let retryCount = 0;
// deno-lint-ignore no-explicit-any
const sendRequest = async (): Promise<any> => {
Expand All @@ -271,15 +272,16 @@ export class SparqlEndpoint {
}
return await response.json();
} catch (error) {
if (
if (error instanceof DOMException) {
// i.e. signal is aborted
throw error;
} else if (
error instanceof Error &&
retryCount < 5 /* && error.message.endsWith("502") */
) {
++retryCount;
console.warn(
`!! Fetch Error. Retrying in ${
retryCount * 50
}ms (${retryCount})`,
`!! Fetch Error. Retrying in ${retryCount * 50}ms (${retryCount})`,
);
await sleep(retryCount * 50);
return await sendRequest();
Expand Down Expand Up @@ -307,6 +309,8 @@ export default class SynonymGroup implements AsyncIterable<JustifiedSynonym> {
isFinished = false;
isAborted = false;

private controller = new AbortController();

constructor(
sparqlEndpoint: SparqlEndpoint,
taxonName: string,
Expand All @@ -324,10 +328,13 @@ export default class SynonymGroup implements AsyncIterable<JustifiedSynonym> {
this.monitor.dispatchEvent(new CustomEvent("updated"));
};

const fetchInit = { signal: this.controller.signal };

const build = async () => {
function getStartingPoints(
taxonName: string,
): Promise<JustifiedSynonym[]> {
if (fetchInit.signal.aborted) return new Promise((r) => r([]));
const [genus, species, subspecies] = taxonName.split(" ");
// subspecies could also be variety
// ignoreRank has no effect when there is a 'subspecies', as this is assumed to be the lowest rank & should thus not be able to return results in another rank
Expand All @@ -346,7 +353,8 @@ export default class SynonymGroup implements AsyncIterable<JustifiedSynonym> {
a <http://filteredpush.org/ontologies/oa/dwcFP#TaxonConcept>.
}`;
// console.info('%cREQ', 'background: red; font-weight: bold; color: white;', `getStartingPoints('${taxonName}')`)
return sparqlEndpoint.getSparqlResultSet(query)
if (fetchInit.signal.aborted) return new Promise((r) => r([]));
return sparqlEndpoint.getSparqlResultSet(query, fetchInit)
.then((json: SparqlJson) =>
json.results.bindings.map((t) => {
return {
Expand All @@ -364,8 +372,10 @@ export default class SynonymGroup implements AsyncIterable<JustifiedSynonym> {
},
loading: true,
};
})
);
}), (error) => {
console.warn("SPARQL Error: " + error);
return [];
});
}

const synonymFinders = [
Expand All @@ -385,7 +395,8 @@ export default class SynonymGroup implements AsyncIterable<JustifiedSynonym> {
return Promise.resolve([]);
}
expandedTaxonNames.add(taxon.taxonNameUri);
return sparqlEndpoint.getSparqlResultSet(query).then((
if (fetchInit.signal.aborted) return new Promise((r) => r([]));
return sparqlEndpoint.getSparqlResultSet(query, fetchInit).then((
json: SparqlJson,
) =>
json.results.bindings.filter((t) => t.tc).map((t) => {
Expand All @@ -404,8 +415,10 @@ export default class SynonymGroup implements AsyncIterable<JustifiedSynonym> {
},
loading: true,
};
})
);
}), (error) => {
console.warn("SPARQL Error: " + error);
return [];
});
},
/** Get the Synonyms deprecating {taxon} */
(taxon: JustifiedSynonym): Promise<JustifiedSynonym[]> => {
Expand All @@ -425,7 +438,9 @@ export default class SynonymGroup implements AsyncIterable<JustifiedSynonym> {
}
GROUP BY ?tc ?tn ?treat ?date`;
// console.info('%cREQ', 'background: red; font-weight: bold; color: white;', `synonymFinder[1]( ${taxon.taxonConceptUri} )`)
return sparqlEndpoint.getSparqlResultSet(query).then((

if (fetchInit.signal.aborted) return new Promise((r) => r([]));
return sparqlEndpoint.getSparqlResultSet(query, fetchInit).then((
json: SparqlJson,
) =>
json.results.bindings.filter((t) => t.tc).map((t) => {
Expand All @@ -450,8 +465,10 @@ export default class SynonymGroup implements AsyncIterable<JustifiedSynonym> {
},
loading: true,
};
})
);
}), (error) => {
console.warn("SPARQL Error: " + error);
return [];
});
},
/** Get the Synonyms deprecated by {taxon} */
(taxon: JustifiedSynonym): Promise<JustifiedSynonym[]> => {
Expand All @@ -471,7 +488,8 @@ export default class SynonymGroup implements AsyncIterable<JustifiedSynonym> {
}
GROUP BY ?tc ?tn ?treat ?date`;
// console.info('%cREQ', 'background: red; font-weight: bold; color: white;', `synonymFinder[2]( ${taxon.taxonConceptUri} )`)
return sparqlEndpoint.getSparqlResultSet(query).then((
if (fetchInit.signal.aborted) return new Promise((r) => r([]));
return sparqlEndpoint.getSparqlResultSet(query, fetchInit).then((
json: SparqlJson,
) =>
json.results.bindings.filter((t) => t.tc).map((t) => {
Expand All @@ -496,8 +514,10 @@ export default class SynonymGroup implements AsyncIterable<JustifiedSynonym> {
},
loading: true,
};
})
);
}), (error) => {
console.warn("SPARQL Error: " + error);
return [];
});
},
];

Expand Down Expand Up @@ -531,7 +551,9 @@ export default class SynonymGroup implements AsyncIterable<JustifiedSynonym> {
}
GROUP BY ?treat ?how ?date`;
// console.info('%cREQ', 'background: red; font-weight: bold; color: white;', `getTreatments('${uri}')`)
return sparqlEndpoint.getSparqlResultSet(query).then(

if (fetchInit.signal.aborted) return new Promise((r) => r());
return sparqlEndpoint.getSparqlResultSet(query, fetchInit).then(
(json: SparqlJson) => {
json.results.bindings.forEach((t) => {
if (!t.treat) return;
Expand All @@ -554,6 +576,7 @@ export default class SynonymGroup implements AsyncIterable<JustifiedSynonym> {
}
});
},
(error) => console.warn("SPARQL Error: " + error),
);
}

Expand Down Expand Up @@ -583,7 +606,8 @@ export default class SynonymGroup implements AsyncIterable<JustifiedSynonym> {
OPTIONAL { ?mc trt:gbifSpecimenId ?gbifSpecimenId . }
OPTIONAL { ?mc trt:httpUri ?httpUri . }
}`;
return sparqlEndpoint.getSparqlResultSet(query).then(
if (fetchInit.signal.aborted) return new Promise((r) => r([]));
return sparqlEndpoint.getSparqlResultSet(query, fetchInit).then(
(json: SparqlJson) => {
const resultArray: MaterialCitation[] = [];
json.results.bindings.forEach((t) => {
Expand Down Expand Up @@ -612,6 +636,10 @@ export default class SynonymGroup implements AsyncIterable<JustifiedSynonym> {
});
return resultArray;
},
(error) => {
console.warn("SPARQL Error: " + error);
return [];
},
);
}

Expand Down Expand Up @@ -681,6 +709,7 @@ export default class SynonymGroup implements AsyncIterable<JustifiedSynonym> {

abort() {
this.isAborted = true;
this.controller.abort();
}

[Symbol.asyncIterator]() {
Expand Down
85 changes: 47 additions & 38 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,53 @@ const taxonName = Deno.args.length > 0
: "Sadayoshia acroporae";
const synoGroup = new SynoGroup(sparqlEndpoint, taxonName);

setTimeout(() => {
console.log("aborting after 1s");
synoGroup.abort();
}, 1000);

console.log(Colors.blue(`Synonym Group For ${taxonName}`));
for await (const synonym of synoGroup) {
console.log(Colors.red(` * Found synonym: ${synonym.taxonConceptUri}`));
try {
for await (const synonym of synoGroup) {
console.log(Colors.red(` * Found synonym: ${synonym.taxonConceptUri}`));

(async () => {
for await (const justification of synonym.justifications) {
console.log(
Colors.magenta(
` - Found justification for ${synonym.taxonConceptUri}: ${justification}`,
),
);
}
})();
(async () => {
for await (const treatment of synonym.treatments!.aug) {
console.log(
Colors.gray(
` - Found augmenting treatment for ${synonym.taxonConceptUri}: ${treatment.url}`,
),
);
}
})();
(async () => {
for await (const treatment of synonym.treatments.def) {
console.log(
Colors.gray(
` - Found defining treatment for ${synonym.taxonConceptUri}: ${treatment.url}`,
),
);
}
})();
(async () => {
for await (const treatment of synonym.treatments.dpr) {
console.log(
Colors.gray(
` - Found deprecating treatment for ${synonym.taxonConceptUri}: ${treatment.url}`,
),
);
}
})();
(async () => {
for await (const justification of synonym.justifications) {
console.log(
Colors.magenta(
` - Found justification for ${synonym.taxonConceptUri}: ${justification}`,
),
);
}
})();
(async () => {
for await (const treatment of synonym.treatments!.aug) {
console.log(
Colors.gray(
` - Found augmenting treatment for ${synonym.taxonConceptUri}: ${treatment.url}`,
),
);
}
})();
(async () => {
for await (const treatment of synonym.treatments.def) {
console.log(
Colors.gray(
` - Found defining treatment for ${synonym.taxonConceptUri}: ${treatment.url}`,
),
);
}
})();
(async () => {
for await (const treatment of synonym.treatments.dpr) {
console.log(
Colors.gray(
` - Found deprecating treatment for ${synonym.taxonConceptUri}: ${treatment.url}`,
),
);
}
})();
}
} catch (error) {
console.error(Colors.red(error + ""));
}

0 comments on commit beec196

Please sign in to comment.