- Run
$ createdb uniq-db-test
to create DB - Run example with Ruby (e.g.,
$ ruby 1_find_or_create_by_single_thread.rb
)
With many successful INSERTs
Warming up --------------------------------------
# Unnecessary indexes slows down writes and consumes additional storage and memory. | |
# Just paste this snippet in your Rails console (bundle exec rails c). | |
# And it will print all redundant indexes that are already covered by another index on the table: | |
# Table `pages`: index `site_idx` (site_id) already covered by `site_slug_idx` (site_id,slug) | |
# Table `optins`: index `list_idx` (list_id) already covered by `list_active_idx` (list_id,active) | |
ActiveRecord::Base.connection.tables.map do |table| | |
indexes = ActiveRecord::Base.connection.indexes(table).select(&:valid).reject(&:where) |
frozen_string_literal: true | |
# A leaky bucket rate limiter for Ruby | |
# | |
# @see https://www.mikeperham.com/2020/11/09/the-leaky-bucket-rate-limiter/ | |
# @see https://en.wikipedia.org/wiki/Leaky_bucket | |
class RateLimit | |
class Error < StandardError | |
attr_accessor :retry_in |
module SessionHelper | |
extend ActiveSupport::Concern | |
class ::SessionsBypassController < ActionController::Base | |
def show | |
session[:user_id] = params[:user_id] | |
session[:team_id] = params[:team_id] | |
session[:tenant] = Apartment::Tenant.current |
require 'digest' | |
class ConsistentHashing | |
def initialize(nodes) | |
nodes.map { |node| add_node(node) } | |
end | |
def find_cache(key) | |
puts | |
hash = hash_value(key) |
# spec/system/support/login_helpers.rb | |
# See this blog post for setup guide: https://evilmartians.com/chronicles/system-of-a-test-setting-up-end-to-end-rails-testing | |
module LoginHelpers | |
def login_as(user) | |
# Craft session cookie to make request authenticated (to pass even routing constraints) | |
# Compilation of these: | |
# - https://dev.to/nejremeslnici/migrating-selenium-system-tests-to-cuprite-42ah#faster-signin-in-tests | |
# - https://turriate.com/articles/2011/feb/how-to-generate-signed-rails-session-cookie | |
# - https://github.com/rails/rails/blob/43e29f0f5d54294ed61c31ddecdf76c2e1a474f7/actionpack/test/dispatch/cookies_test.rb#L350 |
# frozen_string_literal: true | |
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
# Activate the gem you are reporting the issue against. |
How to run examples:
ruby '2.7.1' | |
gem 'rails', github: 'rails/rails' | |
gem 'tzinfo-data', '>= 1.2016.7' # Don't rely on OSX/Linux timezone data | |
# Action Text | |
gem 'actiontext', github: 'basecamp/actiontext', ref: 'okra' | |
gem 'okra', github: 'basecamp/okra' | |
# Drivers |
This is just some code I recently used in my development application in order to add token-based authentication for my api-only rails app. The api-client was to be consumed by a mobile application, so I needed an authentication solution that would keep the user logged in indefinetly and the only way to do this was either using refresh tokens or sliding sessions.
I also needed a way to both blacklist and whitelist tokens based on a unique identifier (jti)
Before trying it out DIY, I considered using: