Skip to content

Commit

Permalink
Merge pull request #83 from mreiden/reduce-memory-usage
Browse files Browse the repository at this point in the history
Reduce memory usage of geoip2:update
  • Loading branch information
peter-gribanov authored Aug 19, 2021
2 parents 5710d12 + 7bcae4f commit ace12fd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ You can update several databases:
php bin/console geoip2:update city country
```

Optionally installing splitbrain/php-archive uses significantly less memory when updating a database
and can avoid out of memory errors:

```
composer req splitbrain/php-archive
```

### Download GeoIP database

You can download custom database with console command:
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@
"phpunit/phpunit": "~7.0|~8.0|~9.0",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-phpunit": "^0.12"
},
"suggest": {
"splitbrain/php-archive": "Greatly reduces memory usage for the geoip2:update command"
}
}
31 changes: 22 additions & 9 deletions src/Downloader/MaxMindDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace GpsLab\Bundle\GeoIP2Bundle\Downloader;

use Psr\Log\LoggerInterface;
use splitbrain\PHPArchive\Tar;
use Symfony\Component\Filesystem\Filesystem;

/**
Expand Down Expand Up @@ -67,20 +68,32 @@ public function download(string $url, string $target): void
$this->fs->copy($url, $tmp_zip, true);

$this->logger->debug(sprintf('Download complete to %s', $tmp_zip));
$this->logger->debug(sprintf('De-compressing file to %s', $tmp_unzip));

$this->fs->mkdir(dirname($target), 0755);

// decompress gz file
$zip = new \PharData($tmp_zip);
$tar = $zip->decompress();
if (class_exists(Tar::class)) {
$this->logger->debug(sprintf('Extracting archive file to %s', $tmp_untar));

$this->logger->debug('Decompression complete');
$this->logger->debug(sprintf('Extract tar file to %s', $tmp_untar));
// extract tar.gz archive
$tar = new Tar();
$tar->open($tmp_zip);
$tar->extract($tmp_untar);
$tar->close();
unset($tar);
} else {
$this->logger->debug(sprintf('De-compressing file to %s', $tmp_unzip));

// extract tar archive
$tar->extractTo($tmp_untar);
unset($zip, $tar);
// decompress gz file
$zip = new \PharData($tmp_zip);
$tar = $zip->decompress();

$this->logger->debug('Decompression complete');
$this->logger->debug(sprintf('Extract tar file to %s', $tmp_untar));

// extract tar archive
$tar->extractTo($tmp_untar);
unset($zip, $tar);
}

$this->logger->debug('Tar archive extracted');

Expand Down

0 comments on commit ace12fd

Please sign in to comment.