-
Notifications
You must be signed in to change notification settings - Fork 117
feat(binary): лимит памяти для ДвоичныеДанные из конфигурации (#1667) #1670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
johnnyshut
wants to merge
6
commits into
EvilBeaver:develop
Choose a base branch
from
johnnyshut:fix/1667
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a7f38e3
feat(binary): вынесли порог памяти для ДвоичныеДанные в конфигурацию
johnnyshut 2c90bbf
refactor(BslProcess): Оптимизация уведомлений исполнителя и улучшение…
johnnyshut ae3651f
feat(binary): добавление поддержки конфигурации лимита памяти для дво…
johnnyshut 2b1e0e6
fix(oscript.cfg): добавлен недостающий перевод строки в конфигурацию …
johnnyshut 27a89b6
refactor(binary): обновление ключей конфигурации лимита памяти для дв…
johnnyshut 40d80cb
Рефакторинг
EvilBeaver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
src/OneScript.StandardLibrary/Binary/BinaryDataConstants.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /*---------------------------------------------------------- | ||
| This Source Code Form is subject to the terms of the | ||
| Mozilla Public License, v.2.0. If a copy of the MPL | ||
| was not distributed with this file, You can obtain one | ||
| at http://mozilla.org/MPL/2.0/. | ||
| ----------------------------------------------------------*/ | ||
|
|
||
| using System; | ||
|
|
||
| namespace OneScript.StandardLibrary.Binary | ||
| { | ||
| public static class BinaryDataConstants | ||
| { | ||
| /// <summary> | ||
| /// Максимальный размер массива, доступный в среде выполнения. | ||
| /// Де-факто он чуть меньше 2Гб, он же Int32.MaxValue, поэтому используется системная константа <see cref="Array.MaxLength"/> | ||
| /// </summary> | ||
| public static readonly int SYSTEM_IN_MEMORY_LIMIT = Array.MaxLength; | ||
|
|
||
| /// <summary> | ||
| /// Размер двоичных данных, хранимый в памяти по умолчанию. | ||
| /// </summary> | ||
| public const int DEFAULT_IN_MEMORY_LIMIT = 1024 * 1024 * 50; | ||
| } | ||
| } |
This file contains hidden or 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
101 changes: 101 additions & 0 deletions
101
src/OneScript.StandardLibrary/Binary/BinaryDataOptions.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // /*---------------------------------------------------------- | ||
| // This Source Code Form is subject to the terms of the | ||
| // Mozilla Public License, v.2.0. If a copy of the MPL | ||
| // was not distributed with this file, You can obtain one | ||
| // at http://mozilla.org/MPL/2.0/. | ||
| // ----------------------------------------------------------*/ | ||
|
|
||
| using System.Globalization; | ||
| using ScriptEngine; | ||
| using ScriptEngine.Hosting; | ||
|
|
||
| namespace OneScript.StandardLibrary.Binary | ||
| { | ||
| /// <summary> | ||
| /// Инкапсулирует логику чтения и хранения настроек двоичных данных | ||
| /// </summary> | ||
| public class BinaryDataOptions : IBinaryDataMemoryLimit | ||
| { | ||
| public const string IN_MEMORY_LIMIT_KEY_NAME = "binaryData.inMemoryMaxSize"; | ||
| public const string IN_MEMORY_MAX_MAGIC = "max"; | ||
|
|
||
| public BinaryDataOptions(KeyValueConfig config) | ||
| { | ||
| var configValue = config[IN_MEMORY_LIMIT_KEY_NAME]; | ||
| MaxBytesInMemory = ResolveFromConfigString(configValue); | ||
| } | ||
|
|
||
| public int MaxBytesInMemory { get; } | ||
|
|
||
| private static int ResolveFromConfigString(string rawValue) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(rawValue)) | ||
| return BinaryDataConstants.DEFAULT_IN_MEMORY_LIMIT; | ||
|
|
||
| if (rawValue.Trim() == IN_MEMORY_MAX_MAGIC) | ||
| return BinaryDataConstants.SYSTEM_IN_MEMORY_LIMIT; | ||
|
|
||
| if (!TryParseByteSize(rawValue.Trim(), out var bytes)) | ||
| { | ||
| SystemLogger.Write($"Invalid value for {IN_MEMORY_LIMIT_KEY_NAME}: {rawValue}"); | ||
| return BinaryDataConstants.DEFAULT_IN_MEMORY_LIMIT; | ||
| } | ||
|
|
||
| if (bytes <= 0 || bytes >= BinaryDataConstants.SYSTEM_IN_MEMORY_LIMIT) | ||
| { | ||
| SystemLogger.Write($"Value for {IN_MEMORY_LIMIT_KEY_NAME} must be between 1 and {BinaryDataConstants.SYSTEM_IN_MEMORY_LIMIT - 1}: {bytes}"); | ||
| return BinaryDataConstants.DEFAULT_IN_MEMORY_LIMIT; | ||
| } | ||
|
|
||
| return (int)bytes; | ||
| } | ||
|
|
||
| private static bool TryParseByteSize(string value, out long bytes) | ||
| { | ||
| bytes = 0; | ||
|
|
||
| if (value.Length == 0) | ||
| return false; | ||
|
|
||
| var suffix = value[value.Length - 1]; | ||
| long multiplier = 1; | ||
| var numberPart = value; | ||
|
|
||
| const long KILOBYTES = 1024L; | ||
| const long MEGABYTES = KILOBYTES * 1024L; | ||
| const long GIGABYTES = MEGABYTES * 1024L; | ||
|
|
||
| switch (suffix) | ||
| { | ||
| case 'k': | ||
| case 'K': | ||
| multiplier = KILOBYTES; | ||
| numberPart = value.Substring(0, value.Length - 1).TrimEnd(); | ||
| break; | ||
| case 'm': | ||
| case 'M': | ||
| multiplier = MEGABYTES; | ||
| numberPart = value.Substring(0, value.Length - 1).TrimEnd(); | ||
| break; | ||
| case 'g': | ||
| case 'G': | ||
| multiplier = GIGABYTES; | ||
| numberPart = value.Substring(0, value.Length - 1).TrimEnd(); | ||
| break; | ||
| } | ||
|
|
||
| if (numberPart.Length == 0) | ||
| return false; | ||
|
|
||
| if (!long.TryParse(numberPart, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, CultureInfo.InvariantCulture, out var number)) | ||
| return false; | ||
|
|
||
| if (number <= 0) | ||
| return false; | ||
|
|
||
| bytes = number * multiplier; | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| } |
17 changes: 0 additions & 17 deletions
17
src/OneScript.StandardLibrary/Binary/FileBackingConstants.cs
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
18 changes: 18 additions & 0 deletions
18
src/OneScript.StandardLibrary/Binary/IBinaryDataMemoryLimit.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /*---------------------------------------------------------- | ||
| This Source Code Form is subject to the terms of the | ||
| Mozilla Public License, v.2.0. If a copy of the MPL | ||
| was not distributed with this file, You can obtain one | ||
| at http://mozilla.org/MPL/2.0/. | ||
| ----------------------------------------------------------*/ | ||
|
|
||
| namespace OneScript.StandardLibrary.Binary | ||
| { | ||
| /// <summary> | ||
| /// Лимит объёма данных в памяти для объектов «ДвоичныеДанные» и смежных потоков | ||
| /// до выгрузки во временный файл (байты). | ||
| /// </summary> | ||
| public interface IBinaryDataMemoryLimit | ||
| { | ||
| int MaxBytesInMemory { get; } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.