mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2026-01-17 22:44:34 +01:00
opt: Better performance on server refreshing (#999)
* refactor(server): Replace Future.wait with an explicit list of futures to enhance readability Refactor the nested map and async functions into explicit for loops and future lists to make the code logic clearer * fix(server): Fixed the auto-refresh logic and concurrency control issues - Add `_refreshCompleter` to prevent concurrent refreshes - Fixed the issue where the status was not updated after the automatic refresh timer was canceled - Remove the invalid check for `duration == 1` * refactor(server): Optimize the server refresh logic by filtering out servers that do not need to be refreshed in advance Move the server filtering logic outside the loop and use the `where` method to filter the servers that need to be refreshed, avoiding repeated condition checks within the loop. This improves code readability and reduces redundant condition checks. * refactor: Optimize server refresh logic to enhance readability Break down complex conditional checks into clearer steps, separating the logic for server refresh and rate limiter reset. Replace chained calls with explicit loops to make the code easier to maintain and understand. * refactor(server): Remove `updateFuture` from `ServerState` and use the `_isRefreshing` flag instead Simplify the server refresh logic, replace Future state tracking with a boolean flag, and avoid unnecessary state updates * refactor(server_detail): Extract the setting items as local variables to improve performance Extract the globally set items that are accessed repeatedly as local variables, reduce unnecessary state retrieval operations, and optimize page performance * refactor: Rename `_displayCpuIndexSetting` to `_displayCpuIndex` for consistency * refactor(server): Fix the issue of parallel blocking in server refresh The original code uses Future.wait to wait for all refresh operations to complete, but in fact, there is no need to wait for the results of these operations. Instead, directly calling ignore() to ignore the results can avoid blocking caused by the slowest server * fix: Adjust the order of logging and default value settings Ensure to set the default value after recording the invalid duration warning * refactor(server): Rename _refreshCompleter to _refreshInProgress to enhance readability Change the variable name from `_refreshCompleter` to `_refreshInProgress`, so that it more accurately reflects the actual purpose of the variable, which is to indicate whether the refresh operation is in progress * refactor(server): Remove unnecessary refresh progress status management Simplify the server refresh logic, remove the unused _refreshInProgress state variable and related Completer handling, making the code more concise and straightforward * chore: Update dependent package versions Update the following dependent package versions: - camera_web has been upgraded from 0.3.5 to 0.3.5+3 - ffi has been upgraded from 2.1.4 to 2.1.5 - hive_ce_flutter is upgraded from 2.3.3 to 2.3.4 - watcher is upgraded from 1.1.4 to 1.2.1 * opt. --------- Co-authored-by: lollipopkit🏳️⚧️ <10864310+lollipopkit@users.noreply.github.com>
This commit is contained in:
@@ -58,7 +58,7 @@ final class ContainerNotifierProvider
|
||||
}
|
||||
}
|
||||
|
||||
String _$containerNotifierHash() => r'fea65e66499234b0a59bffff8d69c4ab8c93b2fd';
|
||||
String _$containerNotifierHash() => r'85457ec75264199c284572ee45beeaccba2044a1';
|
||||
|
||||
final class ContainerNotifierFamily extends $Family
|
||||
with
|
||||
|
||||
@@ -58,7 +58,7 @@ final class PveNotifierProvider
|
||||
}
|
||||
}
|
||||
|
||||
String _$pveNotifierHash() => r'ba5f2d6cb47c33735f7cc09b771b4a86501b86c6';
|
||||
String _$pveNotifierHash() => r'1e71faadee074b9c07bee731ef4ae6505e791967';
|
||||
|
||||
final class PveNotifierFamily extends $Family
|
||||
with $ClassFamilyOverride<PveNotifier, PveState, PveState, PveState, Spi> {
|
||||
|
||||
@@ -103,37 +103,44 @@ class ServersNotifier extends _$ServersNotifier {
|
||||
return;
|
||||
}
|
||||
|
||||
await Future.wait(
|
||||
state.servers.entries.map((entry) async {
|
||||
final serverId = entry.key;
|
||||
final spi = entry.value;
|
||||
final serversToRefresh = <MapEntry<String, Spi>>[];
|
||||
final idsToResetLimiter = <String>[];
|
||||
|
||||
if (onlyFailed) {
|
||||
final serverState = ref.read(serverProvider(serverId));
|
||||
if (serverState.conn != ServerConn.failed) return;
|
||||
TryLimiter.reset(serverId);
|
||||
}
|
||||
for (final entry in state.servers.entries) {
|
||||
final serverId = entry.key;
|
||||
final spi = entry.value;
|
||||
|
||||
if (state.manualDisconnectedIds.contains(serverId)) return;
|
||||
if (state.manualDisconnectedIds.contains(serverId)) continue;
|
||||
|
||||
final serverState = ref.read(serverProvider(serverId));
|
||||
if (serverState.conn == ServerConn.disconnected && !spi.autoConnect) {
|
||||
return;
|
||||
}
|
||||
final serverState = ref.read(serverProvider(serverId));
|
||||
|
||||
final serverNotifier = ref.read(serverProvider(serverId).notifier);
|
||||
await serverNotifier.refresh();
|
||||
}),
|
||||
);
|
||||
if (onlyFailed) {
|
||||
if (serverState.conn != ServerConn.failed) continue;
|
||||
idsToResetLimiter.add(serverId);
|
||||
}
|
||||
|
||||
if (serverState.conn == ServerConn.disconnected && !spi.autoConnect) continue;
|
||||
|
||||
serversToRefresh.add(entry);
|
||||
}
|
||||
|
||||
for (final id in idsToResetLimiter) {
|
||||
TryLimiter.reset(id);
|
||||
}
|
||||
|
||||
for (final entry in serversToRefresh) {
|
||||
final serverNotifier = ref.read(serverProvider(entry.key).notifier);
|
||||
serverNotifier.refresh().ignore();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> startAutoRefresh() async {
|
||||
var duration = Stores.setting.serverStatusUpdateInterval.fetch();
|
||||
stopAutoRefresh();
|
||||
if (duration == 0) return;
|
||||
if (duration < 0 || duration > 10 || duration == 1) {
|
||||
duration = 3;
|
||||
if (duration <= 1 || duration > 10) {
|
||||
Loggers.app.warning('Invalid duration: $duration, use default 3');
|
||||
duration = 3;
|
||||
}
|
||||
final timer = Timer.periodic(Duration(seconds: duration), (_) async {
|
||||
await refresh();
|
||||
@@ -145,8 +152,8 @@ class ServersNotifier extends _$ServersNotifier {
|
||||
final timer = state.autoRefreshTimer;
|
||||
if (timer != null) {
|
||||
timer.cancel();
|
||||
state = state.copyWith(autoRefreshTimer: null);
|
||||
}
|
||||
state = state.copyWith(autoRefreshTimer: null);
|
||||
}
|
||||
|
||||
bool get isAutoRefreshOn => state.autoRefreshTimer != null;
|
||||
|
||||
@@ -41,7 +41,7 @@ final class ServersNotifierProvider
|
||||
}
|
||||
}
|
||||
|
||||
String _$serversNotifierHash() => r'3292bdce7d602ff64687b05ff81d120e71761ec2';
|
||||
String _$serversNotifierHash() => r'dc5da44f9bd8d8dcfba3e6e932cca3e2f379e582';
|
||||
|
||||
abstract class _$ServersNotifier extends $Notifier<ServersState> {
|
||||
ServersState build();
|
||||
|
||||
@@ -35,7 +35,6 @@ abstract class ServerState with _$ServerState {
|
||||
required ServerStatus status,
|
||||
@Default(ServerConn.disconnected) ServerConn conn,
|
||||
SSHClient? client,
|
||||
Future<void>? updateFuture,
|
||||
}) = _ServerState;
|
||||
}
|
||||
|
||||
@@ -81,19 +80,16 @@ class ServerNotifier extends _$ServerNotifier {
|
||||
}
|
||||
|
||||
// Refresh server status
|
||||
bool _isRefreshing = false;
|
||||
|
||||
Future<void> refresh() async {
|
||||
if (state.updateFuture != null) {
|
||||
await state.updateFuture;
|
||||
return;
|
||||
}
|
||||
|
||||
final updateFuture = _updateServer();
|
||||
state = state.copyWith(updateFuture: updateFuture);
|
||||
if (_isRefreshing) return;
|
||||
|
||||
_isRefreshing = true;
|
||||
try {
|
||||
await updateFuture;
|
||||
await _updateServer();
|
||||
} finally {
|
||||
state = state.copyWith(updateFuture: null);
|
||||
_isRefreshing = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ T _$identity<T>(T value) => value;
|
||||
/// @nodoc
|
||||
mixin _$ServerState {
|
||||
|
||||
Spi get spi; ServerStatus get status; ServerConn get conn; SSHClient? get client; Future<void>? get updateFuture;
|
||||
Spi get spi; ServerStatus get status; ServerConn get conn; SSHClient? get client;
|
||||
/// Create a copy of ServerState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@@ -25,16 +25,16 @@ $ServerStateCopyWith<ServerState> get copyWith => _$ServerStateCopyWithImpl<Serv
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return identical(this, other) || (other.runtimeType == runtimeType&&other is ServerState&&(identical(other.spi, spi) || other.spi == spi)&&(identical(other.status, status) || other.status == status)&&(identical(other.conn, conn) || other.conn == conn)&&(identical(other.client, client) || other.client == client)&&(identical(other.updateFuture, updateFuture) || other.updateFuture == updateFuture));
|
||||
return identical(this, other) || (other.runtimeType == runtimeType&&other is ServerState&&(identical(other.spi, spi) || other.spi == spi)&&(identical(other.status, status) || other.status == status)&&(identical(other.conn, conn) || other.conn == conn)&&(identical(other.client, client) || other.client == client));
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(runtimeType,spi,status,conn,client,updateFuture);
|
||||
int get hashCode => Object.hash(runtimeType,spi,status,conn,client);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'ServerState(spi: $spi, status: $status, conn: $conn, client: $client, updateFuture: $updateFuture)';
|
||||
return 'ServerState(spi: $spi, status: $status, conn: $conn, client: $client)';
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ abstract mixin class $ServerStateCopyWith<$Res> {
|
||||
factory $ServerStateCopyWith(ServerState value, $Res Function(ServerState) _then) = _$ServerStateCopyWithImpl;
|
||||
@useResult
|
||||
$Res call({
|
||||
Spi spi, ServerStatus status, ServerConn conn, SSHClient? client, Future<void>? updateFuture
|
||||
Spi spi, ServerStatus status, ServerConn conn, SSHClient? client
|
||||
});
|
||||
|
||||
|
||||
@@ -62,14 +62,13 @@ class _$ServerStateCopyWithImpl<$Res>
|
||||
|
||||
/// Create a copy of ServerState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@pragma('vm:prefer-inline') @override $Res call({Object? spi = null,Object? status = null,Object? conn = null,Object? client = freezed,Object? updateFuture = freezed,}) {
|
||||
@pragma('vm:prefer-inline') @override $Res call({Object? spi = null,Object? status = null,Object? conn = null,Object? client = freezed,}) {
|
||||
return _then(_self.copyWith(
|
||||
spi: null == spi ? _self.spi : spi // ignore: cast_nullable_to_non_nullable
|
||||
as Spi,status: null == status ? _self.status : status // ignore: cast_nullable_to_non_nullable
|
||||
as ServerStatus,conn: null == conn ? _self.conn : conn // ignore: cast_nullable_to_non_nullable
|
||||
as ServerConn,client: freezed == client ? _self.client : client // ignore: cast_nullable_to_non_nullable
|
||||
as SSHClient?,updateFuture: freezed == updateFuture ? _self.updateFuture : updateFuture // ignore: cast_nullable_to_non_nullable
|
||||
as Future<void>?,
|
||||
as SSHClient?,
|
||||
));
|
||||
}
|
||||
/// Create a copy of ServerState
|
||||
@@ -163,10 +162,10 @@ return $default(_that);case _:
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( Spi spi, ServerStatus status, ServerConn conn, SSHClient? client, Future<void>? updateFuture)? $default,{required TResult orElse(),}) {final _that = this;
|
||||
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( Spi spi, ServerStatus status, ServerConn conn, SSHClient? client)? $default,{required TResult orElse(),}) {final _that = this;
|
||||
switch (_that) {
|
||||
case _ServerState() when $default != null:
|
||||
return $default(_that.spi,_that.status,_that.conn,_that.client,_that.updateFuture);case _:
|
||||
return $default(_that.spi,_that.status,_that.conn,_that.client);case _:
|
||||
return orElse();
|
||||
|
||||
}
|
||||
@@ -184,10 +183,10 @@ return $default(_that.spi,_that.status,_that.conn,_that.client,_that.updateFutur
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( Spi spi, ServerStatus status, ServerConn conn, SSHClient? client, Future<void>? updateFuture) $default,) {final _that = this;
|
||||
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( Spi spi, ServerStatus status, ServerConn conn, SSHClient? client) $default,) {final _that = this;
|
||||
switch (_that) {
|
||||
case _ServerState():
|
||||
return $default(_that.spi,_that.status,_that.conn,_that.client,_that.updateFuture);case _:
|
||||
return $default(_that.spi,_that.status,_that.conn,_that.client);case _:
|
||||
throw StateError('Unexpected subclass');
|
||||
|
||||
}
|
||||
@@ -204,10 +203,10 @@ return $default(_that.spi,_that.status,_that.conn,_that.client,_that.updateFutur
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( Spi spi, ServerStatus status, ServerConn conn, SSHClient? client, Future<void>? updateFuture)? $default,) {final _that = this;
|
||||
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( Spi spi, ServerStatus status, ServerConn conn, SSHClient? client)? $default,) {final _that = this;
|
||||
switch (_that) {
|
||||
case _ServerState() when $default != null:
|
||||
return $default(_that.spi,_that.status,_that.conn,_that.client,_that.updateFuture);case _:
|
||||
return $default(_that.spi,_that.status,_that.conn,_that.client);case _:
|
||||
return null;
|
||||
|
||||
}
|
||||
@@ -219,14 +218,13 @@ return $default(_that.spi,_that.status,_that.conn,_that.client,_that.updateFutur
|
||||
|
||||
|
||||
class _ServerState implements ServerState {
|
||||
const _ServerState({required this.spi, required this.status, this.conn = ServerConn.disconnected, this.client, this.updateFuture});
|
||||
const _ServerState({required this.spi, required this.status, this.conn = ServerConn.disconnected, this.client});
|
||||
|
||||
|
||||
@override final Spi spi;
|
||||
@override final ServerStatus status;
|
||||
@override@JsonKey() final ServerConn conn;
|
||||
@override final SSHClient? client;
|
||||
@override final Future<void>? updateFuture;
|
||||
|
||||
/// Create a copy of ServerState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@@ -238,16 +236,16 @@ _$ServerStateCopyWith<_ServerState> get copyWith => __$ServerStateCopyWithImpl<_
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return identical(this, other) || (other.runtimeType == runtimeType&&other is _ServerState&&(identical(other.spi, spi) || other.spi == spi)&&(identical(other.status, status) || other.status == status)&&(identical(other.conn, conn) || other.conn == conn)&&(identical(other.client, client) || other.client == client)&&(identical(other.updateFuture, updateFuture) || other.updateFuture == updateFuture));
|
||||
return identical(this, other) || (other.runtimeType == runtimeType&&other is _ServerState&&(identical(other.spi, spi) || other.spi == spi)&&(identical(other.status, status) || other.status == status)&&(identical(other.conn, conn) || other.conn == conn)&&(identical(other.client, client) || other.client == client));
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(runtimeType,spi,status,conn,client,updateFuture);
|
||||
int get hashCode => Object.hash(runtimeType,spi,status,conn,client);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'ServerState(spi: $spi, status: $status, conn: $conn, client: $client, updateFuture: $updateFuture)';
|
||||
return 'ServerState(spi: $spi, status: $status, conn: $conn, client: $client)';
|
||||
}
|
||||
|
||||
|
||||
@@ -258,7 +256,7 @@ abstract mixin class _$ServerStateCopyWith<$Res> implements $ServerStateCopyWith
|
||||
factory _$ServerStateCopyWith(_ServerState value, $Res Function(_ServerState) _then) = __$ServerStateCopyWithImpl;
|
||||
@override @useResult
|
||||
$Res call({
|
||||
Spi spi, ServerStatus status, ServerConn conn, SSHClient? client, Future<void>? updateFuture
|
||||
Spi spi, ServerStatus status, ServerConn conn, SSHClient? client
|
||||
});
|
||||
|
||||
|
||||
@@ -275,14 +273,13 @@ class __$ServerStateCopyWithImpl<$Res>
|
||||
|
||||
/// Create a copy of ServerState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@override @pragma('vm:prefer-inline') $Res call({Object? spi = null,Object? status = null,Object? conn = null,Object? client = freezed,Object? updateFuture = freezed,}) {
|
||||
@override @pragma('vm:prefer-inline') $Res call({Object? spi = null,Object? status = null,Object? conn = null,Object? client = freezed,}) {
|
||||
return _then(_ServerState(
|
||||
spi: null == spi ? _self.spi : spi // ignore: cast_nullable_to_non_nullable
|
||||
as Spi,status: null == status ? _self.status : status // ignore: cast_nullable_to_non_nullable
|
||||
as ServerStatus,conn: null == conn ? _self.conn : conn // ignore: cast_nullable_to_non_nullable
|
||||
as ServerConn,client: freezed == client ? _self.client : client // ignore: cast_nullable_to_non_nullable
|
||||
as SSHClient?,updateFuture: freezed == updateFuture ? _self.updateFuture : updateFuture // ignore: cast_nullable_to_non_nullable
|
||||
as Future<void>?,
|
||||
as SSHClient?,
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ final class ServerNotifierProvider
|
||||
}
|
||||
}
|
||||
|
||||
String _$serverNotifierHash() => r'185c6b4546c3bc526f5b2ca79d16aed665818863';
|
||||
String _$serverNotifierHash() => r'04b1beef4d96242fd10d5b523c6f5f17eb774bae';
|
||||
|
||||
final class ServerNotifierFamily extends $Family
|
||||
with
|
||||
|
||||
Reference in New Issue
Block a user