Skip to content

Instantly share code, notes, and snippets.

View seriyps's full-sized avatar

Sergey Prokhorov seriyps

View GitHub Profile
@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 / exported.erl
Created December 5, 2017 16:48
Erlang is function exported with loading
is_exported(M, F, A) ->
%% make a cheap test first to avoid unnecessary calls to code server
case erlang:module_loaded(M) of
false -> code:ensure_loaded(M);
true -> ok
end,
erlang:function_exported(M, F, A).
@seriyps
seriyps / rf.erl
Last active February 1, 2018 15:43
Print erlang records as records, not tuples; print function source code
%% Prints source code of a function.
%%
%% Requires debug_info
%% Will not work for modules mocked by meck
%%
%% > io:format("~s~n", [rf:print_function(dict, new, 0)]).
%% new() ->
%% Empty = mk_seg(16),
%% #dict{empty = Empty, segs = {Empty}}.
%%% @author sergey <me@seriyps.ru>
%%% @copyright (C) 2018, sergey
%%% @doc
%%% Given a liast of resources M and list of users N, N > M;
%%% Any resourse might be acquired by one or more users (by request from user).
%%% There is no bound on how many users can acquire single resource.
%%% User may release resource at any time.
%%% User must get resource that is used by least amount of other users.
%%% @end
%%% Created : 8 Oct 2018 by sergey <me@seriyps.ru>
@seriyps
seriyps / mtp_install.sh
Last active May 1, 2024 14:19
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 / proper_json.erl
Created October 2, 2019 14:47
Proper / QuickCheck bounded recursive generator for JSON
-module(proper_json).
-export([json/0, json/1]).
json() ->
?SIZED(Size, json(Size)).
json(0) ->
j_literal();
@seriyps
seriyps / epgsql_cmd_eequery.erl
Last active November 1, 2021 18:58
Single - roundtrip version of epgsql:equery
%% Single-roundtrip version of epgsql:equery/3
%%
%% It does parse-bind-execute sequence in 1 network roundtrip.
%% The cost is that user should manually provide the datatype information for
%% each bind-parameter.
%% Another potential problem is that connection will crash if epgsql does not
%% have a codec for any of result columns. Explicit type casting may save you
%% in this case: `SELECT my_enum::text FROM my_tab'. Or you can implement the
%% codec you need.
%%
@seriyps
seriyps / parameterized_record.erl
Created July 28, 2020 13:41
Parameterized record types dialyzer oddity
$ rebar3 dialyzer [15:33:30]
===> Verifying dependencies...
===> Compiling mylib
===> Dialyzer starting, this may take a while...
===> Updating plt...
===> Resolving files...
===> Checking 202 files in "sandbox/mylib/_build/default/rebar3_23.0_plt"...
===> Doing success typing analysis...
===> Resolving files...
===> Analyzing 2 files with "sandbox/mylib/_build/default/rebar3_23.0_plt"...
%% Version of epgsql:equery/3 that uses named statements and caches them in process dictionary
%%
%% Algorithm pseudocode is:
%% <pre>
%% stmt = cache_get(name)
%% if not stmt:
%% stmt = parse_and_describe(name, sql) # network roundtrip
%% cache_put(name, stmt)
%% return bind_and_execute(stmt, params) # network roundtrip
%% </pre>