mail_smtp_connection_pool 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b0058cb8356a709adda99c190df8c3afc3f37eb201c69f705ccf44affb1cc1a3
4
+ data.tar.gz: 52abaf3ecb4912329c65248205a93057ba8b616a6f09d3fd3b2680faa621fc18
5
+ SHA512:
6
+ metadata.gz: f04824f52482621e0392c67049d054eb0152ad8a29d9c9d0722722c11506a0e204baffb4d7a7d2d64813b7523639d831bdc981b858cc50878a6cd4e2dbe366a2
7
+ data.tar.gz: 2c0df938e72fb0ef6a55478cb9b89da05033e733c5b0cc2b75faa85d3a3dfe2a2e5b7786f1f1ca28fde841990b823af8dff64c14e6624737a1ac52f3b6309a52
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A connection object that can be used to deliver mail.
4
+ #
5
+ # This is meant to be used in a pool so the main difference between this
6
+ # and Mail::SMTP is that this expects deliver! to be called multiple times.
7
+ #
8
+ # SMTP connection reset and error handling is handled by this class and
9
+ # the SMTP connection is not closed after a delivery.
10
+
11
+ require 'mail'
12
+
13
+ module Mail
14
+ class SMTPPool
15
+ class Connection < Mail::SMTP
16
+ def initialize(values)
17
+ super
18
+
19
+ @smtp_session = nil
20
+ end
21
+
22
+ def deliver!(mail)
23
+ response = Mail::SMTPConnection.new(connection: smtp_session, return_response: true).deliver!(mail)
24
+
25
+ settings[:return_response] ? response : self
26
+ end
27
+
28
+ def finish
29
+ finish_smtp_session if @smtp_session && @smtp_session.started?
30
+ end
31
+
32
+ private
33
+
34
+ def smtp_session
35
+ return start_smtp_session if @smtp_session.nil? || !@smtp_session.started?
36
+ return @smtp_session if reset_smtp_session
37
+
38
+ finish_smtp_session
39
+ start_smtp_session
40
+ end
41
+
42
+ def start_smtp_session
43
+ @smtp_session = build_smtp_session.start(settings[:domain], settings[:user_name], settings[:password], settings[:authentication])
44
+ end
45
+
46
+ def reset_smtp_session
47
+ !@smtp_session.instance_variable_get(:@error_occurred) && @smtp_session.rset.success?
48
+ rescue Net::SMTPError, IOError
49
+ false
50
+ end
51
+
52
+ def finish_smtp_session
53
+ @smtp_session.finish
54
+ rescue Net::SMTPError, IOError
55
+ ensure
56
+ @smtp_session = nil
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'connection_pool'
4
+ require 'mail/smtp_pool/connection'
5
+
6
+ module Mail
7
+ class SMTPPool
8
+ VERSION = '0.1.0'
9
+
10
+ POOL_DEFAULTS = {
11
+ pool_size: 5,
12
+ pool_timeout: 5
13
+ }.freeze
14
+
15
+ class << self
16
+ def create_pool(settings = {})
17
+ pool_settings = POOL_DEFAULTS.merge(settings)
18
+ smtp_settings = settings.reject { |k, v| POOL_DEFAULTS.keys.include?(k) }
19
+
20
+ ConnectionPool.new(size: pool_settings[:pool_size], timeout: pool_settings[:pool_timeout]) do
21
+ Mail::SMTPPool::Connection.new(smtp_settings)
22
+ end
23
+ end
24
+ end
25
+
26
+ def initialize(settings)
27
+ raise ArgumentError, 'pool is required. You can create one using Mail::SMTPPool.create_pool.' if settings[:pool].nil?
28
+
29
+ @pool = settings[:pool]
30
+ end
31
+
32
+ def deliver!(mail)
33
+ @pool.with { |conn| conn.deliver!(mail) }
34
+ end
35
+
36
+ # This makes it compatible with Mail's `#deliver!` method
37
+ # https://github.com/mikel/mail/blob/22a7afc23f253319965bf9228a0a430eec94e06d/lib/mail/message.rb#L271
38
+ def settings
39
+ {}
40
+ end
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mail_smtp_connection_pool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Fabio Tranjan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: connection_pool
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mail
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.8'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.12.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.12.0
55
+ description:
56
+ email:
57
+ - fabiotranjan@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/mail/smtp_pool.rb
63
+ - lib/mail/smtp_pool/connection.rb
64
+ homepage: https://github.com/FabioTranjan/mail_smtp_connection_pool
65
+ licenses:
66
+ - MIT
67
+ metadata:
68
+ source_code_uri: https://github.com/FabioTranjan/mail_smtp_connection_pool
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubygems_version: 3.5.3
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Mail extension for sending mails using an SMTP connection pool
88
+ test_files: []