Skip to content

Instantly share code, notes, and snippets.

@polonskiy
polonskiy / 10-custom.conf
Last active June 4, 2021 22:36
sysctl tuning for docker & serf
# values from https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/Welcome%20to%20High%20Performance%20Computing%20%28HPC%29%20Central/page/Linux%20System%20Tuning%20Recommendations
# install (root): curl -s https://gist.githubusercontent.com/polonskiy/00a71bab32360ffcb79f/raw/10-custom.conf > /etc/sysctl.d/10-custom.conf
# apply (root): sysctl -p /etc/sysctl.d/10-custom.conf
net.ipv4.neigh.default.gc_thresh1 = 30000
net.ipv4.neigh.default.gc_thresh2 = 32000
net.ipv4.neigh.default.gc_thresh3 = 32768
net.ipv6.neigh.default.gc_thresh1 = 30000
net.ipv6.neigh.default.gc_thresh2 = 32000
@polonskiy
polonskiy / rkxor.php
Last active August 8, 2016 11:12
Repeating-key XOR encryption
<?php
$data = file_get_contents('php://stdin');
$password = 'ps8T0G/39oHgRehuV0TjUv2lyeA=';
$c = rkxor($data, $password);
$x = rkxor($c, $password);
var_dump($c,$x);
function rkxor($data, $password) {
@polonskiy
polonskiy / PHProutines.php
Created December 4, 2014 09:32
PHProutines
<?php
class Runner {
public function __construct() {
pcntl_signal(SIGCHLD, SIG_IGN);
}
public function go() {
if (pcntl_fork()) return;
#!/usr/bin/php
<?php
$data = file_get_contents('php://stdin');
preg_match('#^To:(.*)#i', $data, $matches);
$filename = tempnam('/tmp', trim($matches[1]) . '_');
file_put_contents($filename, $data);
@polonskiy
polonskiy / fork.php
Last active August 29, 2015 14:07
echo servers in php and go
<?php
$server = stream_socket_server('tcp://127.0.0.1:8000');
while (true) {
$client = stream_socket_accept($server, -1);
if (pcntl_fork() === 0) {
stream_copy_to_stream($client, $client);
die;
}
fclose($client);
@polonskiy
polonskiy / memcached2array.php
Last active January 28, 2019 11:16
dump memcached data
#!/usr/bin/php
<?php
$argv += [1 => 'localhost', 2 => 11211];
$memcached = new Memcached;
$memcached->addServer($argv[1], $argv[2]);
$result = [];
foreach($memcached->getAllKeys() as $k) $result[$k] = $memcached->get($k);
var_export($result);
echo ";\n";
@polonskiy
polonskiy / fsstorage.php
Created January 28, 2014 13:27
FileSystem Storage Concept
<?php
class FSStorage {
protected $dir;
public function __construct($dir) {
$this->dir = $dir;
}
@polonskiy
polonskiy / shingles.php
Created January 27, 2014 16:00
Near-duplicate finder. Shingling
<?php
function similar_text_shingles($texts, $shingle_len = 10, $algo = 'md5') {
$c = count($texts);
$total = 0;
for ($i = 0; $i < $c; $i++) {
preg_match_all('#\w{4,}#u', mb_strtolower($texts[$i], 'utf-8'), $matches);
$words = $matches[0];
$cc = count($words) - $shingle_len;
for ($j = 0; $j < $cc; $j++) {
@polonskiy
polonskiy / byte2.php
Created January 9, 2014 13:30
Bytes to KB/MB/GB/TB converter
<?php
function byte2($bytes) {
$size = array('B','KB','MB','GB','TB');
$factor = floor(log($bytes, 1024));
$format = $factor > 2 ? '%.1F %s' : '%u %s';
return sprintf($format, $bytes / pow(1024, $factor), $size[$factor]);
}
@polonskiy
polonskiy / pomodoro.sh
Last active January 2, 2016 02:38
pomodoro
#!/bin/bash
sleep ${1:-25}m
notify-send -i clock 'Time is up'