diff --git a/examples/golang-push/rideshare/docker-compose.yml b/examples/golang-push/rideshare/docker-compose.yml index 43d3249c12..2417ad3ec6 100644 --- a/examples/golang-push/rideshare/docker-compose.yml +++ b/examples/golang-push/rideshare/docker-compose.yml @@ -6,8 +6,8 @@ services: environment: - REGION=us-east - PYROSCOPE_SERVER_ADDRESS=http://pyroscope:4040 - - PARAMETERS_POOL_SIZE=1000 - - PARAMETERS_POOL_BUFFER_SIZE_KB=1000 + - PARAMETERS_POOL_SIZE=10000 + - PARAMETERS_POOL_BUFFER_SIZE_KB=10000 build: context: . @@ -35,6 +35,35 @@ services: ports: - '4040:4040' + us-east-ruby: + ports: + - 5000 + environment: + - REGION=us-east + - COMPRESSION=low + build: + context: ./rideshare_rails + links: + - 'pyroscope' + + eu-north-ruby: + ports: + - 5000 + environment: + - REGION=eu-north + build: + context: ./rideshare_rails + + ap-south-ruby: + ports: + - 5000 + environment: + - REGION=ap-south + - COMPRESSION=high + build: + context: ./rideshare_rails + + load-generator: build: context: . diff --git a/examples/golang-push/rideshare/loadgen.go b/examples/golang-push/rideshare/loadgen.go index c8a2ab15c5..a337ee5030 100644 --- a/examples/golang-push/rideshare/loadgen.go +++ b/examples/golang-push/rideshare/loadgen.go @@ -38,6 +38,9 @@ func main() { "us-east", "eu-north", "ap-south", + "us-east-ruby", + "eu-north-ruby", + "ap-south-ruby", } } diff --git a/examples/golang-push/rideshare/rideshare-mem/Dockerfile b/examples/golang-push/rideshare/rideshare-mem/Dockerfile new file mode 100644 index 0000000000..61d47cf3e6 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare-mem/Dockerfile @@ -0,0 +1,12 @@ +FROM ruby:3.0.1 + +WORKDIR /opt/app + +COPY Gemfile ./Gemfile +COPY Gemfile.lock ./Gemfile.lock +# RUN bundle config set --local deployment true +RUN bundle install + +COPY lib ./lib + +CMD [ "ruby", "lib/server.rb" ] diff --git a/examples/golang-push/rideshare/rideshare-mem/Gemfile b/examples/golang-push/rideshare/rideshare-mem/Gemfile new file mode 100644 index 0000000000..7dc01447b5 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare-mem/Gemfile @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } + +# gem "rails" + +gem "pyroscope" +gem "sinatra", "~> 2.1" + +gem "thin", "~> 1.8" diff --git a/examples/golang-push/rideshare/rideshare-mem/Gemfile.lock b/examples/golang-push/rideshare/rideshare-mem/Gemfile.lock new file mode 100644 index 0000000000..edd2dd58c0 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare-mem/Gemfile.lock @@ -0,0 +1,44 @@ +GEM + remote: https://rubygems.org/ + specs: + daemons (1.4.1) + eventmachine (1.2.7) + ffi (1.16.3) + mustermann (1.1.1) + ruby2_keywords (~> 0.0.1) + pyroscope (0.5.10) + ffi + pyroscope (0.5.10-aarch64-linux) + ffi + pyroscope (0.5.10-arm64-darwin) + ffi + pyroscope (0.5.10-x86_64-linux) + ffi + rack (2.2.3) + rack-protection (2.1.0) + rack + ruby2_keywords (0.0.5) + sinatra (2.1.0) + mustermann (~> 1.0) + rack (~> 2.2) + rack-protection (= 2.1.0) + tilt (~> 2.0) + thin (1.8.1) + daemons (~> 1.0, >= 1.0.9) + eventmachine (~> 1.0, >= 1.0.4) + rack (>= 1, < 3) + tilt (2.0.10) + +PLATFORMS + aarch64-linux + arm64-darwin-22 + arm64-linux + x86_64-linux + +DEPENDENCIES + pyroscope + sinatra (~> 2.1) + thin (~> 1.8) + +BUNDLED WITH + 2.2.22 diff --git a/examples/golang-push/rideshare/rideshare-mem/lib/bike/bike.rb b/examples/golang-push/rideshare/rideshare-mem/lib/bike/bike.rb new file mode 100644 index 0000000000..492cfae42b --- /dev/null +++ b/examples/golang-push/rideshare/rideshare-mem/lib/bike/bike.rb @@ -0,0 +1,5 @@ +require_relative '../utility/utility' + +def order_bike(search_radius) + find_nearest_vehicle(search_radius, "bike") +end diff --git a/examples/golang-push/rideshare/rideshare-mem/lib/car/car.rb b/examples/golang-push/rideshare/rideshare-mem/lib/car/car.rb new file mode 100644 index 0000000000..452d78d2df --- /dev/null +++ b/examples/golang-push/rideshare/rideshare-mem/lib/car/car.rb @@ -0,0 +1,5 @@ +require_relative '../utility/utility' + +def order_car(search_radius) + find_nearest_vehicle(search_radius, "car") +end diff --git a/examples/golang-push/rideshare/rideshare-mem/lib/scooter/scooter.rb b/examples/golang-push/rideshare/rideshare-mem/lib/scooter/scooter.rb new file mode 100644 index 0000000000..d9c13ac9da --- /dev/null +++ b/examples/golang-push/rideshare/rideshare-mem/lib/scooter/scooter.rb @@ -0,0 +1,5 @@ +require_relative '../utility/utility' + +def order_scooter(search_radius) + find_nearest_vehicle(search_radius, "scooter") +end diff --git a/examples/golang-push/rideshare/rideshare-mem/lib/server.rb b/examples/golang-push/rideshare/rideshare-mem/lib/server.rb new file mode 100644 index 0000000000..51bf4b0330 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare-mem/lib/server.rb @@ -0,0 +1,36 @@ +require "sinatra" +require "thin" +require "pyroscope" +require_relative 'scooter/scooter' +require_relative 'bike/bike' +require_relative 'car/car' + + +Pyroscope.configure do |config| + config.application_name = "ride-sharing-app-ruby" + config.server_address = "http://pyroscope:4040" + config.tags = { + "region": ENV["REGION"], + } +end + +get "/bike" do + order_bike(0.4) + "

Bike ordered

" +end + +get "/scooter" do + order_scooter(0.6) + "

Scooter ordered

" +end + +get "/car" do + order_car(0.8) + "

Car ordered

