Skip to content

Commit

Permalink
Introduce MessageSizeException
Browse files Browse the repository at this point in the history
  • Loading branch information
zio-mitch committed Oct 15, 2024
1 parent 762c5fd commit 5e5f54a
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 25 deletions.
46 changes: 23 additions & 23 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/RawMailerSdk/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ private function send(
$messageSize = $message->getAttachmentsSize();

if ($this->smtpClient instanceof SesClientFacade && $messageSize > static::MAX_ATTACHMENT_SIZE_AWS_SES) {
throw new InvalidArgumentException('Attachment size cannot exceed ' . static::MAX_ATTACHMENT_SIZE_AWS_SES . ' bytes, please use the simple mailer driver instead');
throw new MessageSizeException($messageSize, static::MAX_ATTACHMENT_SIZE_AWS_SES, static::DRIVERS['SES'], static::DRIVERS['STD']);
} else if ($this->smtpClient instanceof SwiftMailerClientFacade && $messageSize > static::MAX_ATTACHMENT_SIZE_SMTP) {
throw new InvalidArgumentException('Attachment size cannot exceed ' . static::MAX_ATTACHMENT_SIZE_SMTP . ' bytes');
throw new MessageSizeException($messageSize, static::MAX_ATTACHMENT_SIZE_SMTP, static::DRIVERS['SES'], null);
}

if ($this->recipientsCatchallDomain) {
Expand Down
34 changes: 34 additions & 0 deletions src/RawMailerSdk/ForksCounter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace multidialogo\RawMailerSdk;

class ForksCounter
{
public static function getApproximativelyAailable(): int
{
// Get max number of user processes from ulimit
$ulimitProcesses = (int)trim(shell_exec('ulimit -u'));
echo "Max user processes (ulimit -u): $ulimitProcesses\n";

// Get max number of open file descriptors
$ulimitFiles = (int)trim(shell_exec('ulimit -n'));
echo "Max open files (ulimit -n): $ulimitFiles\n";

// Get max PID limit from proc file system (Linux-specific)
$pidMax = (int)trim(file_get_contents('/proc/sys/kernel/pid_max'));
echo "Max PID value (pid_max): $pidMax\n";

// Memory available on the system (Linux-specific)
$memInfo = file_get_contents('/proc/meminfo');
preg_match('/MemAvailable:\s+(\d+) kB/', $memInfo, $matches);
$availableMemoryKB = isset($matches[1]) ? (int)$matches[1] : 0;
echo "Available memory (kB): $availableMemoryKB kB\n";

// Total number of available forks depends on the lesser of the ulimit, pid_max, and available system memory.
// This is a rough estimate since each forked process requires memory and resources.
// FIXME: save this to file based on average memory peak
$estimatedForksByMemory = $availableMemoryKB / 20000; // Assume each process uses about 20MB (adjust as necessary)

return min($ulimitProcesses, $pidMax, $estimatedForksByMemory);
}
}
47 changes: 47 additions & 0 deletions src/RawMailerSdk/MessageSizeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace multidialogo\RawMailerSdk;


use InvalidArgumentException;

class MessageSizeException extends InvalidArgumentException
{
private int $messageSize;

private int $maxSize;

private string $driver;

private ?string $suggestedDriver;

public function __construct(int $messageSize, int $maxSize, string $driver, ?string $suggestedDriver)
{
parent::__construct("Message size {$messageSize}, not allowed for driver: {$driver}. Suggested driver: {$suggestedDriver}");

$this->messageSize = $messageSize;
$this->maxSize = $maxSize;
$this->driver = $driver;
$this->suggestedDriver = $suggestedDriver;
}

public function getMessageSize(): int
{
return $this->messageSize;
}

public function getMaxSize(): int
{
return $this->maxSize;
}

public function getDriver(): string
{
return $this->driver;
}

public function getSuggestedDriver(): ?string
{
return $this->suggestedDriver;
}
}

0 comments on commit 5e5f54a

Please sign in to comment.