Skip to content

Commit f3f06da

Browse files
committed
change rails context listener to assume thread-safe by default
1 parent 86955ee commit f3f06da

File tree

4 files changed

+42
-30
lines changed

4 files changed

+42
-30
lines changed

src/main/java/org/jruby/rack/RackServletContextListener.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,8 @@ public void contextDestroyed(final ServletContextEvent event) {
6969
protected RackApplicationFactory newApplicationFactory(RackConfig config) {
7070
if (factory != null) return factory; // only != null while testing
7171

72-
final RackApplicationFactory factory = new DefaultRackApplicationFactory();
73-
final Integer maxRuntimes = config.getMaximumRuntimes();
74-
// for backwards compatibility when runtime min/max values not specified
75-
// we assume a single shared (threadsafe) runtime to be used :
76-
if ( maxRuntimes == null || maxRuntimes.intValue() == 1 ) {
72+
final RackApplicationFactory factory = getRealRackApplicationFactoryImpl();
73+
if (useSharedApplication((config))) {
7774
return new SharedRackApplicationFactory(factory);
7875
}
7976
else {
@@ -82,7 +79,16 @@ protected RackApplicationFactory newApplicationFactory(RackConfig config) {
8279
new PoolingRackApplicationFactory(factory) ;
8380
}
8481
}
85-
82+
83+
private static boolean useSharedApplication(final RackConfig config) {
84+
final Integer maxRuntimes = config.getMaximumRuntimes();
85+
return maxRuntimes == null || maxRuntimes == 1;
86+
}
87+
88+
protected RackApplicationFactory getRealRackApplicationFactoryImpl() {
89+
return new DefaultRackApplicationFactory();
90+
}
91+
8692
protected void handleInitializationException(
8793
final Exception e,
8894
final RackApplicationFactory factory,
@@ -96,5 +102,5 @@ protected void handleInitializationException(
96102
// NOTE: factory should have already logged the error ...
97103
rackContext.log(ERROR, "initialization failed", e);
98104
}
99-
105+
100106
}

src/main/java/org/jruby/rack/rails/RailsServletContextListener.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,17 @@
77

88
package org.jruby.rack.rails;
99

10-
import org.jruby.rack.SerialPoolingRackApplicationFactory;
11-
import org.jruby.rack.SharedRackApplicationFactory;
12-
import org.jruby.rack.PoolingRackApplicationFactory;
1310
import org.jruby.rack.RackApplicationFactory;
14-
import org.jruby.rack.RackConfig;
1511
import org.jruby.rack.RackServletContextListener;
1612

1713
/**
1814
*
1915
* @author nicksieger
2016
*/
2117
public class RailsServletContextListener extends RackServletContextListener {
22-
18+
2319
@Override
24-
protected RackApplicationFactory newApplicationFactory(RackConfig config) {
25-
final RackApplicationFactory factory = new RailsRackApplicationFactory();
26-
final Integer maxRuntimes = config.getMaximumRuntimes();
27-
// TODO maybe after Rails 4 is out switch to shared by default as well !
28-
if ( maxRuntimes != null && maxRuntimes.intValue() == 1 ) {
29-
return new SharedRackApplicationFactory(factory);
30-
}
31-
else {
32-
return config.isSerialInitialization() ?
33-
new SerialPoolingRackApplicationFactory(factory) :
34-
new PoolingRackApplicationFactory(factory) ;
35-
}
20+
protected RackApplicationFactory getRealRackApplicationFactoryImpl() {
21+
return new RailsRackApplicationFactory();
3622
}
37-
3823
}

src/spec/ruby/jruby/rack/integration_spec.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,16 @@
8787

8888
let(:servlet_context) { new_servlet_context(base_path) }
8989

90-
it "initializes (pooling by default)" do
90+
it "initializes pooling when min/max set" do
91+
servlet_context.addInitParameter('jruby.min.runtimes', '1')
92+
servlet_context.addInitParameter('jruby.max.runtimes', '2')
93+
9194
listener = org.jruby.rack.rails.RailsServletContextListener.new
9295
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
9396

9497
rack_factory = servlet_context.getAttribute("rack.factory")
9598
rack_factory.should be_a(RackApplicationFactory)
9699
rack_factory.should be_a(PoolingRackApplicationFactory)
97-
rack_factory.should respond_to(:realFactory)
98100
rack_factory.realFactory.should be_a(RailsRackApplicationFactory)
99101

100102
servlet_context.getAttribute("rack.context").should be_a(RackContext)
@@ -103,7 +105,19 @@
103105
rack_factory.getApplication.should be_a(DefaultRackApplication)
104106
end
105107

106-
it "initializes threadsafe!" do
108+
it "initializes shared (thread-safe) by default" do
109+
listener = org.jruby.rack.rails.RailsServletContextListener.new
110+
listener.contextInitialized javax.servlet.ServletContextEvent.new(servlet_context)
111+
112+
rack_factory = servlet_context.getAttribute("rack.factory")
113+
rack_factory.should be_a(RackApplicationFactory)
114+
rack_factory.should be_a(SharedRackApplicationFactory)
115+
rack_factory.realFactory.should be_a(RailsRackApplicationFactory)
116+
117+
rack_factory.getApplication.should be_a(DefaultRackApplication)
118+
end
119+
120+
it "initializes shared (thread-safe) whem max runtimes is 1" do
107121
servlet_context.addInitParameter('jruby.max.runtimes', '1')
108122

109123
listener = org.jruby.rack.rails.RailsServletContextListener.new

src/spec/ruby/rack/servlet_context_listener_spec.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,20 @@
113113
lambda { RailsServletContextListener.new }.should_not raise_error
114114
end
115115

116-
it "pools runtimes by default" do
116+
it "shares a runtime by default" do
117117
factory = RailsServletContextListener.new.
118118
send(:newApplicationFactory, @rack_config)
119-
factory.should be_a(org.jruby.rack.PoolingRackApplicationFactory)
119+
factory.should be_a(org.jruby.rack.SharedRackApplicationFactory)
120120
end
121121

122122
it "pools runtimes when max > 1" do
123+
@rack_config.stub(:getMaximumRuntimes).and_return(2)
124+
factory = RailsServletContextListener.new.
125+
send(:newApplicationFactory, @rack_config)
126+
factory.should be_a(org.jruby.rack.PoolingRackApplicationFactory)
127+
end
128+
129+
it "pools runtimes when max > 1 and serial initialization" do
123130
@rack_config.stub(:getMaximumRuntimes).and_return(3)
124131
@rack_config.stub(:isSerialInitialization).and_return(true)
125132
factory = RailsServletContextListener.new.

0 commit comments

Comments
 (0)