changed to set and removecancel request added

This commit is contained in:
Clasherzz
2025-02-26 23:25:23 +05:30
parent 5245052d6c
commit 163e5679ce
3 changed files with 60 additions and 58 deletions

View File

@@ -1,13 +1,11 @@
import 'dart:io';
import 'dart:collection';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:http/io_client.dart';
http.Client createHttpClientWithNoSSL() {
var ioClient = HttpClient()
..badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
..badCertificateCallback = (X509Certificate cert, String host, int port) => true;
return IOClient(ioClient);
}
@@ -15,7 +13,7 @@ class HttpClientManager {
static final HttpClientManager _instance = HttpClientManager._internal();
static const int _maxCancelledRequests = 100;
final Map<String, http.Client> _clients = {};
final Queue<String> _cancelledRequests = Queue();
final Set<String> _cancelledRequests = {};
factory HttpClientManager() {
return _instance;
@@ -23,12 +21,8 @@ class HttpClientManager {
HttpClientManager._internal();
http.Client createClient(
String requestId, {
bool noSSL = false,
}) {
final client =
(noSSL && !kIsWeb) ? createHttpClientWithNoSSL() : http.Client();
http.Client createClient(String requestId, {bool noSSL = false}) {
final client = (noSSL && !kIsWeb) ? createHttpClientWithNoSSL() : http.Client();
_clients[requestId] = client;
return client;
}
@@ -38,9 +32,9 @@ class HttpClientManager {
_clients[requestId]?.close();
_clients.remove(requestId);
_cancelledRequests.addLast(requestId);
while (_cancelledRequests.length > _maxCancelledRequests) {
_cancelledRequests.removeFirst();
_cancelledRequests.add(requestId);
if (_cancelledRequests.length > _maxCancelledRequests) {
_cancelledRequests.remove(_cancelledRequests.first);
}
}
}
@@ -49,6 +43,10 @@ class HttpClientManager {
return _cancelledRequests.contains(requestId);
}
void removeCancelledRequest(String requestId) {
_cancelledRequests.remove(requestId);
}
void closeClient(String requestId) {
if (_clients.containsKey(requestId)) {
_clients[requestId]?.close();

View File

@@ -19,6 +19,9 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
bool noSSL = false,
}) async {
if(httpClientManager.wasRequestCancelled(requestId)){
httpClientManager.removeCancelledRequest(requestId);
}
final client = httpClientManager.createClient(requestId, noSSL: noSSL);
(Uri?, String?) uriRec = getValidRequestUri(
@@ -71,7 +74,8 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
}
}
http.StreamedResponse multiPartResponse =
await multiPartRequest.send();
await client.send(multiPartRequest);
stopwatch.stop();
http.Response convertedMultiPartResponse =
await convertStreamedResponse(multiPartResponse);