I’d like to run http_proxy without install package management for Elixir as portable aspect. So, I prepare simple shell script to help the problem. It is that downloading precompiled package and unzip it, run http_proxy with the precompiled mix. Example https://github.com/KazuCocoa/run_http_proxyMore
Tag Archives: elixir
[Elixir]read GenStage and try a bit
GenStageの0.3.0がリリースされたと合わせて、GenStageに関してのアナウンスがされたので触ってみました。合わせて、GenEventの問題点や解決したい問題がかかれているので参考になると思います。 http://elixir-lang.org/blog/2016/07/14/announcing-genstage/ GenStageとは https://github.com/elixir-lang/gen_stage#genstage に書いている通り、producerとconsumerの別れた役割を持つプロセスの間のイベントをやりとりするためのライブラリです。 GenEventだとイベントを1つの1つのプロセスで処理していたので、何かエラーが発生した時にsupervisorを再起動するというような戦略が取れないことだったりなど、データの流れを考える上で平行処理やその周辺に開発者が気を配らないといけない、と手間がかかっていました。そのため、開発者がデータの処理に集中できるように、そこら辺をいい感じにしてくれるものを用意するために作られたものがこれのようです。 3つの役割 GenStageは、0.3.0現在は3つの役割を持つことができます。 producer consumer producer_consumer 1は、callbackの実装時に handle_demand/2 を実装する必要があります。これは、2や3から要求が来た時に行う処理を書きます。2と3は handle_events/3 を実装する必要があります。これは、イベントを得た時に行う処理を書きます。 関係性の定義 GenStage.sync_subscribe c, to: b のように書くことで、このcとbの間にはconsumer(c)とproducer(b)の関係ができます。つまり、cからbに要求が送られ、それに対して行われた要求が処理されcに対して応答として帰ってきます。ここでは sync_subscribe/3 なので、同期的な挙動を行います。一定時間応答がなかったらtimeoutに成るし、そうなるまでcはbから返答を待ちます。 他にも async_subscribe/2 も存在して、これはこの関係が非同期的に行われる、ということですね。 https://hexdocs.pm/gen_stage/Experimental.GenStage.html#async_subscribe/2 https://hexdocs.pm/gen_stage/Experimental.GenStage.html#sync_subscribe/3 これらの関係性がわかるサンプル実装が以下にあります。 https://github.com/elixir-lang/gen_stage/blob/master/examples/producer_consumer.exs 最後に GenStageはまだ開発中で、 Experimental.GenStage.Flow の実装を控えているそうです。今はまだプロトタイプとしてのドキュメントが以下にあるだけですが、外部データの流れをうまく扱う Flow を練っているようですね。 https://hexdocs.pm/gen_stage/Experimental.GenStage.Flow.html ここら辺は以下のような使い方に寄与するみたいです。 Developers who maintain libraries that integrate with external data sources, be it a RabbitMQ, Redis or…More
[Swift][Elixir]implement chat server and client
Implement Chat Server with Phoenix and chat client with iOS application. Server https://github.com/KazuCocoa/my_chat_ex/tree/master After running the server, you can open chat page, http://localhost:4000/, via browser. The server has no DB. So, history will be deleted if someone reload browser. Client https://github.com/KazuCocoa/WebSocketDemoForiOS/tree/master connect to http://localhost:4000/socket/websocket as websocket Only join the room and send pre-set message to…More
[Elixir]Channelを使った実装とテストコード
Phoenixは、WebSocketを提供するためにChannelという機構を持っています。少し探せば色々参考になるコードもあるのですが、テストコードも書いてみたのでここに備忘録としてのせておきます。 Pathの設定 Phoenix v1.2 の時点では、以下の通り endpoint.ex 内でWebSocketのコネクションを成功させるパスとその処理先を決めます。 https://github.com/KazuCocoa/my_chat_ex/blob/master/lib/my_chat_ex/endpoint.ex#L4 例えば、 とした時、 /socket にWebSocketで接続を確立するとその処理先は MyChatEx.UserSocket、 /socket/other に対しては MyChatEx.OtherSocket が処理先になります。 WebSocketの確立では、transportの指定が必要です。このtransportの指定により、Phoenix Frameworkでは処理対象がWebSocketか、Long pollingかを判定します。 これは、 user_socket.ex におけるChannel Routesの指定箇所で合わせて処理されます。具体的には、以下のようにwebsocketを指定します。 これにより、最終的には以下のパスが構築され、このパスへのWebSocketの通信がそのまま MyChatEx.UserSocket などの処理へ通されます。 このパスは、たとえばChromeのinspectionにより確認可能です。 Channel Channelにおける内部処理は use Phoenix.Channel によるコールバックの実装で最低限は実現可能になります。同期的に通信する応答や、非同期で行う通信など。 https://github.com/KazuCocoa/my_chat_ex/blob/dc536e573c9b01d313cc4aaad89cf76208a8da13/web/channels/my_room_channel.ex Channelに対する認証 このChannelには、認証をかけることが容易に出来ます。これをうまく使うと、WebSocketの確立なども認証されていないものは弾く、という処理が容易に可能です。 https://github.com/KazuCocoa/my_chat_ex/blob/dc536e573c9b01d313cc4aaad89cf76208a8da13/web/channels/user_socket.ex#L24 https://github.com/KazuCocoa/myChatEx/blob/dc536e573c9b01d313cc4aaad89cf76208a8da13/web/router.ex#L28 Test code Channelに対するテストコードは以下 https://github.com/KazuCocoa/myChatEx/blob/dc536e573c9b01d313cc4aaad89cf76208a8da13/test/channels/my_room_channel_test.exs 最後に Elixir x Phoenixにて、このようなWebSocket実装などをBEAM VM上に構築できるというのは良いですね。それに特化した形でフレームワークのサポートもしていますし。More
[Elixir]diff of struct against elixir 1.2.6 and 1.3.1
pattern match with struct Structでもpattern matchingすることができる [Kernel] Allow variable struct names when matching, for example, %module{key: “value”} = struct __struct__ __struct__ が補完候補に出てこない。けれど、使うことは可能。 Elixir 1.2.6 Elixir 1.3.1 ただし、Elixir1.3.1でも、 と使うことは可能。More
[Elixir]Calendar Type in Elixir 1.3
in Elixir 1.3, calendar type was introduced. Usually, we use except for NaiveDateTime in application layer. NaiveDateTime is for developers who develop 3rd party libraries. Date http://elixir-lang.org/docs/stable/elixir/Date.html Time http://elixir-lang.org/docs/stable/elixir/Time.html DateTime http://elixir-lang.org/docs/stable/elixir/DateTime.html NaviDateTime http://elixir-lang.org/docs/stable/elixir/NaiveDateTime.html Developers should avoid creating the NaiveDateTime struct directly and instead rely on the functions provided by this module as well as the…More
[Elixir]ExUnit.DuplicateTestError
in 1.3.0, we can get ExUnit.DuplicateTestError ! This error is helpful 🙂More
[Elixir](UndefinedFunctionError) function List.Chars.to_charlist/1 is undefined or private with Elixir 1.3.0
https://github.com/elixir-lang/elixir/releases/tag/v1.3.0-rc.0 Elixir 1.3.0では to_char_list がsoft deprecatedになり、 to_charlist にリネームされました。 そのため、例えばelixir 1.2.x以前でビルドしていたバイナリが存在している状態で 1.3.0 で 再ビルドなし でコマンドを実行した場合、エラーが発生します。 例えば、 Doctest の場合、以下のようなエラーが表示されました。 これを解決するには、 _build を削除して再ビルドすればOK。この逆もありました。注意が必要ですね。 あと、もう1つ。ExUnitに標準で describe/2 が入ったことは良いですね。1段のコンテキストの分離はだいぶグルーピングに使えそう。 shouldi を使わなくても良くなりそうです。 注意が必要。More
[Elixir][Erlang]connect to remote node
Connecting to remote production node is very useful to debug or observe production. target node local node Sometimes, you should set port forwarding up to connect to target node via a particular node. References http://blog.plataformatec.com.br/2016/05/tracing-and-observing-your-remote-node/ https://mfeckie.github.io/Remote-Profiling-Elixir-Over-SSH/More
[Elixir]add search box
ある人に検索窓まだー?と言われたので、メモがてら軽くサンプル実装してみた。 こんなもんで良いかな… controller def search_email(conn, %{“search” => %{“query” => query}}) do result = User.search_user_with_email_ilike query <> “%” users = Repo.all(User) render(conn, “search_index.html”, users: users, assigns: result) end model def search_user_with_email_ilike(email) do from(user in User, where: ilike(user.email, ^email)) |> Repo.all end web/router.ex post “/search”, UserController, :search_email search_index.html %script function sample(users) { window.alert(users) } – form_for @conn,…More