Skip to content

Instantly share code, notes, and snippets.

package chash
import (
"encoding/binary"
"errors"
"hash/crc32"
"math"
"sort"
)
@kirs
kirs / chash.c
Last active June 23, 2025 15:06
// Taken from:
// https://raw.githubusercontent.com/openresty/lua-resty-balancer/refs/heads/master/chash.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include "chash.h"
@kirs
kirs / application_record.rb
Created April 21, 2025 15:28
Rails sharding
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
connects_to database: { writing: :primary }
end
# this is a dirty implementation of logger that
# compiles AR queries with trace into /last_request_log.html
# the snippet is useful when optimizing performance of the endpoint
class QueryLogSubscriber < ActiveSupport::LogSubscriber
TRACE_LEVEL = :app
LINES = 5
IGNORE_CACHED_QUERIES = false
def initialize
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE list (id VARCHAR(64) NOT NULL, value VARCHAR(64) NOT NULL, PRIMARY KEY(id));
INSERT INTO list VALUES('AF','Afghanistan');
INSERT INTO list VALUES('AL','Albania');
INSERT INTO list VALUES('DZ','Algerie');
INSERT INTO list VALUES('AS','Amerikansk Samoa');
INSERT INTO list VALUES('AD','Andorra');
INSERT INTO list VALUES('AO','Angola');
INSERT INTO list VALUES('AI','Anguilla');
@kirs
kirs / putinvor.js
Created January 25, 2014 08:30
delete all vk messages
window.delMessages = function() {
window.nodes = document.getElementsByClassName('mail_actions');
window.alex = 0;
window.c = 0;
var m = document.body.scrollHeight;var e = function() {
var num = window.nodes[window.alex]
if (num) {
num.childNodes[1].onclick();
window.alex++;
topError('Удалено ' + window.alex + ' сообщений. Made by Alexander Sergeev.');
@kirs
kirs / interuptible.rb
Last active October 21, 2022 10:35
Interruptible jobs
require 'bundler/inline'
gemfile do
source "https://rubygems.org"
gem "resque"
gem "pry"
gem "activerecord"
gem 'mysql2'
end
@kirs
kirs / translate.rb
Created December 11, 2011 19:41
Google Translation API: Ruby example
require 'google/api_client'
client = Google::APIClient.new
translate = client.discovered_api('translate', 'v2')
client.authorization.access_token = '123' # dummy
client.key = "my-key-example" # your key from Google API console
response = client.execute(
:api_method => translate.translations.list,
# Cleans up branches like:
# if Shopify.rails_next?
# # Rails 5 login
# else
# # Rails 4 login
# end
module RuboCop
module Cop
module ShopifyRails
class RailsNextUnless < Cop
$(function() {
var projectForm = $(".form-repo")
var repoOwnerField = projectForm.find("select[name=owner]")
var repoNameField = projectForm.find("select[name=name]")
if(projectForm.length == 0) {
return
}
repoNameField.selectize({