actionmailer-balancer 1.2.3 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 71e8a5201cca1f34015fede104510047c24ab1903d396c2a141596ec593bd745
4
- data.tar.gz: 899913a05d263d9fd38a1ad056e47c228bdbf5850bc7c43eca9d050afaa5d99a
3
+ metadata.gz: 31b0398b89e3cd5701358f565f74d7f38d1d2a29dfeb1bf6844d35c032a875f1
4
+ data.tar.gz: cb51555f81e1a0d9afab8bfbfef9af54f4a46c7f1a71de03c677ca9ee1038b3c
5
5
  SHA512:
6
- metadata.gz: 95527b2061f78ac24b876813c540147dcecd8bcfcdfccc140c124bb7637d91a805e582567b9be2a35c5d1ffc70eb86328f0e2f4971a45068a57f0ab5a78ef5e5
7
- data.tar.gz: 8deb4d6f173c18d32ca8598c7842a23f9d834578af3a82512887118fe7c2c5b93ec738d159c1f99cd0b803ee7f1d81fca2512c28f38cc2ba3efbc22f539de1b7
6
+ metadata.gz: a3b2ba56df40593989425eebb058f3e98827bf117b25b0cd092bce9718366e42ce9fcbff7f37cb586ee78fd10219ba42f8bc4912a6f7514231ec65e6f94000b4
7
+ data.tar.gz: 007c96c0664751a74a48bd4b1e89b6e7c1cf9e373ae4f84714649cab32b2d9fa7765710d052ec5bb6cb83a8f67c233cc39d36cdd27939c67870c10b9d185da5e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## [1.3.0] - 2026-11-19
2
+
3
+ ## What's Changed
4
+ * Bump crass from 1.0.7 to 1.0.8 by @dependabot[bot] in https://github.com/mailtrap/actionmailer-balancer/pull/70
5
+ * Fix double callbacks and validate balancer settings by @SergeyKorolev in https://github.com/mailtrap/actionmailer-balancer/pull/66
6
+ * Update development dependencies to patched versions by @mklocek in https://github.com/mailtrap/actionmailer-balancer/pull/72
7
+
8
+ ## New Contributors
9
+ * @SergeyKorolev made their first contribution in https://github.com/mailtrap/actionmailer-balancer/pull/66
10
+
11
+ **Full Changelog**: https://github.com/mailtrap/actionmailer-balancer/compare/v1.2.3...v1.3.0
12
+
1
13
  ## [1.2.3] - 2026-11-12
2
14
 
3
15
  ## What's Changed
data/Gemfile CHANGED
@@ -4,6 +4,8 @@ source 'https://rubygems.org'
4
4
 
5
5
  gemspec
6
6
 
7
+ # rubocop needs it on Ruby 4.0, where benchmark is no longer a default gem; ActiveSupport 8.1 stopped pulling it in.
8
+ gem 'benchmark'
7
9
  gem 'mail'
8
10
  gem 'rake', '~> 13.0'
9
11
  gem 'rspec', '~> 3.0'