" +end + + +set :bind, '0.0.0.0' +set :port, 5000 + +run Sinatra::Application.run! diff --git a/examples/golang-push/rideshare/rideshare-mem/lib/utility/utility.rb b/examples/golang-push/rideshare/rideshare-mem/lib/utility/utility.rb new file mode 100644 index 0000000000..8f1a5edaea --- /dev/null +++ b/examples/golang-push/rideshare/rideshare-mem/lib/utility/utility.rb @@ -0,0 +1,38 @@ +require "pyroscope" + +def mutex_lock(n) + i = 0 + start_time = Time.new + while Time.new - start_time < n * 10 do + i += 1 + end +end + +def check_driver_availability(n) + i = 0 + start_time = Time.new + while Time.new - start_time < n / 2 do + i += 1 + end + + # Every 4 minutes this will artificially create make requests in eu-north region slow + # this is just for demonstration purposes to show how performance impacts show up in the + # flamegraph + current_time = Time.now + current_minute = current_time.strftime('%M').to_i + force_mutex_lock = (current_minute * 4 % 8) == 0 + + mutex_lock(n) if ENV["REGION"] == "eu-north" and force_mutex_lock +end + +def find_nearest_vehicle(n, vehicle) + Pyroscope.tag_wrapper({ "vehicle" => vehicle }) do + i = 0 + start_time = Time.new + while Time.new - start_time < n do + i += 1 + end + + check_driver_availability(n) if vehicle == "car" + end +end diff --git a/examples/golang-push/rideshare/rideshare_rails/.gitattributes b/examples/golang-push/rideshare/rideshare_rails/.gitattributes new file mode 100644 index 0000000000..31eeee0b6a --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/.gitattributes @@ -0,0 +1,7 @@ +# See https://git-scm.com/docs/gitattributes for more about git attribute files. + +# Mark the database schema as having been generated. +db/schema.rb linguist-generated + +# Mark any vendored files as having been vendored. +vendor/* linguist-vendored diff --git a/examples/golang-push/rideshare/rideshare_rails/.gitignore b/examples/golang-push/rideshare/rideshare_rails/.gitignore new file mode 100644 index 0000000000..d94741110f --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/.gitignore @@ -0,0 +1,31 @@ +# See https://help.github.com/articles/ignoring-files for more about ignoring files. +# +# If you find yourself ignoring temporary files generated by your text editor +# or operating system, you probably want to add a global ignore instead: +# git config --global core.excludesfile '~/.gitignore_global' + +# Ignore bundler config. +/.bundle + +# Ignore the default SQLite database. +/db/*.sqlite3 +/db/*.sqlite3-* + +# Ignore all logfiles and tempfiles. +/log/* +/tmp/* +!/log/.keep +!/tmp/.keep + +# Ignore pidfiles, but keep the directory. +/tmp/pids/* +!/tmp/pids/ +!/tmp/pids/.keep + + +/public/assets + +# Ignore master key for decrypting credentials and more. +/config/master.key + +_git diff --git a/examples/golang-push/rideshare/rideshare_rails/Dockerfile b/examples/golang-push/rideshare/rideshare_rails/Dockerfile new file mode 100644 index 0000000000..51349bf3d8 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/Dockerfile @@ -0,0 +1,43 @@ +FROM ruby:3.1.4 + +# RUN apk add --no-cache --update \ +# build-base \ +# linux-headers \ +# git \ +# postgresql-dev \ +# nodejs \ +# yarn \ +# tzdata \ +# graphviz \ +# gmp-dev + +# RUN apk add --no-cache --update \ +# sqlite-dev + +RUN mkdir -p /usr/src/app +WORKDIR /usr/src/app + +ENV RAILS_ENV production +ENV RAILS_SERVE_STATIC_FILES true +ENV RAILS_LOG_TO_STDOUT true + +COPY Gemfile /usr/src/app/ +COPY Gemfile.lock /usr/src/app/ + +RUN bundle config --global frozen 1 +RUN bundle install --without development test + +COPY app /usr/src/app/app +COPY bin /usr/src/app/bin +COPY config /usr/src/app/config +COPY public /usr/src/app/public + +COPY Rakefile /usr/src/app/ +COPY config.ru /usr/src/app/ + +EXPOSE 5000 + +RUN rm -f tmp/pids/server.pid + +CMD ["rails", "s", "-b", "0.0.0.0", "-p", "5000"] + diff --git a/examples/golang-push/rideshare/rideshare_rails/Dockerfile.load-generator b/examples/golang-push/rideshare/rideshare_rails/Dockerfile.load-generator new file mode 100644 index 0000000000..f729376f38 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/Dockerfile.load-generator @@ -0,0 +1,6 @@ +FROM golang:1.19 + + +COPY loadgen.go ./loadgen.go +RUN go build -o /loadgen loadgen.go +CMD [ "/loadgen" ] diff --git a/examples/golang-push/rideshare/rideshare_rails/Gemfile b/examples/golang-push/rideshare/rideshare_rails/Gemfile new file mode 100644 index 0000000000..ea6fef40fe --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/Gemfile @@ -0,0 +1,37 @@ +source "https://rubygems.org" +git_source(:github) { |repo| "https://github.com/#{repo}.git" } + +# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" +gem "rails", "~> 7.0.2", ">= 7.0.2.3" + +# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails] +gem "sprockets-rails" + +# Use sqlite3 as the database for Active Record +gem "sqlite3", "~> 1.4" + +# Use the Puma web server [https://github.com/puma/puma] +gem "puma", "~> 5.0" + +gem "brotli" + +gem "pyroscope" + +gem "pyroscope-otel" +gem 'opentelemetry-sdk', "~> 1.2.0" +gem 'opentelemetry-exporter-jaeger', '~> 0.22.0' +gem 'opentelemetry-instrumentation-rails' # it's top span is "http get" which is not super usefull for demo + + +# Windows does not include zoneinfo files, so bundle the tzinfo-data gem +gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] + +group :development, :test do + # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem + gem "debug", platforms: %i[ mri mingw x64_mingw ] +end + +group :development do + # Speed up commands on slow machines / big apps [https://github.com/rails/spring] + # gem "spring" +end diff --git a/examples/golang-push/rideshare/rideshare_rails/Gemfile.lock b/examples/golang-push/rideshare/rideshare_rails/Gemfile.lock new file mode 100644 index 0000000000..ff0cf7a2b5 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/Gemfile.lock @@ -0,0 +1,252 @@ +GEM + remote: https://rubygems.org/ + specs: + actioncable (7.0.2.3) + actionpack (= 7.0.2.3) + activesupport (= 7.0.2.3) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + actionmailbox (7.0.2.3) + actionpack (= 7.0.2.3) + activejob (= 7.0.2.3) + activerecord (= 7.0.2.3) + activestorage (= 7.0.2.3) + activesupport (= 7.0.2.3) + mail (>= 2.7.1) + net-imap + net-pop + net-smtp + actionmailer (7.0.2.3) + actionpack (= 7.0.2.3) + actionview (= 7.0.2.3) + activejob (= 7.0.2.3) + activesupport (= 7.0.2.3) + mail (~> 2.5, >= 2.5.4) + net-imap + net-pop + net-smtp + rails-dom-testing (~> 2.0) + actionpack (7.0.2.3) + actionview (= 7.0.2.3) + activesupport (= 7.0.2.3) + rack (~> 2.0, >= 2.2.0) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.2.0) + actiontext (7.0.2.3) + actionpack (= 7.0.2.3) + activerecord (= 7.0.2.3) + activestorage (= 7.0.2.3) + activesupport (= 7.0.2.3) + globalid (>= 0.6.0) + nokogiri (>= 1.8.5) + actionview (7.0.2.3) + activesupport (= 7.0.2.3) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.1, >= 1.2.0) + activejob (7.0.2.3) + activesupport (= 7.0.2.3) + globalid (>= 0.3.6) + activemodel (7.0.2.3) + activesupport (= 7.0.2.3) + activerecord (7.0.2.3) + activemodel (= 7.0.2.3) + activesupport (= 7.0.2.3) + activestorage (7.0.2.3) + actionpack (= 7.0.2.3) + activejob (= 7.0.2.3) + activerecord (= 7.0.2.3) + activesupport (= 7.0.2.3) + marcel (~> 1.0) + mini_mime (>= 1.1.0) + activesupport (7.0.2.3) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 1.6, < 2) + minitest (>= 5.1) + tzinfo (~> 2.0) + brotli (0.4.0) + builder (3.2.4) + concurrent-ruby (1.1.10) + crass (1.0.6) + debug (1.5.0) + irb (>= 1.3.6) + reline (>= 0.2.7) + digest (3.1.0) + erubi (1.10.0) + ffi (1.16.3) + globalid (1.0.0) + activesupport (>= 5.0) + i18n (1.10.0) + concurrent-ruby (~> 1.0) + io-console (0.5.11) + irb (1.4.1) + reline (>= 0.3.0) + loofah (2.16.0) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + mail (2.7.1) + mini_mime (>= 0.1.1) + marcel (1.0.2) + method_source (1.0.0) + mini_mime (1.1.2) + minitest (5.15.0) + net-imap (0.2.3) + digest + net-protocol + strscan + net-pop (0.1.1) + digest + net-protocol + timeout + net-protocol (0.1.3) + timeout + net-smtp (0.3.1) + digest + net-protocol + timeout + nio4r (2.5.8) + nokogiri (1.15.4-aarch64-linux) + racc (~> 1.4) + nokogiri (1.15.4-arm-linux) + racc (~> 1.4) + nokogiri (1.15.4-arm64-darwin) + racc (~> 1.4) + nokogiri (1.15.4-x86_64-linux) + racc (~> 1.4) + opentelemetry-api (1.1.0) + opentelemetry-common (0.19.6) + opentelemetry-api (~> 1.0) + opentelemetry-exporter-jaeger (0.22.0) + opentelemetry-api (~> 1.1) + opentelemetry-common (~> 0.19.6) + opentelemetry-sdk (~> 1.2) + opentelemetry-semantic_conventions + thrift + opentelemetry-instrumentation-action_pack (0.2.1) + opentelemetry-api (~> 1.0) + opentelemetry-instrumentation-base (~> 0.21.0) + opentelemetry-instrumentation-rack (~> 0.21.0) + opentelemetry-instrumentation-action_view (0.3.0) + opentelemetry-api (~> 1.0) + opentelemetry-instrumentation-active_support (~> 0.1) + opentelemetry-instrumentation-base (~> 0.20) + opentelemetry-instrumentation-active_record (0.4.0) + opentelemetry-api (~> 1.0) + opentelemetry-instrumentation-base (~> 0.21.0) + ruby2_keywords + opentelemetry-instrumentation-active_support (0.2.0) + opentelemetry-api (~> 1.0) + opentelemetry-instrumentation-base (~> 0.21.0) + opentelemetry-instrumentation-base (0.21.0) + opentelemetry-api (~> 1.0) + opentelemetry-registry (~> 0.1) + opentelemetry-instrumentation-rack (0.21.1) + opentelemetry-api (~> 1.0) + opentelemetry-common (~> 0.19.3) + opentelemetry-instrumentation-base (~> 0.21.0) + opentelemetry-instrumentation-rails (0.22.0) + opentelemetry-api (~> 1.0) + opentelemetry-instrumentation-action_pack (~> 0.2.0) + opentelemetry-instrumentation-action_view (~> 0.3.0) + opentelemetry-instrumentation-active_record (~> 0.4.0) + opentelemetry-instrumentation-active_support (~> 0.2.0) + opentelemetry-instrumentation-base (~> 0.21.0) + opentelemetry-registry (0.2.0) + opentelemetry-api (~> 1.1) + opentelemetry-sdk (1.2.0) + opentelemetry-api (~> 1.1) + opentelemetry-common (~> 0.19.3) + opentelemetry-registry (~> 0.2) + opentelemetry-semantic_conventions + opentelemetry-semantic_conventions (1.8.0) + opentelemetry-api (~> 1.0) + puma (5.6.4) + nio4r (~> 2.0) + pyroscope (0.5.10) + ffi + pyroscope (0.5.10-aarch64-linux) + ffi + pyroscope (0.5.10-arm64-darwin) + ffi + pyroscope (0.5.10-x86_64-linux) + ffi + pyroscope-otel (0.1.1) + opentelemetry-api (~> 1.1.0) + pyroscope (~> 0.5.1) + racc (1.7.3) + rack (2.2.3) + rack-test (1.1.0) + rack (>= 1.0, < 3) + rails (7.0.2.3) + actioncable (= 7.0.2.3) + actionmailbox (= 7.0.2.3) + actionmailer (= 7.0.2.3) + actionpack (= 7.0.2.3) + actiontext (= 7.0.2.3) + actionview (= 7.0.2.3) + activejob (= 7.0.2.3) + activemodel (= 7.0.2.3) + activerecord (= 7.0.2.3) + activestorage (= 7.0.2.3) + activesupport (= 7.0.2.3) + bundler (>= 1.15.0) + railties (= 7.0.2.3) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.4.2) + loofah (~> 2.3) + railties (7.0.2.3) + actionpack (= 7.0.2.3) + activesupport (= 7.0.2.3) + method_source + rake (>= 12.2) + thor (~> 1.0) + zeitwerk (~> 2.5) + rake (13.0.6) + reline (0.3.1) + io-console (~> 0.5) + ruby2_keywords (0.0.5) + sprockets (4.0.3) + concurrent-ruby (~> 1.0) + rack (> 1, < 3) + sprockets-rails (3.4.2) + actionpack (>= 5.2) + activesupport (>= 5.2) + sprockets (>= 3.0.0) + sqlite3 (1.4.2) + strscan (3.0.1) + thor (1.2.1) + thrift (0.17.0) + timeout (0.2.0) + tzinfo (2.0.4) + concurrent-ruby (~> 1.0) + websocket-driver (0.7.5) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.5) + zeitwerk (2.5.4) + +PLATFORMS + aarch64-linux + arm64-darwin-22 + arm64-linux + x86_64-linux + +DEPENDENCIES + brotli + debug + opentelemetry-exporter-jaeger (~> 0.22.0) + opentelemetry-instrumentation-rails + opentelemetry-sdk (~> 1.2.0) + puma (~> 5.0) + pyroscope + pyroscope-otel + rails (~> 7.0.2, >= 7.0.2.3) + sprockets-rails + sqlite3 (~> 1.4) + tzinfo-data + +BUNDLED WITH + 2.3.7 diff --git a/examples/golang-push/rideshare/rideshare_rails/README.md b/examples/golang-push/rideshare/rideshare_rails/README.md new file mode 100644 index 0000000000..156ceb75e2 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/README.md @@ -0,0 +1,12 @@ +# Rails rideshare example + +``` +# Pull latest pyroscope image: +docker pull pyroscope/pyroscope:latest + +# Run the example project: +docker-compose up --build + +# Reset the database (if needed): +docker-compose down +``` diff --git a/examples/golang-push/rideshare/rideshare_rails/Rakefile b/examples/golang-push/rideshare/rideshare_rails/Rakefile new file mode 100644 index 0000000000..9a5ea7383a --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/Rakefile @@ -0,0 +1,6 @@ +# Add your own tasks in files placed in lib/tasks ending in .rake, +# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. + +require_relative "config/application" + +Rails.application.load_tasks diff --git a/examples/golang-push/rideshare/rideshare_rails/app/assets/config/manifest.js b/examples/golang-push/rideshare/rideshare_rails/app/assets/config/manifest.js new file mode 100644 index 0000000000..cc060c5c9a --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/assets/config/manifest.js @@ -0,0 +1,2 @@ +// = link_tree ../images +// = link_directory ../stylesheets .css diff --git a/examples/golang-push/rideshare/rideshare_rails/app/assets/images/.keep b/examples/golang-push/rideshare/rideshare_rails/app/assets/images/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/golang-push/rideshare/rideshare_rails/app/assets/stylesheets/application.css b/examples/golang-push/rideshare/rideshare_rails/app/assets/stylesheets/application.css new file mode 100644 index 0000000000..288b9ab718 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/assets/stylesheets/application.css @@ -0,0 +1,15 @@ +/* + * This is a manifest file that'll be compiled into application.css, which will include all the files + * listed below. + * + * Any CSS (and SCSS, if configured) file within this directory, lib/assets/stylesheets, or any plugin's + * vendor/assets/stylesheets directory can be referenced here using a relative path. + * + * You're free to add application-wide styles to this file and they'll appear at the bottom of the + * compiled file so the styles you add here take precedence over styles defined in any other CSS + * files in this directory. Styles in this file should be added after the last require_* statement. + * It is generally better to create a new file per style scope. + * + *= require_tree . + *= require_self + */ diff --git a/examples/golang-push/rideshare/rideshare_rails/app/controllers/application_controller.rb b/examples/golang-push/rideshare/rideshare_rails/app/controllers/application_controller.rb new file mode 100644 index 0000000000..461a1751d8 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/controllers/application_controller.rb @@ -0,0 +1,15 @@ +class ApplicationController < ActionController::Base + include Logging::LoggingHelper + + around_action :trace_action + + private + + def trace_action + Pyroscope.tag_wrapper({ "vehicle" => action_name }) do + OpenTelemetry.tracer_provider.tracer('my-tracer').in_span(controller_name.classify) do |_| + yield + end + end + end +end diff --git a/examples/golang-push/rideshare/rideshare_rails/app/controllers/bike_controller.rb b/examples/golang-push/rideshare/rideshare_rails/app/controllers/bike_controller.rb new file mode 100644 index 0000000000..f9cb48aa57 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/controllers/bike_controller.rb @@ -0,0 +1,8 @@ +class BikeController < ApplicationController + def index + helpers.find_nearest_vehicle( 0.4, "bike") + i = 0; while i < MULTIPLIER * 3; i += 1; end + logger_debug "Bike ordered" + render html: "Bike ordered" + end +end diff --git a/examples/golang-push/rideshare/rideshare_rails/app/controllers/car_controller.rb b/examples/golang-push/rideshare/rideshare_rails/app/controllers/car_controller.rb new file mode 100644 index 0000000000..65aae41f69 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/controllers/car_controller.rb @@ -0,0 +1,8 @@ +class CarController < ApplicationController + def index + helpers.find_nearest_vehicle(0.5, "car") + i = 0; while i < MULTIPLIER * 3; i += 1; end + logger_debug "Car ordered" + render html: "Car ordered" + end +end diff --git a/examples/golang-push/rideshare/rideshare_rails/app/controllers/concerns/.keep b/examples/golang-push/rideshare/rideshare_rails/app/controllers/concerns/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/golang-push/rideshare/rideshare_rails/app/controllers/scooter_controller.rb b/examples/golang-push/rideshare/rideshare_rails/app/controllers/scooter_controller.rb new file mode 100644 index 0000000000..9c8bf1f212 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/controllers/scooter_controller.rb @@ -0,0 +1,8 @@ +class ScooterController < ApplicationController + def index + helpers.find_nearest_vehicle( 0.6, "scooter") + i = 0; while i < MULTIPLIER * 3; i += 1; end + logger_debug "Scooter ordered" + render html: "Scooter ordered" + end +end diff --git a/examples/golang-push/rideshare/rideshare_rails/app/helpers/application_helper.rb b/examples/golang-push/rideshare/rideshare_rails/app/helpers/application_helper.rb new file mode 100644 index 0000000000..2b72dcbb97 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/helpers/application_helper.rb @@ -0,0 +1,39 @@ + +require "pyroscope" + +module ApplicationHelper + extend Drivers::DriversHelper + extend Strings::StringsHelper + extend Logging::LoggingHelper + extend Mutexes::MutexesHelper + + def check_driver_availability(n) + current_minute = Time.new.strftime('%M').to_i + force_mutex_lock = (current_minute % 2) == 0 + + i = 0; while i < MULTIPLIER * 5; i += 1; end + mutex_lock(n) if ENV["REGION"] == "eu-north" and force_mutex_lock + end + + def find_nearest_vehicle(n, vehicle) + logger_debug("find_nearest_vehicle") + i = 0; while i < MULTIPLIER * 0.25; i += 1; end + traverse_vehicle_options(n, vehicle) + end + + def traverse_vehicle_options(n, vehicle) + logger_debug("traverse_vehicle_options") + i = 0; while i < MULTIPLIER * 0.33; i += 1; end + compile_options(n, vehicle) + end + + def compile_options(n, vehicle) + logger_debug("compile_options") + i = 0; while i < MULTIPLIER * 0.2; i += 1; end + build_list_of_options + if vehicle == "car" + check_driver_availability(n) + prepare_drivers_response + end + end +end diff --git a/examples/golang-push/rideshare/rideshare_rails/app/helpers/bike_helper.rb b/examples/golang-push/rideshare/rideshare_rails/app/helpers/bike_helper.rb new file mode 100644 index 0000000000..554085ae51 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/helpers/bike_helper.rb @@ -0,0 +1,2 @@ +module BikeHelper +end diff --git a/examples/golang-push/rideshare/rideshare_rails/app/helpers/car_helper.rb b/examples/golang-push/rideshare/rideshare_rails/app/helpers/car_helper.rb new file mode 100644 index 0000000000..94f1fab251 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/helpers/car_helper.rb @@ -0,0 +1,2 @@ +module CarHelper +end diff --git a/examples/golang-push/rideshare/rideshare_rails/app/helpers/drivers/drivers_helper.rb b/examples/golang-push/rideshare/rideshare_rails/app/helpers/drivers/drivers_helper.rb new file mode 100644 index 0000000000..8a3a4c7810 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/helpers/drivers/drivers_helper.rb @@ -0,0 +1,36 @@ +module Drivers + module DriversHelper + extend Logging::LoggingHelper + BODY = (SecureRandom.hex(1024) * 1000000)[0,MULTIPLIER*800] + + + def prepare_drivers_response + logger_debug("prepare_drivers_response") + i = 0; t = rand(MULTIPLIER * 1.2); while i < t; i += 1; end + generate_drivers_data + end + + def generate_drivers_data + logger_debug("generate_drivers_data") + i = 0; t = rand(MULTIPLIER * 1.0); while i < t; i += 1; end + prepare_response_body + end + + def prepare_response_body + logger_debug("prepare_response_body") + i = 0; t = rand(MULTIPLIER * 0.8); while i < t; i += 1; end + compress_response + end + + def compress_response + logger_debug("compress_response") + i = 0; t = rand(MULTIPLIER * 1.0); while i < t; i += 1; end + quality = ENV["COMPRESSION"] == "low" ? 2 : 11 + i=0 + while i < 4 + Brotli.deflate(BODY, :quality => quality) + i+=1 + end + end + end +end diff --git a/examples/golang-push/rideshare/rideshare_rails/app/helpers/logging/logging_helper.rb b/examples/golang-push/rideshare/rideshare_rails/app/helpers/logging/logging_helper.rb new file mode 100644 index 0000000000..3113d2848e --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/helpers/logging/logging_helper.rb @@ -0,0 +1,14 @@ +module Logging + module LoggingHelper + def logger_debug(str) + Rails.logger.info(str) + i = 0; while i < MULTIPLIER; i += 1; end + + # (MULTIPLIER / 100000).times do + # str += Drivers::DriversHelper::BODY[0, 1024] + # end + + # Rails.logger.debug(str) + end + end +end diff --git a/examples/golang-push/rideshare/rideshare_rails/app/helpers/mutexes/mutexes_helper.rb b/examples/golang-push/rideshare/rideshare_rails/app/helpers/mutexes/mutexes_helper.rb new file mode 100644 index 0000000000..4ef181a60f --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/helpers/mutexes/mutexes_helper.rb @@ -0,0 +1,9 @@ +module Mutexes + module MutexesHelper + extend Logging::LoggingHelper + def mutex_lock(n) + logger_debug("mutex lock") + i = 0; while i < MULTIPLIER * 5 * n*5; i += 1; end + end + end +end diff --git a/examples/golang-push/rideshare/rideshare_rails/app/helpers/scooter_helper.rb b/examples/golang-push/rideshare/rideshare_rails/app/helpers/scooter_helper.rb new file mode 100644 index 0000000000..a0edc34640 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/helpers/scooter_helper.rb @@ -0,0 +1,2 @@ +module ScooterHelper +end diff --git a/examples/golang-push/rideshare/rideshare_rails/app/helpers/strings/strings_helper.rb b/examples/golang-push/rideshare/rideshare_rails/app/helpers/strings/strings_helper.rb new file mode 100644 index 0000000000..97c1dd5d52 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/helpers/strings/strings_helper.rb @@ -0,0 +1,13 @@ +module Strings + module StringsHelper + extend Logging::LoggingHelper + def build_list_of_options + str = "" + i = 0 + while i < MULTIPLIER / 1000 + str += Drivers::DriversHelper::BODY[0, 1024] + i += 1 + end + end + end +end diff --git a/examples/golang-push/rideshare/rideshare_rails/app/models/application_record.rb b/examples/golang-push/rideshare/rideshare_rails/app/models/application_record.rb new file mode 100644 index 0000000000..b63caeb8a5 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/models/application_record.rb @@ -0,0 +1,3 @@ +class ApplicationRecord < ActiveRecord::Base + primary_abstract_class +end diff --git a/examples/golang-push/rideshare/rideshare_rails/app/models/concerns/.keep b/examples/golang-push/rideshare/rideshare_rails/app/models/concerns/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/golang-push/rideshare/rideshare_rails/app/views/layouts/application.html.erb b/examples/golang-push/rideshare/rideshare_rails/app/views/layouts/application.html.erb new file mode 100644 index 0000000000..a9b67b1a13 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/app/views/layouts/application.html.erb @@ -0,0 +1,15 @@ + + + + RideshareRails + + <%= csrf_meta_tags %> + <%= csp_meta_tag %> + + <%= stylesheet_link_tag "application" %> + + + + <%= yield %> + + diff --git a/examples/golang-push/rideshare/rideshare_rails/bin/bundle b/examples/golang-push/rideshare/rideshare_rails/bin/bundle new file mode 100755 index 0000000000..5b593cb62d --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/bin/bundle @@ -0,0 +1,114 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'bundle' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +require "rubygems" + +m = Module.new do + module_function + + def invoked_as_script? + File.expand_path($0) == File.expand_path(__FILE__) + end + + def env_var_version + ENV["BUNDLER_VERSION"] + end + + def cli_arg_version + return unless invoked_as_script? # don't want to hijack other binstubs + return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update` + bundler_version = nil + update_index = nil + ARGV.each_with_index do |a, i| + if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN + bundler_version = a + end + next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/ + bundler_version = $1 + update_index = i + end + bundler_version + end + + def gemfile + gemfile = ENV["BUNDLE_GEMFILE"] + return gemfile if gemfile && !gemfile.empty? + + File.expand_path("../../Gemfile", __FILE__) + end + + def lockfile + lockfile = + case File.basename(gemfile) + when "gems.rb" then gemfile.sub(/\.rb$/, gemfile) + else "#{gemfile}.lock" + end + File.expand_path(lockfile) + end + + def lockfile_version + return unless File.file?(lockfile) + lockfile_contents = File.read(lockfile) + return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/ + Regexp.last_match(1) + end + + def bundler_requirement + @bundler_requirement ||= + env_var_version || cli_arg_version || + bundler_requirement_for(lockfile_version) + end + + def bundler_requirement_for(version) + return "#{Gem::Requirement.default}.a" unless version + + bundler_gem_version = Gem::Version.new(version) + + requirement = bundler_gem_version.approximate_recommendation + + return requirement unless Gem.rubygems_version < Gem::Version.new("2.7.0") + + requirement += ".a" if bundler_gem_version.prerelease? + + requirement + end + + def load_bundler! + ENV["BUNDLE_GEMFILE"] ||= gemfile + + activate_bundler + end + + def activate_bundler + gem_error = activation_error_handling do + gem "bundler", bundler_requirement + end + return if gem_error.nil? + require_error = activation_error_handling do + require "bundler/version" + end + return if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION)) + warn "Activating bundler (#{bundler_requirement}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_requirement}'`" + exit 42 + end + + def activation_error_handling + yield + nil + rescue StandardError, LoadError => e + e + end +end + +m.load_bundler! + +if m.invoked_as_script? + load Gem.bin_path("bundler", "bundle") +end diff --git a/examples/golang-push/rideshare/rideshare_rails/bin/rails b/examples/golang-push/rideshare/rideshare_rails/bin/rails new file mode 100755 index 0000000000..efc0377492 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/bin/rails @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +APP_PATH = File.expand_path("../config/application", __dir__) +require_relative "../config/boot" +require "rails/commands" diff --git a/examples/golang-push/rideshare/rideshare_rails/bin/rake b/examples/golang-push/rideshare/rideshare_rails/bin/rake new file mode 100755 index 0000000000..4fbf10b960 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/bin/rake @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +require_relative "../config/boot" +require "rake" +Rake.application.run diff --git a/examples/golang-push/rideshare/rideshare_rails/bin/setup b/examples/golang-push/rideshare/rideshare_rails/bin/setup new file mode 100755 index 0000000000..ec47b79b3b --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/bin/setup @@ -0,0 +1,33 @@ +#!/usr/bin/env ruby +require "fileutils" + +# path to your application root. +APP_ROOT = File.expand_path("..", __dir__) + +def system!(*args) + system(*args) || abort("\n== Command #{args} failed ==") +end + +FileUtils.chdir APP_ROOT do + # This script is a way to set up or update your development environment automatically. + # This script is idempotent, so that you can run it at any time and get an expectable outcome. + # Add necessary setup steps to this file. + + puts "== Installing dependencies ==" + system! "gem install bundler --conservative" + system("bundle check") || system!("bundle install") + + # puts "\n== Copying sample files ==" + # unless File.exist?("config/database.yml") + # FileUtils.cp "config/database.yml.sample", "config/database.yml" + # end + + puts "\n== Preparing database ==" + system! "bin/rails db:prepare" + + puts "\n== Removing old logs and tempfiles ==" + system! "bin/rails log:clear tmp:clear" + + puts "\n== Restarting application server ==" + system! "bin/rails restart" +end diff --git a/examples/golang-push/rideshare/rideshare_rails/config.ru b/examples/golang-push/rideshare/rideshare_rails/config.ru new file mode 100644 index 0000000000..4a3c09a688 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config.ru @@ -0,0 +1,6 @@ +# This file is used by Rack-based servers to start the application. + +require_relative "config/environment" + +run Rails.application +Rails.application.load_server diff --git a/examples/golang-push/rideshare/rideshare_rails/config/application.rb b/examples/golang-push/rideshare/rideshare_rails/config/application.rb new file mode 100644 index 0000000000..a1f605276b --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/application.rb @@ -0,0 +1,40 @@ +require_relative "boot" + +require "rails" +# Pick the frameworks you want: +require "active_model/railtie" +# require "active_job/railtie" +require "active_record/railtie" +# require "active_storage/engine" +require "action_controller/railtie" +# require "action_mailer/railtie" +# require "action_mailbox/engine" +# require "action_text/engine" +require "action_view/railtie" +# require "action_cable/engine" +require "rails/test_unit/railtie" +require "pyroscope" + +# Require the gems listed in Gemfile, including any gems +# you've limited to :test, :development, or :production. +Bundler.require(*Rails.groups) + +module RideshareRails + class Application < Rails::Application + # Initialize configuration defaults for originally generated Rails version. + config.load_defaults 7.0 + + # Configuration for the application, engines, and railties goes here. + # + # These settings can be overridden in specific environments using the files + # in config/environments, which are processed later. + # + # config.time_zone = "Central Time (US & Canada)" + # config.eager_load_paths << Rails.root.join("extras") + + # Don't generate system test files. + config.generators.system_tests = nil + config.action_controller.include_all_helpers = true + config.active_record.sqlite3_production_warning=false + end +end diff --git a/examples/golang-push/rideshare/rideshare_rails/config/boot.rb b/examples/golang-push/rideshare/rideshare_rails/config/boot.rb new file mode 100644 index 0000000000..282011619d --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/boot.rb @@ -0,0 +1,3 @@ +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) + +require "bundler/setup" # Set up gems listed in the Gemfile. diff --git a/examples/golang-push/rideshare/rideshare_rails/config/credentials.yml.enc b/examples/golang-push/rideshare/rideshare_rails/config/credentials.yml.enc new file mode 100644 index 0000000000..5119df7724 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/credentials.yml.enc @@ -0,0 +1 @@ +38Z3YTyTIEFJy3zWXElE9oQoTz7k9XmKo+GAHzVtTSRKb3hxI7CbiyUTnU8q0IjGDcUhtZ9zjwZvDBL3HBXCg8PkKCpZfb/DwneNBA4dkn2Y7LspZyGMOF0BnzYW6QQNNHVVOvDFKJHBzaMESc55Pn5cS3IpTFq/6HDv0Ey9SMDCrdLACAr6rdY3iyHjlHeHDXIJ02yiWUP1ZYQrmorRvVOPOHz5szHkVkZym6XCuIEwMs5AL6zd9n0NoG7ktqxiBW/KoYMRVewgDTngRjAp3iSJgKrQkIsD7iwSYStd46b7q7G97VbOOr2x5vb54K7TwaIU6APOYfeuVmgp8fI/w8SsBxHmJkgacgKp68sujFgG44AhSpVf4NekZdacE6XK0meC5WPmEV2HjJyrhYWrAfUw/YDRwhFFyapL--V//q5JnqYs+MF3Gd--PVOacF0BEj6/sCcYPrPINQ== \ No newline at end of file diff --git a/examples/golang-push/rideshare/rideshare_rails/config/database.yml b/examples/golang-push/rideshare/rideshare_rails/config/database.yml new file mode 100644 index 0000000000..fcba57f19f --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/database.yml @@ -0,0 +1,25 @@ +# SQLite. Versions 3.8.0 and up are supported. +# gem install sqlite3 +# +# Ensure the SQLite 3 gem is defined in your Gemfile +# gem "sqlite3" +# +default: &default + adapter: sqlite3 + pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> + timeout: 5000 + +development: + <<: *default + database: db/development.sqlite3 + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: db/test.sqlite3 + +production: + <<: *default + database: db/production.sqlite3 diff --git a/examples/golang-push/rideshare/rideshare_rails/config/environment.rb b/examples/golang-push/rideshare/rideshare_rails/config/environment.rb new file mode 100644 index 0000000000..cac5315775 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/environment.rb @@ -0,0 +1,5 @@ +# Load the Rails application. +require_relative "application" + +# Initialize the Rails application. +Rails.application.initialize! diff --git a/examples/golang-push/rideshare/rideshare_rails/config/environments/development.rb b/examples/golang-push/rideshare/rideshare_rails/config/environments/development.rb new file mode 100644 index 0000000000..5ab254920f --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/environments/development.rb @@ -0,0 +1,62 @@ +require "active_support/core_ext/integer/time" + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # In the development environment your application's code is reloaded any time + # it changes. This slows down response time but is perfect for development + # since you don't have to restart the web server when you make code changes. + config.cache_classes = false + + # Do not eager load code on boot. + config.eager_load = false + + # Show full error reports. + config.consider_all_requests_local = true + + # Enable server timing + config.server_timing = true + + # Enable/disable caching. By default caching is disabled. + # Run rails dev:cache to toggle caching. + if Rails.root.join("tmp/caching-dev.txt").exist? + config.action_controller.perform_caching = true + config.action_controller.enable_fragment_cache_logging = true + + config.cache_store = :memory_store + config.public_file_server.headers = { + "Cache-Control" => "public, max-age=#{2.days.to_i}" + } + else + config.action_controller.perform_caching = false + + config.cache_store = :null_store + end + + # Print deprecation notices to the Rails logger. + config.active_support.deprecation = :log + + # Raise exceptions for disallowed deprecations. + config.active_support.disallowed_deprecation = :raise + + # Tell Active Support which deprecation messages to disallow. + config.active_support.disallowed_deprecation_warnings = [] + + # Raise an error on page load if there are pending migrations. + config.active_record.migration_error = :page_load + + # Highlight code that triggered database queries in logs. + config.active_record.verbose_query_logs = true + + # Suppress logger output for asset requests. + config.assets.quiet = true + + # Raises error for missing translations. + # config.i18n.raise_on_missing_translations = true + + # Annotate rendered view with file names. + # config.action_view.annotate_rendered_view_with_filenames = true + + # Uncomment if you wish to allow Action Cable access from any origin. + # config.action_cable.disable_request_forgery_protection = true +end diff --git a/examples/golang-push/rideshare/rideshare_rails/config/environments/production.rb b/examples/golang-push/rideshare/rideshare_rails/config/environments/production.rb new file mode 100644 index 0000000000..ac46ed41c3 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/environments/production.rb @@ -0,0 +1,75 @@ +require "active_support/core_ext/integer/time" + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # Code is not reloaded between requests. + config.cache_classes = true + + # Eager load code on boot. This eager loads most of Rails and + # your application in memory, allowing both threaded web servers + # and those relying on copy on write to perform better. + # Rake tasks automatically ignore this option for performance. + config.eager_load = true + + # Full error reports are disabled and caching is turned on. + config.consider_all_requests_local = false + config.action_controller.perform_caching = true + + # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] + # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). + # config.require_master_key = true + + # Disable serving static files from the `/public` folder by default since + # Apache or NGINX already handles this. + config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present? + + # Compress CSS using a preprocessor. + # config.assets.css_compressor = :sass + + # Do not fallback to assets pipeline if a precompiled asset is missed. + config.assets.compile = false + + # Enable serving of images, stylesheets, and JavaScripts from an asset server. + # config.asset_host = "http://assets.example.com" + + # Specifies the header that your server uses for sending files. + # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache + # config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX + + # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. + # config.force_ssl = true + + # Include generic and useful information about system operation, but avoid logging too much + # information to avoid inadvertent exposure of personally identifiable information (PII). + config.log_level = :info + + # Prepend all log lines with the following tags. + config.log_tags = [ :request_id ] + + # Use a different cache store in production. + # config.cache_store = :mem_cache_store + + # Enable locale fallbacks for I18n (makes lookups for any locale fall back to + # the I18n.default_locale when a translation cannot be found). + config.i18n.fallbacks = true + + # Don't log any deprecations. + config.active_support.report_deprecations = false + + # Use default logging formatter so that PID and timestamp are not suppressed. + config.log_formatter = ::Logger::Formatter.new + + # Use a different logger for distributed setups. + # require "syslog/logger" + # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name") + + if ENV["RAILS_LOG_TO_STDOUT"].present? + logger = ActiveSupport::Logger.new(STDOUT) + logger.formatter = config.log_formatter + config.logger = ActiveSupport::TaggedLogging.new(logger) + end + + # Do not dump schema after migrations. + config.active_record.dump_schema_after_migration = false +end diff --git a/examples/golang-push/rideshare/rideshare_rails/config/environments/test.rb b/examples/golang-push/rideshare/rideshare_rails/config/environments/test.rb new file mode 100644 index 0000000000..eb2f1716c4 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/environments/test.rb @@ -0,0 +1,50 @@ +require "active_support/core_ext/integer/time" + +# The test environment is used exclusively to run your application's +# test suite. You never need to work with it otherwise. Remember that +# your test database is "scratch space" for the test suite and is wiped +# and recreated between test runs. Don't rely on the data there! + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # Turn false under Spring and add config.action_view.cache_template_loading = true. + config.cache_classes = true + + # Eager loading loads your whole application. When running a single test locally, + # this probably isn't necessary. It's a good idea to do in a continuous integration + # system, or in some way before deploying your code. + config.eager_load = ENV["CI"].present? + + # Configure public file server for tests with Cache-Control for performance. + config.public_file_server.enabled = true + config.public_file_server.headers = { + "Cache-Control" => "public, max-age=#{1.hour.to_i}" + } + + # Show full error reports and disable caching. + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + config.cache_store = :null_store + + # Raise exceptions instead of rendering exception templates. + config.action_dispatch.show_exceptions = false + + # Disable request forgery protection in test environment. + config.action_controller.allow_forgery_protection = false + + # Print deprecation notices to the stderr. + config.active_support.deprecation = :stderr + + # Raise exceptions for disallowed deprecations. + config.active_support.disallowed_deprecation = :raise + + # Tell Active Support which deprecation messages to disallow. + config.active_support.disallowed_deprecation_warnings = [] + + # Raises error for missing translations. + # config.i18n.raise_on_missing_translations = true + + # Annotate rendered view with file names. + # config.action_view.annotate_rendered_view_with_filenames = true +end diff --git a/examples/golang-push/rideshare/rideshare_rails/config/initializers/assets.rb b/examples/golang-push/rideshare/rideshare_rails/config/initializers/assets.rb new file mode 100644 index 0000000000..2eeef966fe --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/initializers/assets.rb @@ -0,0 +1,12 @@ +# Be sure to restart your server when you modify this file. + +# Version of your assets, change this if you want to expire all your assets. +Rails.application.config.assets.version = "1.0" + +# Add additional assets to the asset load path. +# Rails.application.config.assets.paths << Emoji.images_path + +# Precompile additional assets. +# application.js, application.css, and all non-JS/CSS in the app/assets +# folder are already added. +# Rails.application.config.assets.precompile += %w( admin.js admin.css ) diff --git a/examples/golang-push/rideshare/rideshare_rails/config/initializers/content_security_policy.rb b/examples/golang-push/rideshare/rideshare_rails/config/initializers/content_security_policy.rb new file mode 100644 index 0000000000..3621f97f8e --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/initializers/content_security_policy.rb @@ -0,0 +1,26 @@ +# Be sure to restart your server when you modify this file. + +# Define an application-wide content security policy +# For further information see the following documentation +# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy + +# Rails.application.configure do +# config.content_security_policy do |policy| +# policy.default_src :self, :https +# policy.font_src :self, :https, :data +# policy.img_src :self, :https, :data +# policy.object_src :none +# policy.script_src :self, :https +# policy.style_src :self, :https +# # Specify URI for violation reports +# # policy.report_uri "/csp-violation-report-endpoint" +# end +# +# # Generate session nonces for permitted importmap and inline scripts +# config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s } +# config.content_security_policy_nonce_directives = %w(script-src) +# +# # Report CSP violations to a specified URI. See: +# # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only +# # config.content_security_policy_report_only = true +# end diff --git a/examples/golang-push/rideshare/rideshare_rails/config/initializers/filter_parameter_logging.rb b/examples/golang-push/rideshare/rideshare_rails/config/initializers/filter_parameter_logging.rb new file mode 100644 index 0000000000..adc6568ce8 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/initializers/filter_parameter_logging.rb @@ -0,0 +1,8 @@ +# Be sure to restart your server when you modify this file. + +# Configure parameters to be filtered from the log file. Use this to limit dissemination of +# sensitive information. See the ActiveSupport::ParameterFilter documentation for supported +# notations and behaviors. +Rails.application.config.filter_parameters += [ + :passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn +] diff --git a/examples/golang-push/rideshare/rideshare_rails/config/initializers/inflections.rb b/examples/golang-push/rideshare/rideshare_rails/config/initializers/inflections.rb new file mode 100644 index 0000000000..3860f659ea --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/initializers/inflections.rb @@ -0,0 +1,16 @@ +# Be sure to restart your server when you modify this file. + +# Add new inflection rules using the following format. Inflections +# are locale specific, and you may define rules for as many different +# locales as you wish. All of these examples are active by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.plural /^(ox)$/i, "\\1en" +# inflect.singular /^(ox)en/i, "\\1" +# inflect.irregular "person", "people" +# inflect.uncountable %w( fish sheep ) +# end + +# These inflection rules are supported but not enabled by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.acronym "RESTful" +# end diff --git a/examples/golang-push/rideshare/rideshare_rails/config/initializers/multiplier.rb b/examples/golang-push/rideshare/rideshare_rails/config/initializers/multiplier.rb new file mode 100644 index 0000000000..7caa698389 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/initializers/multiplier.rb @@ -0,0 +1 @@ +MULTIPLIER = 1000000/2 diff --git a/examples/golang-push/rideshare/rideshare_rails/config/initializers/permissions_policy.rb b/examples/golang-push/rideshare/rideshare_rails/config/initializers/permissions_policy.rb new file mode 100644 index 0000000000..00f64d71b0 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/initializers/permissions_policy.rb @@ -0,0 +1,11 @@ +# Define an application-wide HTTP permissions policy. For further +# information see https://developers.google.com/web/updates/2018/06/feature-policy +# +# Rails.application.config.permissions_policy do |f| +# f.camera :none +# f.gyroscope :none +# f.microphone :none +# f.usb :none +# f.fullscreen :self +# f.payment :self, "https://secure.example.com" +# end diff --git a/examples/golang-push/rideshare/rideshare_rails/config/initializers/pyroscope.rb b/examples/golang-push/rideshare/rideshare_rails/config/initializers/pyroscope.rb new file mode 100644 index 0000000000..63e389f9e5 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/initializers/pyroscope.rb @@ -0,0 +1,24 @@ +require 'pyroscope/otel' + +app_name = ENV.fetch("PYROSCOPE_APPLICATION_NAME", "rails-ride-sharing-app") +pyroscope_server_address = ENV.fetch("PYROSCOPE_SERVER_ADDRESS", "http://pyroscope:4040") +jaeger_endpoint = ENV.fetch("JAEGER_ENDPOINT", "http://localhost:14268/api/traces") + +Pyroscope.configure do |config| + config.app_name = app_name + config.server_address = pyroscope_server_address + config.auth_token = ENV.fetch("PYROSCOPE_AUTH_TOKEN", "") + + config.tags = { + "region": ENV["REGION"] || "us-east", + "compression": ENV["COMPRESSION"], + } +end + +OpenTelemetry::SDK.configure do |c| + c.service_name = app_name + c.add_span_processor Pyroscope::Otel::SpanProcessor.new("#{app_name}.cpu", pyroscope_server_address) + c.add_span_processor OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new( + OpenTelemetry::Exporter::Jaeger::CollectorExporter.new(endpoint: jaeger_endpoint)) + c.use_all() +end diff --git a/examples/golang-push/rideshare/rideshare_rails/config/locales/en.yml b/examples/golang-push/rideshare/rideshare_rails/config/locales/en.yml new file mode 100644 index 0000000000..105801565d --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/locales/en.yml @@ -0,0 +1,33 @@ +# Files in the config/locales directory are used for internationalization +# and are automatically loaded by Rails. If you want to use locales other +# than English, add the necessary files in this directory. +# +# To use the locales, use `I18n.t`: +# +# I18n.t "hello" +# +# In views, this is aliased to just `t`: +# +# <%= t("hello") %> +# +# To use a different locale, set it with `I18n.locale`: +# +# I18n.locale = :es +# +# This would use the information in config/locales/es.yml. +# +# The following keys must be escaped otherwise they will not be retrieved by +# the default I18n backend: +# +# true, false, on, off, yes, no +# +# Instead, surround them with single quotes. +# +# en: +# "true": "foo" +# +# To learn more, please read the Rails Internationalization guide +# available at https://guides.rubyonrails.org/i18n.html. + +en: + hello: 'Hello world' diff --git a/examples/golang-push/rideshare/rideshare_rails/config/puma.rb b/examples/golang-push/rideshare/rideshare_rails/config/puma.rb new file mode 100644 index 0000000000..ccf482f278 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/puma.rb @@ -0,0 +1,43 @@ +# Puma can serve each request in a thread from an internal thread pool. +# The `threads` method setting takes two numbers: a minimum and maximum. +# Any libraries that use thread pools should be configured to match +# the maximum value specified for Puma. Default is set to 5 threads for minimum +# and maximum; this matches the default thread size of Active Record. +# +max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } +min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count } +threads 1,128 + +# Specifies the `worker_timeout` threshold that Puma will use to wait before +# terminating a worker in development environments. +# +worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development" + +# Specifies the `port` that Puma will listen on to receive requests; default is 3000. +# +port ENV.fetch("PORT") { 3000 } + +# Specifies the `environment` that Puma will run in. +# +environment ENV.fetch("RAILS_ENV") { "development" } + +# Specifies the `pidfile` that Puma will use. +pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" } + +# Specifies the number of `workers` to boot in clustered mode. +# Workers are forked web server processes. If using threads and workers together +# the concurrency of the application would be max `threads` * `workers`. +# Workers do not work on JRuby or Windows (both of which do not support +# processes). +# +# workers ENV.fetch("WEB_CONCURRENCY") { 2 } + +# Use the `preload_app!` method when specifying a `workers` number. +# This directive tells Puma to first boot the application and load code +# before forking the application. This takes advantage of Copy On Write +# process behavior so workers use less memory. +# +# preload_app! + +# Allow puma to be restarted by `bin/rails restart` command. +plugin :tmp_restart diff --git a/examples/golang-push/rideshare/rideshare_rails/config/routes.rb b/examples/golang-push/rideshare/rideshare_rails/config/routes.rb new file mode 100644 index 0000000000..8d0539f0f5 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/config/routes.rb @@ -0,0 +1,9 @@ +Rails.application.routes.draw do + # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html + + # Defines the root path route ("/") + # root "articles#index" + get "/bike" => "bike#index" + get "/scooter" => "scooter#index" + get "/car" => "car#index" +end diff --git a/examples/golang-push/rideshare/rideshare_rails/db/seeds.rb b/examples/golang-push/rideshare/rideshare_rails/db/seeds.rb new file mode 100644 index 0000000000..bc25fce306 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/db/seeds.rb @@ -0,0 +1,7 @@ +# This file should contain all the record creation needed to seed the database with its default values. +# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup). +# +# Examples: +# +# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }]) +# Character.create(name: "Luke", movie: movies.first) diff --git a/examples/golang-push/rideshare/rideshare_rails/docker-compose.yml b/examples/golang-push/rideshare/rideshare_rails/docker-compose.yml new file mode 100644 index 0000000000..a1c9ed8afe --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/docker-compose.yml @@ -0,0 +1,47 @@ +version: "3" +services: + us-east: + ports: + - 3000 + environment: + - REGION=us-east + - COMPRESSION=low + build: + context: . + links: + - 'pyroscope' + + eu-north: + ports: + - 3000 + environment: + - REGION=eu-north + build: + context: . + + ap-south: + ports: + - 3000 + environment: + - REGION=ap-south + - COMPRESSION=high + build: + context: . + + pyroscope: + image: pyroscope/pyroscope + environment: + - PYROSCOPE_LOG_LEVEL=error + ports: + - '4040:4040' + command: + - 'server' + + load-generator: + build: + context: . + dockerfile: Dockerfile.load-generator + links: + - us-east + - ap-south + - eu-north diff --git a/examples/golang-push/rideshare/rideshare_rails/lib/assets/.keep b/examples/golang-push/rideshare/rideshare_rails/lib/assets/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/golang-push/rideshare/rideshare_rails/lib/tasks/.keep b/examples/golang-push/rideshare/rideshare_rails/lib/tasks/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/golang-push/rideshare/rideshare_rails/load-generator.py b/examples/golang-push/rideshare/rideshare_rails/load-generator.py new file mode 100755 index 0000000000..33e4085083 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/load-generator.py @@ -0,0 +1,26 @@ +import random +import requests +import time + +HOSTS = [ + 'us-east', + 'eu-north', + 'ap-south', +] + +VEHICLES = [ + 'bike', + 'scooter', + 'car', +] + +if __name__ == "__main__": + print(f"starting load generator") + time.sleep(3) + while True: + host = HOSTS[random.randint(0, len(HOSTS) - 1)] + vehicle = VEHICLES[random.randint(0, len(VEHICLES) - 1)] + print(f"requesting {vehicle} from {host}") + resp = requests.get(f'http://{host}:3000/{vehicle}') + print(f"received {resp}") + time.sleep(random.uniform(0.2, 0.4)) diff --git a/examples/golang-push/rideshare/rideshare_rails/loadgen.go b/examples/golang-push/rideshare/rideshare_rails/loadgen.go new file mode 100644 index 0000000000..79e0c4a8a8 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/loadgen.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + "math/rand" + "net/http" + "time" +) + +var ( + HOSTS = []string{ + "us-east", + "eu-north", + "ap-south", + } + + VEHICLES = []string{ + "bike", + "scooter", + "car", + } +) + +func main() { + fmt.Println("starting load generator") + time.Sleep(3 * time.Second) + + // every second + ticker := time.NewTicker(100 * time.Millisecond) + for { + select { + case <-ticker.C: + go func() { + host := HOSTS[rand.Intn(len(HOSTS))] + vehicle := VEHICLES[rand.Intn(len(VEHICLES))] + http.Get(fmt.Sprintf("http://%s:3000/%s", host, vehicle)) + }() + } + } +} diff --git a/examples/golang-push/rideshare/rideshare_rails/log/.keep b/examples/golang-push/rideshare/rideshare_rails/log/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/golang-push/rideshare/rideshare_rails/public/404.html b/examples/golang-push/rideshare/rideshare_rails/public/404.html new file mode 100644 index 0000000000..2be3af26fc --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/public/404.html @@ -0,0 +1,67 @@ + + + + The page you were looking for doesn't exist (404) + + + + + + +
+
+

