Skip to content

Instantly share code, notes, and snippets.

View zhongwencool's full-sized avatar
🌊

zhongwencool zhongwencool

🌊
  • China Guǎng Zhōu
  • 12:01 (UTC +08:00)
View GitHub Profile
-module(mnesia_transform_benchmark).
%% API
-export([start/0]).
-export([benchmark/0]).
%%HOW TO TEST TRANSFORM
%%
%% 1. Compile
%% erlc mnesia_transform_benchmark.erl
%%
-module(gen_server_call_timeout).
%% API
-behaviour(gen_server).
%% API
-export([start/0]).
-export([run/2]).
-export([multi_run/0]).
-export([i/0]).
# response = '*************************'
response = Maxwell.Conn.new("http://httpbin.org/drip")
|> Maxwell.Conn.put_query_string(%{numbytes: 25, duration: 1, delay: 1, code: 200})
|> Maxwell.get!
|> Maxwell.Conn.get_resp_body
defmodule GitHubClient do
#generate get/1, get!/1 function
use Maxwell.Builder, ~w(get)a
middleware Maxwell.Middleware.BaseUrl, "https://api.github.com"
middleware Maxwell.Middleware.Opts, [connect_timeout: 3000]
middleware Maxwell.Middleware.Json
adapter Maxwell.Adapter.Hackney # default adapter is Maxwell.Adapter.Ibrowse
iex(1)> GitHubClient.user_repos(“zhongwencool”)
[%{“statuses_url” => “https://api.github.com/repos/zhongwencool/maxwell/statuses/{sha}"…}…].
Difference Maxwell Tesla
Adapter support send_file send_stream send_multipart httpc ibrowse adapter don't support send file stream multipart
Method build get/* and get!/* only build get/*
Middleware be able to check argument and set default options in compile time don't support check argument in compile time
Connection store req_headers, req_body, resp_body, resp_headers replaces env’s headers and body by response, lead to lose req_headers and req_body context

PS: About Dynamic API clients.

Maxwell: only manipulating connection details by the Maxwell.Conn help functions.

defmodule SendFileClient do
use Maxwell.Builder, ~w(post)a
middleware Maxwell.Middleware.BaseUrl, "http://httpbin.org"
middleware Maxwell.Middleware.Opts, [connect_timeout: 10000, recv_timeout: 20000]
adapter Maxwell.Adapter.Hackney
@doc """
Post whole file once
defmodule SendMultipartClient do
use Maxwell.Builder, ~w(post)a
middleware Maxwell.Middleware.BaseUrl, "http://httpbin.org"
middleware Maxwell.Middleware.Opts, [connect_timeout: 10000, recv_timeout: 20000]
middleware Maxwell.Middleware.DecodeJson
adapter Maxwell.Adapter.Hackney
@doc """
defmodule Client do
use Maxwell.Builder, ~w(post)a
middleware Maxwell.Middleware.BaseUrl, "http://httpbin.org"
middleware Maxwell.Middleware.Opts, [connect_timeout: 10000, recv_timeout: 20000]
middleware Maxwell.Middleware.Json
adapter Maxwell.Adapter.Hackney
@doc """
init([]) –>
erlang:send_after(next_report_time(),self(),report_event),
{ok, #state{}}.
handle_info(report_event, State) –>
NewState = do_report_event(State),%% maybe spawn for heavy task
erlang:send_after(next_time_report(),self(),report_event),
{noreply, NewState}.