VeryGoodOpenSource / dart_frog

A fast, minimalistic backend framework for Dart 🎯
https://dartfrog.vgv.dev
MIT License
1.87k stars 150 forks source link

fix: Using too much memory #1102

Open tbm98 opened 1 year ago

tbm98 commented 1 year ago

Description

It is my code

import 'dart:convert';
import 'dart:io';

import 'package:dart_frog/dart_frog.dart';
import 'package:http_parser/http_parser.dart';
import 'package:path/path.dart' as p;
import 'package:http/http.dart' as http;
import 'package:uuid/uuid.dart';

import '../main.dart';

Future<Response> onRequest(RequestContext context) async {
  try {
    final formDataRequest = await context.request.formData();
    final fileUpload = formDataRequest.files.values.first.name;
    final timestamp = Uuid().v1();
    final pathUp = p.join(
        Directory.current.path, 'imagesUp', timestamp.toString() + '.jpg');
    final pathDown = p.join(
        Directory.current.path, 'imagesDown', timestamp.toString() + '.jpg');
    final fbytes = await formDataRequest.files.values.first.readAsBytes();

    await File(pathUp).writeAsBytes(
      fbytes,
      flush: true,
    );

    print('upfile-ok');

    return Response.bytes(body: []);
  } catch (e, s) {
    print('error- $e - $s');
    return Response(statusCode: 500, body: e.toString() + s.toString());
  }
}

it performs the action of saving the image to the hard drive.

With 100 consecutive requests, each request uploading a 2.5MB image, the server will very quickly reach a RAM usage of about 4GB.

If the number of requests is 500 consecutive requests, the RAM usage will be more than 10GB.

renancaraujo commented 1 year ago

Hello @tbm98 ,

Do you experience this on dev server or prod server? Or both?

tbm98 commented 1 year ago

@renancaraujo I just run by dart_frog dev

fit-jose commented 7 months ago

@tbm98 what happens if you dont run the dev server?