Skip to content

Instantly share code, notes, and snippets.

@sandro
sandro / client_example
Created September 4, 2011 09:09
Ruby select socket server. An example of a single-threaded, event-driven (select) server.
$ irb -r ./select_server
>> client.puts "hi"
=> nil
>> client.puts "bye"
=> nil
>> client.close
=> nil
>> exit
# Makefile template for a shared library in C
# https://www.topbug.net/blog/2019/10/28/makefile-template-for-a-shared-library-in-c-with-explanations/
CC = gcc # C compiler
CFLAGS = -fPIC -Wall -Wextra -O2 -g # C flags
LDFLAGS = -shared # linking flags
RM = rm -f # rm command
TARGET_LIB = libtarget.so # target lib
SRCS = main.c src1.c src2.c # source files
@guilhermesimoes
guilhermesimoes / line_count_benchmark.rb
Last active September 11, 2023 09:36
Ruby Benchmark: Counting the number of lines of a file
# gem install benchmark-ips
require "benchmark/ips"
path = "lib/rubycritic/cli/options.rb"
Benchmark.ips do |x|
x.report("read + each_line") { File.read(path).each_line.count }
x.report("open + each_line") { File.open(path, "r").each_line.count }
@magnetikonline
magnetikonline / README.md
Last active June 27, 2023 17:26
Nginx keepalive connections config example to upstream servers.

Nginx upstream HTTP keepalive config example

As per Nginx documentation, the key directives of proxy_http_version and proxy_set_header need to be set as per below:

upstream backend {
	server 127.0.0.1:8080;
	keepalive 8;
}

server {
@jeffweiss
jeffweiss / mix.exs
Last active December 18, 2018 23:13
Retrieving application version number in mix.exs from git describe
defmodule MyApp.Mixfile do
use Mix.Project
def project do
[app: :my_app,
version: get_version,
elixir: "~> 1.0",
elixirc_paths: elixirc_paths(Mix.env),
compilers: Mix.compilers,
build_embedded: Mix.env == :prod,
@tomykaira
tomykaira / fork_safe.rb
Created July 15, 2016 08:31
Fork safe connection pool in Ruby. Not elegant.
require 'connection_pool'
def open_pool
pool = ConnectionPool.new { File.open('/dev/null', 'r') }
class << pool
def fileno
with { |fd| fd.fileno }
end
end
pool
@wbotelhos
wbotelhos / clear-sidekiq-jobs.sh
Last active July 23, 2024 14:51
Clear Sidekiq Jobs
require 'sidekiq/api'
# 1. Clear retry set
Sidekiq::RetrySet.new.clear
# 2. Clear scheduled jobs
Sidekiq::ScheduledSet.new.clear
‽Erlang/OTP 19 [erts-8.3] [64-bit] [smp:8:8] [async-threads:10]
Interactive Elixir (1.4.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c("tst_gen.ex")
[TstGen]
iex(2)>
nil
iex(3)> {:ok, pid} = TstGen.start_link
{:ok, #PID<0.88.0>}
iex(4)>