Skip to content

Instantly share code, notes, and snippets.

View seriyps's full-sized avatar

Sergey Prokhorov seriyps

View GitHub Profile
@seriyps
seriyps / mtp_install.sh
Last active December 20, 2023 14:51
Interactive MTProto proxy installer
#!/bin/bash
# Automatic interactive installer for mtproto proxy https://github.com/seriyps/mtproto_proxy
# Supported OS:
# - Ubuntu 18.xx
# - Ubuntu 19.xx
# - Ubuntu 20.xx
# - Ubuntu 21.xx
# - Ubuntu 22.xx
# - Debian 11 bullseye
# - Debian 10 buster
@seriyps
seriyps / server.py
Created September 24, 2012 01:16
TornadoWEB streaming file server
# -*- coding: utf-8 -*-
'''
Created on 2012-09-24
@author: Sergey <me@seriyps.ru>
How to test:
$ head -c 100M /dev/urandom > static_file.bin
$ python
@seriyps
seriyps / epgsql_cmd_prepared_query_oom_protection.erl
Created April 18, 2023 09:04
`epgsql` command with OOM protection. It discards the incoming results if the number of bytes received exceeds the threshold
-module(epgsql_cmd_prepared_query_oom_protection).
-behaviour(epgsql_command).
-export([init/1, execute/2, handle_message/4]).
-export_type([response/0]).
-type response() :: {ok, Count :: non_neg_integer(), Cols :: [epgsql:column()], Rows :: [tuple()]}
| {ok, Count :: non_neg_integer()}
| {ok, Cols :: [epgsql:column()], Rows :: [tuple()]}
@seriyps
seriyps / tcp_server.rs
Last active November 17, 2022 10:08
Simple concurrent TCP echo server (rust 0.11)
#![feature(phase)]
#[phase(plugin, link)] extern crate log;
extern crate green;
extern crate rustuv;
use std::io;
use std::os;
use std::io::{Listener,Acceptor,TcpStream};
#[start]
@seriyps
seriyps / email.erl
Last active November 6, 2022 02:03
Send emails using Erlang gen_smtp shortcut.
% Send plaintext email using gen_smtp https://github.com/Vagabond/gen_smtp
% This function sends email directly to receiver's SMTP server and don't use MTA relays.
%
% Example plaintext email:
% Mail = mail_plain(<<"Bob <sender@example.com>">>, <<"Alice <receiver@example.com>">>, <<"The mail subject">>, <<"The mail body">>),
% send_email(Mail).
%
% Example email with image attachment:
% ImgName = "image.jpg",
% {ok, ImgBin} = file:read_file(ImgName),
@seriyps
seriyps / on_same_timezone.erl
Created August 21, 2015 12:48
Check if epgsql database connections are on the same timezone
% If we have some amount of PostgreSQL instances and want to transfer data between them,
% in case when model has 'timeatamp' fields, we should check that both instances configured with the
% same timezone
on_same_timezone(Connections) ->
Tests =
[%% Check using TimeZone connection parameter
{fun(Conn) ->
{ok, TZ} = pgsql:get_parameter(Conn, <<"TimeZone">>),
TZ
@seriyps
seriyps / uptime.erl
Last active April 29, 2022 08:38
Uptime of Erlang node
-export([uptime/0, uptime/1, uptime_string/0]).
%% @doc uptime in native time units
uptime() ->
erlang:monotonic_time() - erlang:system_info(start_time).
%% @doc uptime in specified time units
uptime(Unit) ->
erlang:convert_time_unit(uptime(), native, Unit).
@seriyps
seriyps / walker_alias.erl
Last active February 15, 2022 14:07
Walker alias method implemented in Erlang
%%% @author Sergey Prokhorov <me@seriyps.ru>
%%% @copyright (C) 2013, Sergey Prokhorov
%%% @doc
%%% @license Apache License Version 2.0
%%%
%%% Walker alias method - efficient random selection with defined probabilities.
%%% <http://en.wikipedia.org/wiki/Alias_method>
%%%
%%% > ProbabilityValueList = [{10, a}, {20, b}, {30, c}, {40, d}],
%%% > WalkerVectors = walker_alias:build(ProbabilityValueList),