Task #6120
closedTask #6116: EPIC: Speed up the stats updater daemon (scripts/workflow/update_stats.sh)
Fix releaseToken: response body read twice, so IIKO sessions leak
100%
Description
Problem¶
IikoAuthTokensFetchingService.releaseToken consumes the OkHttp response body twice:
64: if (response.body?.string()!!.contains("Connection released")) { ... }
68: if (response.body?.string()!!.contains("Token is expired or invalid")) { ... }
ResponseBody.string() reads and closes the stream. The second call at line 68 therefore throws on an already-consumed body, which means:
- the "Token is expired or invalid" branch is dead code — it can never be reached;
- any logout response that is not exactly "Connection released" ends in an exception (either the
IllegalStateExceptionfrom the consumed body, or the explicitthrow Exception("Не удалось освободить токен")at the end); - the token row is left
isActive = truein our DB while the IIKO-side session may or may not be closed.
Because fetchOlapData calls releaseToken in the middle of its happy path (line 56, after the data is already in hand), a throw here discards a successful OLAP result and burns a session. With one session opened per request (see the session-reuse task) this compounds directly into the concurrency-limit symptom.
Also note the two response.close() calls in fetchOlapData (lines 50 and 54) — the second is redundant.
Proposed change¶
- Read the body once into a local
val, branch on that. - Mark the token inactive locally on any terminal outcome, including a failed logout — a token we can no longer use is not active regardless of what IIKO said.
- Do not let a logout failure propagate out of a successful data fetch; log it.
Acceptance¶
-
releaseTokennever reads the body more than once. - A non-"Connection released" logout response no longer throws out of
fetchOlapData.
Updated by Redmine Admin about 9 hours ago
- Related to Task #6117: Reuse one IIKO session per command run instead of login/logout per request added
Updated by Redmine Admin about 9 hours ago
- Status changed from New to Resolved
- % Done changed from 0 to 100
Реализовано в ветке speedup/stats-daemon, коммит 50a6ebef.
IikoAuthTokensFetchingService.releaseToken теперь читает тело ответа один раз в локальную переменную и ветвится по ней. Неожиданный ответ логируется как warning, а не бросается наружу; сетевой сбой самого логаута тоже перехватывается. Токен помечается isActive = false при любом исходе — ключ, которым мы больше не пользуемся, активным считать нельзя, что бы ни ответил iiko.
Заодно: в fetchOlapData больше нет двойного response.close() — тело читается через .use { }, и в fetchToken тоже.