Refactor to use LuaJIT and improve performance
Integrate LuaJIT as an optional runtime for better performance, with a fallback to standard Lua 5.4. Update Dockerfile to install LuaJIT and create a wrapper script for execution. Enhance network module with socket fallback support and update README to reflect these changes and configuration options.
This commit is contained in:
@@ -1,11 +1,43 @@
|
||||
-- Network utilities module for auto-boot-ollama-host
|
||||
-- Provides port checking and Wake-on-LAN functionality
|
||||
-- Provides port checking, Wake-on-LAN functionality, and socket fallback support
|
||||
|
||||
local socket = require("socket")
|
||||
local utils = require("utils")
|
||||
|
||||
-- Try to load socket module, fallback to basic implementation for LuaJIT
|
||||
local socket
|
||||
local socket_available = true
|
||||
local ok, err = pcall(function() socket = require("socket") end)
|
||||
if not ok then
|
||||
print("Warning: socket module not available, using fallback implementation")
|
||||
print("Error:", err)
|
||||
socket_available = false
|
||||
-- Create a minimal socket fallback for LuaJIT
|
||||
socket = {
|
||||
gettime = function() return os.time() end,
|
||||
sleep = function(sec) os.execute("sleep " .. tostring(sec)) end,
|
||||
tcp = function() return nil end, -- Will cause network functions to fail gracefully
|
||||
udp = function() return nil end,
|
||||
udp4 = function() return nil end
|
||||
}
|
||||
end
|
||||
|
||||
local network = {}
|
||||
|
||||
-- Check if socket module is fully functional
|
||||
function network.is_socket_available()
|
||||
return socket_available and socket and socket.tcp and socket.udp
|
||||
end
|
||||
|
||||
-- Check if socket module is available for sleep operations
|
||||
function network.is_sleep_available()
|
||||
return socket_available and socket and socket.sleep
|
||||
end
|
||||
|
||||
-- Get the socket module (either real or fallback)
|
||||
function network.get_socket()
|
||||
return socket
|
||||
end
|
||||
|
||||
-- Check if a TCP port is accepting connections within a timeout (seconds)
|
||||
function network.port_is_up(host, port, timeout_sec)
|
||||
host = tostring(host or "127.0.0.1")
|
||||
@@ -13,6 +45,14 @@ function network.port_is_up(host, port, timeout_sec)
|
||||
local timeout = tonumber(timeout_sec or 1) or 1
|
||||
if port <= 0 then return false end
|
||||
|
||||
-- Fallback to basic check if socket is not available
|
||||
if not network.is_socket_available() then
|
||||
utils.log("Socket module not available, using basic port check")
|
||||
local cmd = string.format("nc -z -w1 %s %d 2>/dev/null", host, port)
|
||||
local result = os.execute(cmd)
|
||||
return result == 0
|
||||
end
|
||||
|
||||
local deadline = socket.gettime() + timeout
|
||||
while socket.gettime() < deadline do
|
||||
local tcp = socket.tcp()
|
||||
@@ -28,32 +68,40 @@ end
|
||||
|
||||
-- Convert MAC address string to bytes
|
||||
-- "AA:BB:CC:DD:EE:FF" -> 6 bytes
|
||||
local function mac_to_bytes(mac)
|
||||
local bytes = {}
|
||||
for byte in mac:gmatch("(%x%x)") do
|
||||
table.insert(bytes, tonumber(byte, 16))
|
||||
end
|
||||
if #bytes ~= 6 then return nil end
|
||||
return string.char(table.unpack(bytes))
|
||||
end
|
||||
-- local function mac_to_bytes(mac)
|
||||
-- local bytes = {}
|
||||
-- for byte in mac:gmatch("(%x%x)") do
|
||||
-- table.insert(bytes, tonumber(byte, 16))
|
||||
-- end
|
||||
-- if #bytes ~= 6 then return nil end
|
||||
-- return string.char(table.unpack(bytes))
|
||||
-- end
|
||||
|
||||
-- Send Wake-on-LAN magic packet
|
||||
function network.send_wol(mac_str, bcast_ip, port)
|
||||
-- Build magic packet
|
||||
local bytes = {}
|
||||
for byte in mac_str:gmatch("(%x%x)") do
|
||||
table.insert(bytes, tonumber(byte, 16))
|
||||
for byte in mac_str:gmatch("(%x%x)") do
|
||||
table.insert(bytes, tonumber(byte, 16))
|
||||
end
|
||||
if #bytes ~= 6 then return false, "invalid MAC" end
|
||||
|
||||
|
||||
local mac = string.char(table.unpack(bytes))
|
||||
local packet = string.rep(string.char(0xFF), 6) .. mac:rep(16)
|
||||
|
||||
-- Fallback to external tool if socket is not available
|
||||
if not network.is_socket_available() then
|
||||
utils.log("Socket module not available, using external wakeonlan tool")
|
||||
local cmd = string.format("wakeonlan -i %s -p %d %s", bcast_ip, port, mac_str)
|
||||
local result = os.execute(cmd)
|
||||
return result == 0, result ~= 0 and "wakeonlan command failed" or nil
|
||||
end
|
||||
|
||||
-- Create IPv4 UDP socket (udp4 if available), bind to IPv4 wildcard to lock AF_INET
|
||||
local udp = assert((socket.udp4 or socket.udp)())
|
||||
udp:settimeout(2)
|
||||
assert(udp:setsockname("0.0.0.0", 0)) -- force IPv4 family
|
||||
assert(udp:setoption("broadcast", true)) -- allow broadcast
|
||||
assert(udp:setsockname("0.0.0.0", 0)) -- force IPv4 family
|
||||
assert(udp:setoption("broadcast", true)) -- allow broadcast
|
||||
|
||||
local ok, err = udp:sendto(packet, bcast_ip, port)
|
||||
udp:close()
|
||||
|
||||
Reference in New Issue
Block a user