laravel-swoole icon indicating copy to clipboard operation
laravel-swoole copied to clipboard

streamDownload not working and sometimes also throw "allowed memory size ... exhausted"

Open jasonbigl opened this issue 2 years ago • 2 comments

Make sure you read Issues Guideline and answer these questions before submitting your issue. Thanks! (Any non-English issues will be closed immediately.)

  1. Please provide your PHP and Swoole version. (php -v and php --ri swoole)
PHP 7.4.21 (cli) (built: Jul 22 2021 02:54:54) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
swoole

Swoole => enabled
Author => Swoole Team <[email protected]>
Version => 4.8.12
Built => Oct  9 2022 10:32:04
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
openssl => OpenSSL 1.1.1n  15 Mar 2022
dtls => enabled
http2 => enabled
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608
  1. Please provide your Laravel/Lumen version.

Laravel 8.83.13

  1. Which release version of this package are you using?

v2.12.1

  1. What did you do? If possible, provide a recipe for reproducing the error.

I want to provide customer a option to export large data from database, but when I tried streamdownload, the application only init the download, but never starting download it. Here is a reproduce code:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Str;
use Illuminate\Support\LazyCollection;

class IndexController extends Controller
{
/**
     * Undocumented function
     *
     * @param Request $request
     * @return void
     */
    public function export(Request $request)
    {
        $collection = LazyCollection::times(100000, fn () => [
            'user_id' => mt_rand(10, 100),
            'name' => Str::random(mt_rand(10, 20)),
            'logged_in_at' => now()->toIsoString(),
        ]);

        return response()->streamDownload(function () use ($collection) {
            $output = fopen('php://output', 'w+');

            fputcsv($output, ['User ID', 'Name', 'Login Time']);

            $collection->each(fn ($row) => fputcsv($output, $row));

            fclose($output);
        }, 'file.csv');
    }
}
  1. What did you expect to see?

download large data by streaming without consuming all memory

  1. What did you see instead?

The browser only init the download, but never starting download content.

https://imgur.com/a/GvGzRng

jasonbigl avatar Dec 14 '22 07:12 jasonbigl

yes that is happend with me not work

0utc0ntr0l2021 avatar Dec 22 '22 02:12 0utc0ntr0l2021

happened with me when laravel handle some operation which needs more memory setting in php.ini

szylmzs avatar Oct 24 '23 06:10 szylmzs