Skip to content

Instantly share code, notes, and snippets.

View StevenJL's full-sized avatar

Steven Li StevenJL

  • Fountain Inc.
  • SF Bay Area
  • 22:06 (UTC -07:00)
View GitHub Profile
class Account < ActiveRecord::Base
def withdraw(amount)
# ...
end
def deposit(amount)
# ...
end
def close!
@StevenJL
StevenJL / sti.rb
Last active August 15, 2019 17:38
class Account < ActiveRecord::Base
def withdraw(amount)
# ...
end
def deposit(amount)
# ...
end
def close!
require "benchmark"
user = User.find_by_name("Teemo")
# See the time it takes to count the comments of a post in an n+1 query
puts Benchmark.realtime { user.posts.each { |post| puts post.comments.count } }
# See the time it takes to count the comments of a post with eager-loading
# Should be faster
puts Benchmark.realtime { user.includes(:posts).posts.each { |post| puts post.comments.count } }
@StevenJL
StevenJL / dos.conf
Last active January 18, 2018 02:32
http {
# The `limit_req_zone` directive here prevents a single ip
# address from making more than 30 requests per minute.
# The `$binary_remote_addr` tells nginx to use the remote address
# as the "key" for rate limiting. The zone is used to define a
# name and size where excessive requests are stored.
limit_req_zone $binary_remote_addr zone=my_zone:12m rate=30r/m;
location /do_not_dos_plz {
server {
# Deny all ip addresses from range 192.168.1.0 - 192.168.1.255
# since the /24 means the first 24 bits of the ip address (ie. 192.168.1)
# is the network number, so deny all ips that belong to that network number.
deny 192.168.1.0/24;
allow all;
location /admin {
# If there the upstream server errors, we want to with an error page.
# Note the `location` directive here is performing a regex match.
location ~* (script1|script2|script3)\.php$ {
proxy_pass http://192.168.0.1;
# The `error_page` directive will return the document file
# 50x.html depending on the upstream error.
error_page 500 502 503 504 /50x.html;
}
location @proxy {
# if an upstream server supports SSL, simply change the protocol
# to https.
proxy_pass https://192.168.0.1;
# If the authenticity of the upstream server needs to be verified
# then enable it using the `proxy_ssl_verify` directive. This will
# check the upstream server's certificate against certificate authorities
# which are stored in /etc/ssl/certs
proxy_ssl_verify on;
@StevenJL
StevenJL / named-location.conf
Last active January 15, 2018 20:41
Named Location
http {
server {
upstream upstream_server {
server unix:/tmp/rails.sock;
}
# This defines a "named location" called @proxy, which can be
# referenced elsewhere in this configuration. It's sort of like
# a variable for locations.
@StevenJL
StevenJL / reverse proxy server
Last active January 18, 2018 02:32
Reverse proxy server
http {
server {
listen 80;
server_name myreverseproxy.com;
# The `upstream` directive defines an upstream server that can be referenced
# in the rest of this file. In this case, it's a web application (ie. Rails/Rack)
# that is listening at the given unix socket.
upstream upstream_server_name {
server unix:/tmp/rack.sock;
# Every `http` block must contain at least one `server` block which
# defines http and/or https virtual hosts.
server {
# Have a server listen to port 80, the standard HTTP port
listen 80;
# `server_name` creates a virtual host and sets its names
server_name staticcontent.com;