Skip to content

Commit

Permalink
improve random string performance
Browse files Browse the repository at this point in the history
  • Loading branch information
lstrzebinczyk authored and teamon committed Jan 3, 2020
1 parent 48b0b06 commit e08c94f
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/tesla/multipart.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule Tesla.Multipart do
"""
@spec new() :: t
def new do
%__MODULE__{boundary: unique_string(32)}
%__MODULE__{boundary: unique_string()}
end

@doc """
Expand Down Expand Up @@ -177,12 +177,11 @@ defmodule Tesla.Multipart do
["content-disposition: form-data; #{ds}\r\n"]
end

@spec unique_string(pos_integer) :: String.t()
defp unique_string(length) do
Enum.reduce(1..length, [], fn _i, acc ->
[Enum.random(@boundary_chars) | acc]
end)
|> Enum.join("")
@spec unique_string() :: String.t()
defp unique_string() do
16
|> :crypto.strong_rand_bytes()
|> Base.encode16(case: :lower)
end

@spec assert_part_value!(any) :: :ok | no_return
Expand Down

0 comments on commit e08c94f

Please sign in to comment.