The page you were looking for doesn't exist.

+

You may have mistyped the address or the page may have moved.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/examples/golang-push/rideshare/rideshare_rails/public/422.html b/examples/golang-push/rideshare/rideshare_rails/public/422.html new file mode 100644 index 0000000000..c08eac0d1d --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/public/422.html @@ -0,0 +1,67 @@ + + + + The change you wanted was rejected (422) + + + + + + +
+
+

The change you wanted was rejected.

+

Maybe you tried to change something you didn't have access to.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/examples/golang-push/rideshare/rideshare_rails/public/500.html b/examples/golang-push/rideshare/rideshare_rails/public/500.html new file mode 100644 index 0000000000..78a030af22 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/public/500.html @@ -0,0 +1,66 @@ + + + + We're sorry, but something went wrong (500) + + + + + + +
+
+

We're sorry, but something went wrong.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/examples/golang-push/rideshare/rideshare_rails/public/apple-touch-icon-precomposed.png b/examples/golang-push/rideshare/rideshare_rails/public/apple-touch-icon-precomposed.png new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/golang-push/rideshare/rideshare_rails/public/apple-touch-icon.png b/examples/golang-push/rideshare/rideshare_rails/public/apple-touch-icon.png new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/golang-push/rideshare/rideshare_rails/public/favicon.ico b/examples/golang-push/rideshare/rideshare_rails/public/favicon.ico new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/golang-push/rideshare/rideshare_rails/public/robots.txt b/examples/golang-push/rideshare/rideshare_rails/public/robots.txt new file mode 100644 index 0000000000..c19f78ab68 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/public/robots.txt @@ -0,0 +1 @@ +# See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file diff --git a/examples/golang-push/rideshare/rideshare_rails/test/controllers/.keep b/examples/golang-push/rideshare/rideshare_rails/test/controllers/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/golang-push/rideshare/rideshare_rails/test/controllers/bike_controller_test.rb b/examples/golang-push/rideshare/rideshare_rails/test/controllers/bike_controller_test.rb new file mode 100644 index 0000000000..0e59d8d654 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/test/controllers/bike_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class BikeControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/examples/golang-push/rideshare/rideshare_rails/test/controllers/car_controller_test.rb b/examples/golang-push/rideshare/rideshare_rails/test/controllers/car_controller_test.rb new file mode 100644 index 0000000000..5c3d49cfca --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/test/controllers/car_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class CarControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/examples/golang-push/rideshare/rideshare_rails/test/controllers/scooter_controller_test.rb b/examples/golang-push/rideshare/rideshare_rails/test/controllers/scooter_controller_test.rb new file mode 100644 index 0000000000..fc239fa009 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/test/controllers/scooter_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class ScooterControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/examples/golang-push/rideshare/rideshare_rails/test/fixtures/files/.keep b/examples/golang-push/rideshare/rideshare_rails/test/fixtures/files/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/golang-push/rideshare/rideshare_rails/test/helpers/.keep b/examples/golang-push/rideshare/rideshare_rails/test/helpers/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/golang-push/rideshare/rideshare_rails/test/integration/.keep b/examples/golang-push/rideshare/rideshare_rails/test/integration/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/golang-push/rideshare/rideshare_rails/test/models/.keep b/examples/golang-push/rideshare/rideshare_rails/test/models/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/golang-push/rideshare/rideshare_rails/test/test_helper.rb b/examples/golang-push/rideshare/rideshare_rails/test/test_helper.rb new file mode 100644 index 0000000000..d713e377c9 --- /dev/null +++ b/examples/golang-push/rideshare/rideshare_rails/test/test_helper.rb @@ -0,0 +1,13 @@ +ENV["RAILS_ENV"] ||= "test" +require_relative "../config/environment" +require "rails/test_help" + +class ActiveSupport::TestCase + # Run tests in parallel with specified workers + parallelize(workers: :number_of_processors) + + # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. + fixtures :all + + # Add more helper methods to be used by all tests here... +end diff --git a/examples/golang-push/rideshare/utility/pool.go b/examples/golang-push/rideshare/utility/pool.go index 8a650eeba1..1324f2f99b 100644 --- a/examples/golang-push/rideshare/utility/pool.go +++ b/examples/golang-push/rideshare/utility/pool.go @@ -3,15 +3,13 @@ package utility import ( "os" "sync" - - "rideshare/rideshare" ) type workerPool struct { - poolLock *sync.Mutex - pool []chan struct{} - limit int - bufferSize int + poolLock *sync.Mutex + pool []chan struct{} + retainedData [][]byte // Slice to retain data to simulate memory leak. + limit int } // Run a function using a pool. @@ -30,15 +28,18 @@ func (c *workerPool) Run(fn func()) { c.poolLock.Lock() size := len(c.pool) if c.limit != 0 && size >= c.limit { - // We're at max pool limit, reset the pool. - c.resetWithoutLock() + // We're at max pool limit, release a resource. + last := c.pool[size-1] + last <- struct{}{} + close(last) + c.pool = c.pool[:size-1] } c.pool = append(c.pool, stop) c.poolLock.Unlock() // Create a goroutine to run the function. It will write to done when the // work is over, but won't clean up until it receives a signal from stop. - go c.doWork(fn, stop, done) + go cacheVehicleLocations(fn, stop, done, c) // Block until the worker signals it's done. <-done @@ -50,30 +51,31 @@ func (c *workerPool) Close() { c.poolLock.Lock() defer c.poolLock.Unlock() - c.resetWithoutLock() -} - -func (c *workerPool) resetWithoutLock() { for _, c := range c.pool { c <- struct{}{} close(c) } - c.pool = c.pool[:0] + c.pool = nil // Fix here to properly clean up the slice. + c.retainedData = nil // Ensure that we also clear the retained data. } -func (c *workerPool) doWork(fn func(), stop <-chan struct{}, done chan<- struct{}) { +func cacheVehicleLocations(fn func(), stop <-chan struct{}, done chan<- struct{}, pool *workerPool) { buf := make([]byte, 0) // Do work. fn() // Simulate the work in fn requiring some data to be added to a buffer. - for i := 0; i < c.bufferSize; i++ { - buf = append(buf, byte(i)) + // Increase buffer size to leak more memory. + const mb = 1 << 20 + for i := 0; i < 0.5*mb; i++ { + buf = append(buf, byte(i%256)) } - // Don't let the compiler optimize away the buf. - var _ = buf + // Retain the buffer in the pool to prevent it from being garbage collected. + pool.poolLock.Lock() + pool.retainedData = append(pool.retainedData, buf) + pool.poolLock.Unlock() // Signal we're done working. done <- struct{}{} @@ -82,11 +84,62 @@ func (c *workerPool) doWork(fn func(), stop <-chan struct{}, done chan<- struct{ <-stop } -func newPool(c rideshare.Config) *workerPool { +// func cacheVehicleLocations(fn func(), stop <-chan struct{}, done chan<- struct{}, pool *workerPool) { +// buf := make([]byte, 0) + +// // Do work. +// fn() + +// // Use a ticker to check every minute. +// ticker := time.NewTicker(1 * time.Minute) +// defer ticker.Stop() + +// const mb = 1 << 20 +// leakSize := 0.5 * mb // Start with non-leaky behavior. +// minutesLeaking := 0 + +// for { +// select { +// case <-ticker.C: +// // Calculate the elapsed minutes since the start time. +// elapsed := time.Since(startTime).Minutes() + +// // Toggle behavior every 16 minutes. +// if int(elapsed)%16 == 0 { // At the 0th, 16th, 32nd minute mark, etc. +// leakSize = 0.5 * mb // Reset to non-leaky behavior. +// minutesLeaking = 0 +// } else if minutesLeaking == 1 { +// leakSize = 1.5 * mb // After 1 minute, start leaking. +// } + +// minutesLeaking++ + +// case <-stop: +// // When told to stop, exit the function. +// return +// default: +// // Simulate the work in fn requiring some data to be added to a buffer. +// for i := 0; i < int(leakSize); i++ { +// buf = append(buf, byte(i%256)) +// } + +// // Retain the buffer in the pool to prevent it from being garbage collected. +// pool.poolLock.Lock() +// pool.retainedData = append(pool.retainedData, buf) +// pool.poolLock.Unlock() + +// // Signal we're done working. +// done <- struct{}{} +// return +// } +// } +// } + +func newPool(n int) *workerPool { return &workerPool{ - poolLock: &sync.Mutex{}, - pool: make([]chan struct{}, 0, c.ParametersPoolSize), - limit: c.ParametersPoolSize, - bufferSize: c.ParametersPoolBufferSize, + poolLock: &sync.Mutex{}, + pool: make([]chan struct{}, 0, n), + retainedData: make([][]byte, 0), // Initialize the slice to retain data. + limit: n, } } diff --git a/examples/golang-push/rideshare/utility/utility.go b/examples/golang-push/rideshare/utility/utility.go index dbc0b64dcb..4665f1886e 100644 --- a/examples/golang-push/rideshare/utility/utility.go +++ b/examples/golang-push/rideshare/utility/utility.go @@ -16,9 +16,10 @@ const durationConstant = time.Duration(200 * time.Millisecond) var pool *workerPool -// InitWorkPool initializes the worker pool and returns a clean up function. +// InitWorkerPool initializes the worker pool and returns a clean up function. func InitWorkerPool(c rideshare.Config) func() { - pool = newPool(c) + // Use the ParametersPoolSize from the config to initialize the pool. + pool = newPool(c.ParametersPoolSize) return pool.Close }