Add memory limit option

This commit is contained in:
problematicconsumer
2023-10-26 15:16:25 +03:30
parent f85163476d
commit ab4e6f8b77
14 changed files with 90 additions and 27 deletions

View File

@@ -74,6 +74,23 @@ class EnableAnalytics extends _$EnableAnalytics {
}
}
@Riverpod(keepAlive: true)
class DisableMemoryLimit extends _$DisableMemoryLimit {
late final _pref = Pref(
ref.watch(sharedPreferencesProvider),
"disable_memory_limit",
false,
);
@override
bool build() => _pref.getValue();
Future<void> update(bool value) {
state = value;
return _pref.update(value);
}
}
@Riverpod(keepAlive: true)
class CheckForPreReleaseUpdates extends _$CheckForPreReleaseUpdates {
late final _pref = Pref(

View File

@@ -86,14 +86,19 @@ class CoreFacadeImpl with ExceptionHandler, InfraLogger implements CoreFacade {
}
@override
TaskEither<CoreServiceFailure, Unit> start(String fileName) {
TaskEither<CoreServiceFailure, Unit> start(
String fileName,
bool disableMemoryLimit,
) {
return exceptionHandler(
() {
final configPath = filesEditor.configPath(fileName);
return setup()
.andThen(() => changeConfigOptions(configOptions()))
.andThen(
() => singbox.start(configPath).mapLeft(CoreServiceFailure.start),
() => singbox
.start(configPath, disableMemoryLimit)
.mapLeft(CoreServiceFailure.start),
)
.run();
},
@@ -110,14 +115,18 @@ class CoreFacadeImpl with ExceptionHandler, InfraLogger implements CoreFacade {
}
@override
TaskEither<CoreServiceFailure, Unit> restart(String fileName) {
TaskEither<CoreServiceFailure, Unit> restart(
String fileName,
bool disableMemoryLimit,
) {
return exceptionHandler(
() {
final configPath = filesEditor.configPath(fileName);
return changeConfigOptions(configOptions())
.andThen(
() =>
singbox.restart(configPath).mapLeft(CoreServiceFailure.start),
() => singbox
.restart(configPath, disableMemoryLimit)
.mapLeft(CoreServiceFailure.start),
)
.run();
},

View File

@@ -18,11 +18,17 @@ abstract interface class SingboxFacade {
ConfigOptions options,
);
TaskEither<CoreServiceFailure, Unit> start(String fileName);
TaskEither<CoreServiceFailure, Unit> start(
String fileName,
bool disableMemoryLimit,
);
TaskEither<CoreServiceFailure, Unit> stop();
TaskEither<CoreServiceFailure, Unit> restart(String fileName);
TaskEither<CoreServiceFailure, Unit> restart(
String fileName,
bool disableMemoryLimit,
);
Stream<Either<CoreServiceFailure, List<OutboundGroup>>> watchOutbounds();

View File

@@ -1,3 +1,4 @@
import 'package:hiddify/core/prefs/prefs.dart';
import 'package:hiddify/data/data_providers.dart';
import 'package:hiddify/domain/connectivity/connectivity.dart';
import 'package:hiddify/domain/core_facade.dart';
@@ -50,7 +51,9 @@ class ConnectivityController extends _$ConnectivityController with AppLogger {
return _disconnect();
}
loggy.debug("reconnecting, profile: [$profileId]");
await _core.restart(profileId).mapLeft((err) {
await _core
.restart(profileId, ref.read(disableMemoryLimitProvider))
.mapLeft((err) {
loggy.warning("error reconnecting", err);
state = AsyncError(err, StackTrace.current);
}).run();
@@ -70,8 +73,10 @@ class ConnectivityController extends _$ConnectivityController with AppLogger {
Future<void> _connect() async {
final activeProfile = await ref.read(activeProfileProvider.future);
await _core.start(activeProfile!.id).mapLeft((err) {
loggy.warning("error connecting", err);
await _core
.start(activeProfile!.id, ref.read(disableMemoryLimitProvider))
.mapLeft((err) {
loggy.warning("error connecting $err", err);
state = AsyncError(err, StackTrace.current);
}).run();
}

View File

@@ -18,6 +18,7 @@ class AdvancedSettingTiles extends HookConsumerWidget {
final debug = ref.watch(debugModeNotifierProvider);
final perAppProxy = ref.watch(perAppProxyModeNotifierProvider).enabled;
final disableMemoryLimit = ref.watch(disableMemoryLimitProvider);
return Column(
children: [
@@ -83,6 +84,13 @@ class AdvancedSettingTiles extends HookConsumerWidget {
await ref.read(debugModeNotifierProvider.notifier).update(value);
},
),
SwitchListTile(
title: Text(t.settings.advanced.memoryLimit),
value: !disableMemoryLimit,
onChanged: (value) async {
await ref.read(disableMemoryLimitProvider.notifier).update(!value);
},
),
],
);
}

View File

@@ -930,17 +930,20 @@ class SingboxNativeLibrary {
ffi.Pointer<ffi.Char> start(
ffi.Pointer<ffi.Char> configPath,
int disableMemoryLimit,
) {
return _start(
configPath,
disableMemoryLimit,
);
}
late final _startPtr = _lookup<
ffi.NativeFunction<
ffi.Pointer<ffi.Char> Function(ffi.Pointer<ffi.Char>)>>('start');
ffi.Pointer<ffi.Char> Function(
ffi.Pointer<ffi.Char>, GoUint8)>>('start');
late final _start = _startPtr
.asFunction<ffi.Pointer<ffi.Char> Function(ffi.Pointer<ffi.Char>)>();
.asFunction<ffi.Pointer<ffi.Char> Function(ffi.Pointer<ffi.Char>, int)>();
ffi.Pointer<ffi.Char> stop() {
return _stop();
@@ -952,17 +955,20 @@ class SingboxNativeLibrary {
ffi.Pointer<ffi.Char> restart(
ffi.Pointer<ffi.Char> configPath,
int disableMemoryLimit,
) {
return _restart(
configPath,
disableMemoryLimit,
);
}
late final _restartPtr = _lookup<
ffi.NativeFunction<
ffi.Pointer<ffi.Char> Function(ffi.Pointer<ffi.Char>)>>('restart');
ffi.Pointer<ffi.Char> Function(
ffi.Pointer<ffi.Char>, GoUint8)>>('restart');
late final _restart = _restartPtr
.asFunction<ffi.Pointer<ffi.Char> Function(ffi.Pointer<ffi.Char>)>();
.asFunction<ffi.Pointer<ffi.Char> Function(ffi.Pointer<ffi.Char>, int)>();
ffi.Pointer<ffi.Char> startCommandClient(
int command,

View File

@@ -130,12 +130,16 @@ class FFISingboxService
}
@override
TaskEither<String, Unit> start(String configPath) {
TaskEither<String, Unit> start(String configPath, bool disableMemoryLimit) {
loggy.debug("starting, memory limit: [${!disableMemoryLimit}]");
return TaskEither(
() => CombineWorker().execute(
() {
final err = _box
.start(configPath.toNativeUtf8().cast())
.start(
configPath.toNativeUtf8().cast(),
disableMemoryLimit ? 1 : 0,
)
.cast<Utf8>()
.toDartString();
if (err.isNotEmpty) {
@@ -163,12 +167,16 @@ class FFISingboxService
}
@override
TaskEither<String, Unit> restart(String configPath) {
TaskEither<String, Unit> restart(String configPath, bool disableMemoryLimit) {
loggy.debug("restarting, memory limit: [${!disableMemoryLimit}]");
return TaskEither(
() => CombineWorker().execute(
() {
final err = _box
.restart(configPath.toNativeUtf8().cast())
.restart(
configPath.toNativeUtf8().cast(),
disableMemoryLimit ? 1 : 0,
)
.cast<Utf8>()
.toDartString();
if (err.isNotEmpty) {

View File

@@ -73,7 +73,7 @@ class MobileSingboxService
}
@override
TaskEither<String, Unit> start(String configPath) {
TaskEither<String, Unit> start(String configPath, bool disableMemoryLimit) {
return TaskEither(
() async {
loggy.debug("starting");
@@ -98,7 +98,7 @@ class MobileSingboxService
}
@override
TaskEither<String, Unit> restart(String configPath) {
TaskEither<String, Unit> restart(String configPath, bool disableMemoryLimit) {
return TaskEither(
() async {
loggy.debug("restarting");

View File

@@ -32,11 +32,11 @@ abstract interface class SingboxService {
TaskEither<String, Unit> changeConfigOptions(ConfigOptions options);
TaskEither<String, Unit> start(String configPath);
TaskEither<String, Unit> start(String configPath, bool disableMemoryLimit);
TaskEither<String, Unit> stop();
TaskEither<String, Unit> restart(String configPath);
TaskEither<String, Unit> restart(String configPath, bool disableMemoryLimit);
Stream<String> watchOutbounds();