data/Gemfile.lock CHANGED
@@ -1,76 +1,73 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- actionmailer-balancer (1.2.3)
4
+ actionmailer-balancer (1.3.0)
5
5
  actionmailer (> 4.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- actionmailer (7.2.3.1)
11
- actionpack (= 7.2.3.1)
12
- actionview (= 7.2.3.1)
13
- activejob (= 7.2.3.1)
14
- activesupport (= 7.2.3.1)
10
+ actionmailer (8.1.4.1)
11
+ actionpack (= 8.1.4.1)
12
+ actionview (= 8.1.4.1)
13
+ activejob (= 8.1.4.1)
14
+ activesupport (= 8.1.4.1)
15
15
  mail (>= 2.8.0)
16
16
  rails-dom-testing (~> 2.2)
17
- actionpack (7.2.3.1)
18
- actionview (= 7.2.3.1)
19
- activesupport (= 7.2.3.1)
20
- cgi
17
+ actionpack (8.1.4.1)
18
+ actionview (= 8.1.4.1)
19
+ activesupport (= 8.1.4.1)
21
20
  nokogiri (>= 1.8.5)
22
- racc
23
- rack (>= 2.2.4, < 3.3)
21
+ rack (>= 2.2.4)
24
22
  rack-session (>= 1.0.1)
25
23
  rack-test (>= 0.6.3)
26
24
  rails-dom-testing (~> 2.2)
27
25
  rails-html-sanitizer (~> 1.6)
28
26
  useragent (~> 0.16)
29
- actionview (7.2.3.1)
30
- activesupport (= 7.2.3.1)
27
+ actionview (8.1.4.1)
28
+ activesupport (= 8.1.4.1)
31
29
  builder (~> 3.1)
32
- cgi
33
30
  erubi (~> 1.11)
34
31
  rails-dom-testing (~> 2.2)
35
- rails-html-sanitizer (~> 1.6)
36
- activejob (7.2.3.1)
37
- activesupport (= 7.2.3.1)
32
+ rails-html-sanitizer (~> 1.6, >= 1.7.2)
33
+ activejob (8.1.4.1)
34
+ activesupport (= 8.1.4.1)
38
35
  globalid (>= 0.3.6)
39
- activesupport (7.2.3.1)
36
+ activesupport (8.1.4.1)
40
37
  base64
41
- benchmark (>= 0.3)
42
38
  bigdecimal
43
39
  concurrent-ruby (~> 1.0, >= 1.3.1)
44
40
  connection_pool (>= 2.2.5)
45
41
  drb
46
42
  i18n (>= 1.6, < 2)
43
+ json
47
44
  logger (>= 1.4.2)
48
- minitest (>= 5.1, < 6)
45
+ minitest (>= 5.1)
49
46
  securerandom (>= 0.3)
50
47
  tzinfo (~> 2.0, >= 2.0.5)
48
+ uri (>= 0.13.1)
51
49
  ast (2.4.2)
52
50
  base64 (0.3.0)
53
51
  benchmark (0.5.0)
54
- bigdecimal (4.0.1)
52
+ bigdecimal (4.1.3)
55
53
  builder (3.3.0)
56
- cgi (0.5.1)
57
- concurrent-ruby (1.3.7)
58
- connection_pool (2.5.5)
59
- crass (1.0.7)
60
- date (3.5.1)
54
+ concurrent-ruby (1.3.8)
55
+ connection_pool (3.0.3)
56
+ crass (1.0.8)
57
+ date (3.6.0)
61
58
  diff-lcs (1.5.1)
62
59
  drb (2.2.3)
63
60
  erubi (1.13.1)
64
- globalid (1.2.1)
61
+ globalid (1.4.0)
65
62
  activesupport (>= 6.1)
66
- i18n (1.14.8)
63
+ i18n (1.15.2)
67
64
  concurrent-ruby (~> 1.0)
68
65
  json (2.7.2)
69
66
  language_server-protocol (3.17.0.3)
70
67
  logger (1.7.0)
71
- loofah (2.25.2)
68
+ loofah (2.25.3)
72
69
  crass (~> 1.0.2)
73
- nokogiri (>= 1.12.0)
70
+ nokogiri (>= 1.19.5)
74
71
  mail (2.9.1)
75
72
  logger
76
73
  mini_mime (>= 0.1.1)
@@ -79,38 +76,48 @@ GEM
79
76
  net-smtp
80
77
  mini_mime (1.1.5)
81
78
  mini_portile2 (2.8.9)
82
- minitest (5.27.0)
83
- net-imap (0.5.15)
79
+ minitest (6.1.0)
80
+ drb (~> 2.0)
81
+ prism (>= 1.5, < 3)
82
+ net-imap (0.6.8)
84
83
  date
85
84
  net-protocol
86
85
  net-pop (0.1.2)
87
86
  net-protocol
88
- net-protocol (0.3.0)
87
+ net-protocol (0.4.0)
89
88
  timeout
90
- net-smtp (0.5.1)
89
+ net-smtp (0.5.2)
91
90
  net-protocol
92
- nokogiri (1.18.10)
91
+ nokogiri (1.19.7)
93
92
  mini_portile2 (~> 2.8.2)
94
93
  racc (~> 1.4)
95
- nokogiri (1.18.10-x86_64-darwin)
94
+ nokogiri (1.19.7-arm64-darwin)
95
+ racc (~> 1.4)
96
+ nokogiri (1.19.7-x86_64-darwin)
97
+ racc (~> 1.4)
98
+ nokogiri (1.19.7-x86_64-linux-gnu)
96
99
  racc (~> 1.4)
97
100
  parallel (1.26.2)
98
101
  parser (3.3.4.2)
99
102
  ast (~> 2.4.1)
100
103
  racc
104
+ prism (2.1.0)
105
+ prism (2.1.0-arm64-darwin)
106
+ prism (2.1.0-x86_64-darwin)
107
+ prism (2.1.0-x86_64-linux-gnu)
101
108
  racc (1.8.1)
102
109
  rack (3.2.8)
103
110
  rack-session (2.1.2)
104
111
  base64 (>= 0.1.0)
105
112
  rack (>= 3.0.0)
106
- rack-test (2.1.0)
113
+ rack-test (2.3.0)
107
114
  rack (>= 1.3)
108
115
  rails-dom-testing (2.3.0)
109
116
  activesupport (>= 5.0.0)
110
117
  minitest
111
118
  nokogiri (>= 1.6)
112
- rails-html-sanitizer (1.7.1)
113
- loofah (~> 2.25, >= 2.25.2)
119
+ rails-html-sanitizer (1.7.2)
120
+ loofah (~> 2.25, >= 2.25.3)
114
121
  nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
115
122
  rainbow (3.1.1)
116
123
  rake (13.2.1)
@@ -152,14 +159,18 @@ GEM
152
159
  tzinfo (2.0.6)
153
160
  concurrent-ruby (~> 1.0)
154
161
  unicode-display_width (2.5.0)
162
+ uri (1.1.2)
155
163
  useragent (0.16.11)
156
164
 
157
165
  PLATFORMS
166
+ arm64-darwin
158
167
  ruby
159
168
  x86_64-darwin-20
169
+ x86_64-linux
160
170
 
161
171
  DEPENDENCIES
162
172
  actionmailer-balancer!
173
+ benchmark
163
174
  mail
164
175
  rake (~> 13.0)
165
176
  rspec (~> 3.0)
@@ -168,4 +179,4 @@ DEPENDENCIES
168
179
  rubocop-rspec
169
180
 
170
181
  BUNDLED WITH
171
- 2.4.12
182
+ 2.7.2
data/README.md CHANGED
@@ -61,6 +61,46 @@ end
61
61
 
62
62
  Now, just use ActionMailer as usual, and mail will be auto-balanced.
63
63
 
64
+ ### Mixing SMTP and API delivery methods
65
+
66
+ Delivery methods don't have to use SMTP. Any delivery method registered with ActionMailer works, including ones that send through a provider's HTTP API. For example, to keep 90% of mail on your current SMTP provider and move 10% to the [Mailtrap Email API](https://github.com/mailtrap/mailtrap-ruby#actionmailer):
67
+
68
+ ```ruby
69
+ # Gemfile
70
+ gem 'actionmailer-balancer'
71
+ gem 'mailtrap' # registers the :mailtrap delivery method
72
+ ```
73
+
74
+ ```ruby
75
+ Your::Application.configure do
76
+ config.action_mailer.delivery_method = :balancer
77
+ config.action_mailer.balancer_settings = {
78
+ delivery_methods: [
79
+ {
80
+ method: :smtp,
81
+ settings: {
82
+ address: 'smtp.example.com',
83
+ port: 587,
84
+ user_name: ENV.fetch('SMTP_USERNAME'),
85
+ password: ENV.fetch('SMTP_PASSWORD'),
86
+ authentication: :plain
87
+ },
88
+ weight: 90
89
+ },
90
+ {
91
+ method: :mailtrap,
92
+ settings: {
93
+ api_key: ENV.fetch('MAILTRAP_API_KEY')
94
+ },
95
+ weight: 10
96
+ }
97
+ ]
98
+ }
99
+ end
100
+ ```
101
+
102
+ `settings` are passed to each delivery method as they are, so use the keys that method expects (for `:mailtrap`, see the [mailtrap gem](https://github.com/mailtrap/mailtrap-ruby#actionmailer)). To move more mail to the new provider, change the weights. A weight of `0` turns a delivery method off without removing its configuration.
103
+
64
104
  ## Development
65
105
 
66
106
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -3,19 +3,21 @@
3
3
  module ActionMailer
4
4
  module Balancer
5
5
  class DeliveryMethod
6
- attr_reader :delivery_methods, :weights_sum
6
+ attr_reader :settings, :delivery_methods, :weights_sum
7
7
 
8
8
  def initialize(settings = {})
9
+ @settings = settings
9
10
  add_delivery_methods(settings)
10
11
  end
11
12
 
13
+ # Switches the message to the chosen delivery method, so observers can see
14
+ # which one was used, and delivers through it directly. Calling
15
+ # message.deliver! instead would run interceptors and observers a second
16
+ # time, since ActionMailer has already run them for this message.
12
17
  def deliver!(message)
13
18
  delivery_method = choose_delivery_method
14
- message.delivery_method(
15
- ::ActionMailer::Base.delivery_methods[delivery_method[:method]],
16
- delivery_method[:settings] || {}
17
- )
18
- message.deliver!
19
+ message.delivery_method(delivery_class(delivery_method[:method]), delivery_method[:settings] || {})
20
+ message.delivery_method.deliver!(message)
19
21
  end
20
22
 
21
23
  private
@@ -24,30 +26,63 @@ module ActionMailer
24
26
  validate_settings!(settings)
25
27
 
26
28
  @delivery_methods = settings[:delivery_methods]
27
- @weights_sum = delivery_methods.sum do |method|
28
- unless method[:weight]
29
- raise ActionMailer::Balancer::SettingsError, "No weight set for delivery method #{method[:method]}"
30
- end
29
+ @weights_sum = delivery_methods.sum { |method| method[:weight] }
30
+ # Weights near Float::MAX add up to Infinity or, with Array#sum's compensated summation, NaN.
31
+ # A huge Integer sum stays finite itself but turns into Infinity once rand scales it.
32
+ raise ActionMailer::Balancer::SettingsError, 'The sum of weights is too large' unless weights_sum.to_f.finite?
33
+ return if weights_sum.positive?
31
34
 
32
- method[:weight]
33
- end
35
+ raise ActionMailer::Balancer::SettingsError, 'At least one delivery method must have a weight above 0'
34
36
  end
35
37
 
36
38
  def choose_delivery_method
37
- target = rand(1..weights_sum)
39
+ target = rand * weights_sum
38
40
  delivery_methods.each do |method|
39
- return method if target <= method[:weight]
41
+ return method if target < method[:weight]
40
42
 
41
43
  target -= method[:weight]
42
44
  end
45
+ # Float rounding can leave target just past the last weight.
46
+ delivery_methods.reverse.find { |method| method[:weight].positive? }
47
+ end
48
+
49
+ def delivery_class(name)
50
+ ::ActionMailer::Base.delivery_methods.fetch(name.to_sym) do
51
+ raise ActionMailer::Balancer::SettingsError,
52
+ "Unknown delivery method #{name.inspect}, " \
53
+ "known methods: #{::ActionMailer::Base.delivery_methods.keys.map(&:inspect).join(", ")}"
54
+ end
43
55
  end
44
56
 
45
57
  def validate_settings!(settings)
46
58
  raise ActionMailer::Balancer::SettingsError, 'No settings set' if settings.empty?
47
59
 
48
- return if settings[:delivery_methods].is_a?(Array) && !settings[:delivery_methods].empty?
60
+ unless settings[:delivery_methods].is_a?(Array) && !settings[:delivery_methods].empty?
61
+ raise ActionMailer::Balancer::SettingsError, 'No delivery methods set'
62
+ end
63
+
64
+ settings[:delivery_methods].each { |method| validate_delivery_method!(method) }
65
+ end
66
+
67
+ def validate_delivery_method!(method)
68
+ name = method[:method]
69
+ unless name.respond_to?(:to_sym)
70
+ raise ActionMailer::Balancer::SettingsError, 'No method set for delivery method'
71
+ end
72
+
73
+ if delivery_class(name) <= DeliveryMethod
74
+ raise ActionMailer::Balancer::SettingsError, "Delivery method #{name} can't be a balancer itself"
75
+ end
76
+
77
+ validate_weight!(name, method[:weight])
78
+ end
79
+
80
+ def validate_weight!(name, weight)
81
+ raise ActionMailer::Balancer::SettingsError, "No weight set for delivery method #{name}" unless weight
82
+ return if weight.is_a?(Numeric) && weight.real? && weight.finite? && !weight.negative?
49
83
 
50
- raise ActionMailer::Balancer::SettingsError, 'No delivery methods set'
84
+ raise ActionMailer::Balancer::SettingsError,
85
+ "Weight for delivery method #{name} must be a finite, non-negative number, got #{weight.inspect}"
51
86
  end
52
87
  end
53
88
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActionMailer
4
4
  module Balancer
5
- VERSION = '1.2.3'
5
+ VERSION = '1.3.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actionmailer-balancer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Railsware Products Studio, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-13 00:00:00.000000000 Z
11
+ date: 2026-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionmailer