mirror of
https://git.youjo.love/youjo/youjo-be.git
synced 2024-11-20 13:59:55 +01:00
FlakeId: ignore null mac for workerid, use first mac
This commit is contained in:
parent
9ba6e1fabe
commit
be43aa2875
1 changed files with 11 additions and 11 deletions
|
@ -161,23 +161,23 @@ defmodule Pleroma.FlakeId do
|
||||||
1_000_000_000 * mega_seconds + seconds * 1000 + :erlang.trunc(micro_seconds / 1000)
|
1_000_000_000 * mega_seconds + seconds * 1000 + :erlang.trunc(micro_seconds / 1000)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp mac do
|
def mac do
|
||||||
{:ok, addresses} = :inet.getifaddrs()
|
{:ok, addresses} = :inet.getifaddrs()
|
||||||
|
|
||||||
ifaces_with_mac =
|
macids =
|
||||||
Enum.reduce(addresses, [], fn {iface, attrs}, acc ->
|
Enum.reduce(addresses, [], fn {_iface, attrs}, acc ->
|
||||||
if attrs[:hwaddr], do: [iface | acc], else: acc
|
case attrs[:hwaddr] do
|
||||||
|
[0, 0, 0 | _] -> acc
|
||||||
|
mac when is_list(mac) -> [mac_to_worker_id(mac) | acc]
|
||||||
|
_ -> acc
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
iface = Enum.at(ifaces_with_mac, :rand.uniform(length(ifaces_with_mac)) - 1)
|
List.first(macids)
|
||||||
mac(iface)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp mac(name) do
|
def mac_to_worker_id(mac) do
|
||||||
{:ok, addresses} = :inet.getifaddrs()
|
<<worker::integer-size(48)>> = :binary.list_to_bin(mac)
|
||||||
proplist = :proplists.get_value(name, addresses)
|
|
||||||
hwaddr = Enum.take(:proplists.get_value(:hwaddr, proplist), 6)
|
|
||||||
<<worker::integer-size(48)>> = :binary.list_to_bin(hwaddr)
|
|
||||||
worker
|
worker
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue