parallel_render 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6398215121b3efd01e0a27b948f3435cf046dc25743ab7315075bf597a7c8ec1
4
+ data.tar.gz: c10449b000119cd347d351bac04acbd2c1f627250b79d1fd7887936994aef53f
5
+ SHA512:
6
+ metadata.gz: 823ba44ce587d228d2057e359d690a21a8390d5d6307f89831a0ea3f7926a7bb0d27d293c3503828bb17085c613349818cd05189391b7a7101083250d62cba56
7
+ data.tar.gz: 921ef567f8c1d86ad3abee2aabb0d3f47870d591cd4520442d8a4276417a7af85e2cddea1198f4fa692dfd5662f04fe0cb3c7080cce841f167e003da647ceae4
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright Igor Kasyanchuk
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # ParallelRender
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "parallel_render"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install parallel_render
22
+ ```
23
+
24
+ ## Benchmark
25
+
26
+ Start server.
27
+
28
+ You can use this tool: https://github.com/hatoo/oha
29
+
30
+ ```bash
31
+ $ oha -n 1000 http://localhost:3000/
32
+ ```
33
+
34
+ ## Contributing
35
+ Contribution directions go here.
36
+
37
+ ## License
38
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module ParallelRender
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ParallelRender
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ParallelRender
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module ParallelRender
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: "from@example.com"
4
+ layout "mailer"
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module ParallelRender
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Parallel render</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= yield :head %>
9
+
10
+ <%= stylesheet_link_tag "parallel_render/application", media: "all" %>
11
+ </head>
12
+ <body>
13
+
14
+ <%= yield %>
15
+
16
+ </body>
17
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ ParallelRender::Engine.routes.draw do
2
+ end
@@ -0,0 +1,5 @@
1
+ module ParallelRender
2
+ class Current < ActiveSupport::CurrentAttributes
3
+ attribute :async_futures
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ module ParallelRender
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace ParallelRender
4
+
5
+ initializer 'parallel_render.middleware' do |app|
6
+ app.middleware.use ParallelRender::Middleware
7
+ end
8
+
9
+ initializer 'parallel_render.helper' do
10
+ ActiveSupport.on_load :action_view do
11
+ include ParallelRender::Helper
12
+
13
+ #include ParallelRender::Helper::ExtendViewContext
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,33 @@
1
+ module ParallelRender
2
+ module Helper
3
+ PLACEHOLDER_PREFIX = "<!--ASYNC-PLACEHOLDER:".freeze
4
+ PLACEHOLDER_SUFFIX = "-->".freeze
5
+
6
+ def parallel_render(partial, locals = {}, context: controller.view_context)
7
+ token = SecureRandom.hex(10)
8
+ placeholder = "#{PLACEHOLDER_PREFIX}#{token}#{PLACEHOLDER_SUFFIX}".html_safe
9
+ futures = ParallelRender::Current.async_futures ||= {}
10
+
11
+ futures[token] = Concurrent::Future.execute(executor: ASYNC_RENDER_EXECUTOR) do
12
+ Rails.application.executor.wrap do
13
+ begin
14
+ # ApplicationController.renderer.render(partial:, locals:)
15
+ context.render(partial:, locals:)
16
+ rescue => e
17
+ Rails.logger.error "Error rendering #{partial}: #{e.message}"
18
+ e.backtrace.each { |line| Rails.logger.error " #{line}" }
19
+ if Rails.env.development?
20
+ "<p style='background-color: red; color: white'>" +
21
+ "Error rendering #{partial}: #{e.message}, #{e.backtrace.join("<br>")}" +
22
+ "</p>"
23
+ else
24
+ raise(e)
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ placeholder
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,52 @@
1
+ # lib/parallel_render/middleware.rb
2
+ require "concurrent"
3
+ require "securerandom"
4
+
5
+ ASYNC_RENDER_EXECUTOR = Concurrent::ThreadPoolExecutor.new(
6
+ min_threads: 2,
7
+ max_threads: 20,
8
+ max_queue: 0,
9
+ fallback_policy: :caller_runs
10
+ )
11
+
12
+ module ParallelRender
13
+ class Middleware
14
+ HTML_TYPE = %r{\Atext/html}.freeze
15
+
16
+ def initialize(app)
17
+ @app = app
18
+ end
19
+
20
+ def call(env)
21
+ status, headers, body = @app.call(env)
22
+
23
+ if html?(headers)
24
+ html = +""
25
+ body.each { |part| html << part }
26
+
27
+ (ParallelRender::Current.async_futures || {}).each do |token, future|
28
+ html_fragment = wait_or_empty(future)
29
+ placeholder = "<!--ASYNC-PLACEHOLDER:#{token}-->"
30
+ html.gsub!(placeholder, html_fragment)
31
+ end
32
+
33
+ headers["Content-Length"] = html.bytesize.to_s if headers["Content-Length"]
34
+ ParallelRender::Current.async_futures = {}
35
+ [status, headers, [html]]
36
+ else
37
+ [status, headers, body]
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def html?(headers)
44
+ headers["Content-Type"]&.match?(HTML_TYPE)
45
+ end
46
+
47
+ def wait_or_empty(future, timeout = ParallelRender.timeout)
48
+ future.wait(timeout)
49
+ future.fulfilled? ? future.value : ""
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module ParallelRender
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,14 @@
1
+ require "parallel_render/version"
2
+ require "parallel_render/engine"
3
+ require "parallel_render/middleware"
4
+ require "parallel_render/helper"
5
+ require "parallel_render/current"
6
+
7
+ module ParallelRender
8
+ mattr_accessor :timeout
9
+ @@timeout = 10
10
+
11
+ def self.configure
12
+ yield self
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :parallel_render do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parallel_render
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Kasyanchuk
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2025-06-24 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rails
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: concurrent-ruby
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rspec-rails
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: kaminari
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ description: Parallel render in Rails
69
+ email:
70
+ - igorkasyanchuk@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - MIT-LICENSE
76
+ - README.md
77
+ - Rakefile
78
+ - app/assets/stylesheets/parallel_render/application.css
79
+ - app/controllers/parallel_render/application_controller.rb
80
+ - app/helpers/parallel_render/application_helper.rb
81
+ - app/jobs/parallel_render/application_job.rb
82
+ - app/mailers/parallel_render/application_mailer.rb
83
+ - app/models/parallel_render/application_record.rb
84
+ - app/views/layouts/parallel_render/application.html.erb
85
+ - config/routes.rb
86
+ - lib/parallel_render.rb
87
+ - lib/parallel_render/current.rb
88
+ - lib/parallel_render/engine.rb
89
+ - lib/parallel_render/helper.rb
90
+ - lib/parallel_render/middleware.rb
91
+ - lib/parallel_render/version.rb
92
+ - lib/tasks/parallel_render_tasks.rake
93
+ homepage: https://github.com/igorkasyanchuk/parallel_render
94
+ licenses:
95
+ - MIT
96
+ metadata:
97
+ homepage_uri: https://github.com/igorkasyanchuk/parallel_render
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubygems_version: 3.6.3
113
+ specification_version: 4
114
+ summary: Parallel render in Rails
115
+ test_files: []