Skip to content

Instantly share code, notes, and snippets.

View vinhnglx's full-sized avatar
Code is fun

Vincent Nguyen vinhnglx

Code is fun
View GitHub Profile
vinhnglx / migration.ex
Created April 23, 2018 10:06
Migration script when release a Phoenix/OTP app by Distillery
# apps/myapp/lib/migration.ex
defmodule MyApp.ReleaseTasks do
@start_apps [
@app :myapp
def repos, do: Application.get_env(@app, :ecto_repos, [])
vinhnglx /
Last active August 26, 2016 07:45
Type of servers in Rails application


Webrick is a single-thread, single-process web server.

Webrick will keep the router's connection open until it has downloaded the entirety of the request from the router. The router then will move on to the next request. Webrick server will then take the request, run the application code and send back the response to the router.

During the all of time, your host is busy, and will not accept connections from other routers. If the router attemps to connect to this host while the request is being processed, the router will wait until the host is ready.

The problem with Webrick are exaggerated with slow requests and uploads. If someone is trying to upload a big file, Webrick is going to sit there and wait while the request downloads, and will not download anything in the meantime, and it also will not accept any requests.

vinhnglx /
Last active January 26, 2024 19:17
Little's law

How many instances do you need?

There's a theoretical tool we can use, the number of instances must obey Little's Law. The definition from Wikipedia is so hard to understand. So, basically, we just need to know the formula

Number of application instances = Average number of requests per second (requests/second) * Average response time (second)

Application Instances, its job is process a single request dependently and send it back to client. When using Puma in threaded mode, application instance is the entire Puma process, when using MRI, JRuby,each thread counts as an application instance. When using Unicorn, Puma (clustered) or Passenger, your application instance is each worker process.

vinhnglx /
Last active September 19, 2023 06:46
Difference between Benchmarking and Profiling


Benchmarking. We take two competing pieces of code - could be as simpler as a one liner or as complex as an entire web framework. Then, we put them up against each other (iterations per second). At the end of the task, we come up with a single metric, a score. We use the score to compare the two competing options.

In Ruby, the Benchmark module provides methods to measure and report the time used to execute Ruby code.

Profiling. Profiling your program is a way to determining which methods are called and how long each method take to complete.

vinhnglx / User.rb
Created March 11, 2016 17:10
Custom validation message in Rails - Override prefix
class User < ActiveRecord::Base
validates_uniqueness_of :email, message: 'has already taken'
# user = '')
#! => User has already taken.
# If you wanna change the error message to "Your fucking account has already taken"
# just edit one file config/locales/en.yml

============== #rails #tips ​​_[Mẹo hay]_​ Viết filter phức tạp cho rails controller thì nên cho vào Filter class​ Vd nếu bạn cần một filter phức tạp cho before_action, rails có một dạng là nhận vào một class

class ApplicationController < ActionController::Base
  before_action LoginFilter
class LoginFilter
vinhnglx / random.rb
Created November 5, 2015 04:45
Random record in Rails
# Inspired from Sebastien Saunier
# models/production.rb
class Production < ActiveRecord::Base
enum status: {active: 1, deactive: 0}
validates_presence_of :status
include Randomable
vinhnglx / omniauth_macros.rb
Created October 6, 2015 01:18 — forked from kinopyo/omniauth_macros.rb
Integration test with Omniauth. This example is using twitter, and assume you've installed rspec and capybara. Official document is here:
# in spec/support/omniauth_macros.rb
module OmniauthMacros
def mock_auth_hash
# The mock_auth configuration allows you to set per-provider (or default)
# authentication hashes to return during integration testing.
OmniAuth.config.mock_auth[:twitter] = {
'provider' => 'twitter',
'uid' => '123545',
'user_info' => {
'name' => 'mockuser',
require 'rails_helper'
RSpec.describe TodosController, :type => :controller do
context "GET index" do
#context "POST create" do
#context "GET show" do
#context "PATCH update" do (or PUT update)
#context "DELETE destroy" do
#context "GET new" do
vinhnglx / example_nokogiri_scraping.rb
Created September 20, 2015 04:17
require 'open-uri'
require 'nokogiri'
require 'csv'
# Store URL to be scraped
url = ""
# Parse the page with Nokogiri
page = Nokogiri::HTML(open(url))
# Display output onto the screen