Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix socket_options instead using keywork merge, concat default options with assigned options #183

Merged
merged 4 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/myxql/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule MyXQL.Client do

defstruct [:sock, :connection_id]

@sock_opts [mode: :binary, packet: :raw, active: false]

defmodule Config do
@moduledoc false

Expand All @@ -29,6 +31,8 @@ defmodule MyXQL.Client do
:enable_cleartext_plugin
]

@sock_opts [mode: :binary, packet: :raw, active: false]

def new(opts) do
{address, port} = address_and_port(opts)

Expand All @@ -43,8 +47,7 @@ defmodule MyXQL.Client do
ssl_opts: Keyword.get(opts, :ssl_opts, []),
connect_timeout: Keyword.get(opts, :connect_timeout, @default_timeout),
handshake_timeout: Keyword.get(opts, :handshake_timeout, @default_timeout),
socket_options:
Keyword.merge([mode: :binary, packet: :raw, active: false], opts[:socket_options] || []),
socket_options: (opts[:socket_options] || []) ++ @sock_opts,
charset: Keyword.get(opts, :charset),
collation: Keyword.get(opts, :collation),
enable_cleartext_plugin: Keyword.get(opts, :enable_cleartext_plugin, false)
Expand Down Expand Up @@ -295,7 +298,7 @@ defmodule MyXQL.Client do
buffer? = Keyword.has_key?(socket_options, :buffer)
client = %__MODULE__{connection_id: nil, sock: nil}

case :gen_tcp.connect(address, port, socket_options, connect_timeout) do
case :gen_tcp.connect(address, port, socket_options ++ @sock_opts, connect_timeout) do
{:ok, sock} when buffer? ->
{:ok, %{client | sock: {:gen_tcp, sock}}}

Expand Down
8 changes: 8 additions & 0 deletions test/myxql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ defmodule MyXQLTest do
MyXQL.prepare_execute!(conn, "", "SELECT 1, 2, NOW()")
end

test "custom socket options with :inet6" do
opts = [socket_options: [:inet]] ++ @opts
{:ok, conn} = MyXQL.start_link(opts)

MyXQL.query!(conn, "SELECT 1, 2, NOW()")
MyXQL.prepare_execute!(conn, "", "SELECT 1, 2, NOW()")
end

test "after_connect callback" do
pid = self()
opts = [after_connect: fn conn -> send(pid, {:connected, conn}) end] ++ @opts
Expand Down
Loading