5

I am working with flutter. And previously my code works very well now. When I debug, I have the error:

Exception has occurred.
SocketException (SocketException: Connection failed (OS Error: Too many open files, errno = 24), address = 192.168.8.102, port = 8080). 

My code

static Future<List> getByPhone(String phone) async {
    http.Response response = await http.get(
        Config.serverAddress + "/**/***/getByPhone?phone=" + phone+"&key="+Config.key,
        headers: Config.userHeader);

    if (response.statusCode == 200) {
      List result = jsonDecode(response.body) as List;
      Config.goodConnection = 1;
      
      return result;
    } 
      Config.goodConnection = 0;
    return [];
  } 

Complete error enter image description here

Omatt
  • 8,564
  • 2
  • 42
  • 144
  • I'm also facing this issue while making large number of http requests at once. Might be a memory issue on device. – iAkshay Oct 29 '20 at 07:36

1 Answers1

0

It's likely that the HttpClient is hitting the max number of live connections. By default, HttpClient.maxConnectionsPerHost is set to null for performance. This can be increased but with a potential performance hit.

class MyHttpOverrides extends HttpOverrides {
  final int maxConnections = 5;
  
  @override
  HttpClient createHttpClient(SecurityContext? context) {
    final HttpClient client = super.createHttpClient(context);
    client.maxConnectionsPerHost = maxConnections;
    return client;
  }
}

To set, configure HttpOverrides.global with

HttpOverrides.global = MyHttpOverrides();
Omatt
  • 8,564
  • 2
  • 42
  • 144