-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Goodluckhf/develop
Error handling and docs fixes
- Loading branch information
Showing
4 changed files
with
54 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,56 @@ | ||
export default (config, serviceDiscovery) => { | ||
let oldConfig = null; | ||
|
||
return async (req, res) => { | ||
const actualConfig = serviceDiscovery.getActualConfig(); | ||
|
||
// Если модуль только стартанул, | ||
// надо сразу сгенерировать и отдать конфиг | ||
if (!actualConfig) { | ||
const generatedConfig = await serviceDiscovery.generateConfig(); | ||
res.json(generatedConfig.getRawObject()); | ||
return; | ||
} | ||
|
||
// Если индекса нет | ||
// Значит запроси пришел либо в ручную | ||
// Либо это первый запрос | ||
const { index } = req.query; | ||
if (!index) { | ||
res.json(actualConfig.getRawObject()); | ||
return; | ||
} | ||
|
||
// Сам long polling | ||
// По таймауту, если конфиг не изменился | ||
// Отдаем текущий актуальный | ||
let responseSent = false; | ||
let interval = null; | ||
const timeout = setTimeout(() => { | ||
clearInterval(interval); | ||
if (responseSent) { | ||
return async (req, res, next) => { | ||
try { | ||
const actualConfig = serviceDiscovery.getActualConfig(); | ||
|
||
// Если модуль только стартанул, | ||
// надо сразу сгенерировать и отдать конфиг | ||
if (!actualConfig) { | ||
const generatedConfig = await serviceDiscovery.generateConfig(); | ||
res.json(generatedConfig.getRawObject()); | ||
return; | ||
} | ||
|
||
responseSent = true; | ||
res.json(actualConfig.getRawObject()); | ||
}, config.polling_interval); | ||
|
||
interval = setInterval(() => { | ||
const actualConfigForIteration = serviceDiscovery.getActualConfig(); | ||
if (actualConfigForIteration.equals(oldConfig)) { | ||
// Если индекса нет | ||
// Значит запроси пришел либо в ручную | ||
// Либо это первый запрос | ||
const { index } = req.query; | ||
if (!index) { | ||
res.json(actualConfig.getRawObject()); | ||
return; | ||
} | ||
|
||
clearTimeout(timeout); | ||
clearInterval(interval); | ||
oldConfig = actualConfigForIteration; | ||
const newConfig = serviceDiscovery.getActualConfig(); | ||
res.json(newConfig.getRawObject()); | ||
}, 1000); | ||
// Сам long polling | ||
// По таймауту, если конфиг не изменился | ||
// Отдаем текущий актуальный | ||
let responseSent = false; | ||
let interval = null; | ||
const timeout = setTimeout(() => { | ||
clearInterval(interval); | ||
if (responseSent) { | ||
return; | ||
} | ||
|
||
responseSent = true; | ||
res.json(actualConfig.getRawObject()); | ||
}, config.polling_interval); | ||
|
||
interval = setInterval(() => { | ||
const actualConfigForIteration = serviceDiscovery.getActualConfig(); | ||
if (actualConfigForIteration.equals(oldConfig)) { | ||
return; | ||
} | ||
|
||
clearTimeout(timeout); | ||
clearInterval(interval); | ||
oldConfig = actualConfigForIteration; | ||
const newConfig = serviceDiscovery.getActualConfig(); | ||
res.json(newConfig.getRawObject()); | ||
}, 1000); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
}; |