Nominatim 요청으로 모든 요청 사이에 1500ms의 시간 초과를 유지해야합니다. 여러 동시 실행 프로세스가 해당 기능에서 계속 실행되는 경우 어떻게 처리합니까?
암호:
osmService :
export const getLocation = async (zipCode, street) => {
if (!zipCode) {
return {
address: null,
cacheHit: false,
};
}
const cacheData = await getAddress(zipCode, street);
if (cacheData && cacheData.length === 1) {
return { address: cacheData[0].address, cacheHit: true };
} if (cacheData && cacheData.length > 1) {
logger.error('Found multiple address, this should not be', zipCode + street);
}
try {
const responseObj = await getNominatimLocation(zipCode, street);
if (responseObj) {
const returnObj = {
...responseObj.address,
lat: responseObj.lat,
lon: responseObj.lon,
displayName: responseObj.display_name,
};
await insertAddress(zipCode, street, null, returnObj);
return {
address: returnObj,
cacheHit: false,
};
}
return {
address: null,
cacheHit: false,
};
} catch (ex) {
logger.error(`Error getting location from ${zipCode} ${street}`, ex);
}
return {
address: null,
cacheHit: false,
};
};
보시다시피 사이에 캐싱 레이어가 있습니다. 따라서 요청이 캐시에 맞았을 때 1500ms를 기다릴 필요가 없습니다.
export const getNominatimLocation = async (zipCode, street, query, retries = 0) => {
if (retries > 5) {
return null;
}
try {
const qs = {
format: 'json',
q: query,
postalcode: zipCode,
addressdetails: 1,
country: 'Deutschland',
street,
'accept-language': 'de',
};
const response = await requestPromise()
.get({
url: OSM_SEARCH,
qs,
timeout: 12000,
headers: {
'User-Agent': 'xxxxxxx',
},
});
return JSON.parse(response)[0];
} catch (ex) {
logger.info(`Nominatim timed out - retry ${retries}`, ex);
await timeout(9000);
return await getNominatimLocation(zipCode, street, query, retries + 1);
}
};
Nominatim이 종종 시간 초과되기 때문에이 재귀 호출을 수행해야합니다 (재귀적일 필요는 없습니다-더 쉬웠습니다).
이제 위치를 얻고 자하는 다음 작업 (백엔드 엔진)이 있다고 가정 해 보겠습니다.
const jobA = asnyc () => {
const {address, cacheHit} = await getLocation(10243);
if(!cacheHit){
await timeout(1500)
}
}
const jobB = asnyc () => {
const {address, cacheHit} = await getLocation(10245);
if(!cacheHit){
await timeout(1500)
}
}
const startJobs = async () => {
Promise.all([jobA(),jobB()])
console.log('all jobs done');
}
작업은 부분적으로 내 현재 코드 구조를 나타냅니다. 내 코드에서 작업은 더 많은 작업을 수행합니다 (다른 서비스 호출 등).
이제이 레이아웃을 가지고있을 때-cacheHit이 없을 때 모든 Nominatim 호출 사이에 1500ms를 유지하려면 어떻게해야합니까?
1.5 초마다 잠금 해제되는 잠금을 사용할 수 있습니다.
let lock = Promise.resolve();
let aquireLock = () => (lock = lock.then(() => new Promise(res => setTimeout(res, 1500))));
그때
await aquireLock();
// will only run every 1.5 seconds
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다