Skip to content

Instantly share code, notes, and snippets.

View seriyps's full-sized avatar

Sergey Prokhorov seriyps

View GitHub Profile
@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 / log_format.erl
Last active March 2, 2021 18:44
Small helpers to format common erlang structures (records and stacktraces)
%% @doc Hepers to format common erlang constructs (records, stacktraces) in a more human-readable way
%% Inspired heavily by https://github.com/erlang-lager/lager
-module(log_format).
-export([record/2, f_record/2]).
-export([records/2, f_records/2]).
-export([stack/1, f_stack/1]).
%% @doc Converts record to a string using `#record{}', not `{tuple}' syntax
%%
%% `record(#my_record{a = 1, b = qwerty}, record_info(fields, my_record))'
%% 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>
@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"...
@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 / 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 / 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
%%% @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>