Compare commits
No commits in common. "685d8b574b8128258d4496a32f4675dac2aab0b5" and "ec0f736cad22843edc4436578dd8b2883a3fbf08" have entirely different histories.
685d8b574b
...
ec0f736cad
|
@ -20,14 +20,12 @@ dir_nfds_t: usize = 0,
|
|||
dir_wds: [MAX_FDS]Wd = [_]Wd{.{}} ** MAX_FDS,
|
||||
control_socket: ?std.os.socket_t = null,
|
||||
watch_started: bool = false,
|
||||
sock_name: [:0]const u8,
|
||||
|
||||
pub fn init(file_changed: *const fn (usize) void) Self {
|
||||
if (builtin.os.tag != .linux)
|
||||
@compileError("Unsupported OS");
|
||||
return .{
|
||||
.fileChanged = file_changed,
|
||||
.sock_name = sockName(),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -52,15 +50,12 @@ pub fn deinit(self: *Self) void {
|
|||
std.os.close(fd);
|
||||
}
|
||||
const cwd = std.fs.cwd();
|
||||
cwd.deleteFileZ(self.sock_name) catch |e|
|
||||
log.err("error removing socket file {s}: {any}", .{ self.sock_name, e });
|
||||
cwd.deleteFileZ(SOCK_NAME) catch |e|
|
||||
log.err("error removing socket file " ++ SOCK_NAME ++ ": {any}", .{e});
|
||||
}
|
||||
|
||||
const SOCK_NAME = "S.watch-control";
|
||||
var buf = [_]u8{0} ** (SOCK_NAME.len + "-9223372036854775807 ".len);
|
||||
fn sockName() [:0]const u8 {
|
||||
return std.fmt.bufPrintZ(buf[0..], "{s}-{d}", .{ SOCK_NAME, std.time.timestamp() }) catch unreachable; // buffer designed for Max(i64) with sock name and a trailing \0
|
||||
}
|
||||
|
||||
/// starts the file watch. This function will not return, so it is best
|
||||
/// to put this function in its own thread:
|
||||
///
|
||||
|
@ -71,7 +66,7 @@ fn sockName() [:0]const u8 {
|
|||
/// is intended later
|
||||
pub fn startWatch(self: *Self) void {
|
||||
if (self.control_socket == null)
|
||||
self.addControlSocket(self.sock_name) catch @panic("could not add control socket");
|
||||
self.addControlSocket(SOCK_NAME) catch @panic("could not add control socket");
|
||||
std.debug.assert(self.control_socket != null);
|
||||
|
||||
while (true) {
|
||||
|
@ -108,7 +103,7 @@ pub fn startWatch(self: *Self) void {
|
|||
|
||||
// self.control_socket_accepted_fd = self.control_socket_accepted_fd orelse acceptSocket(self.control_socket.?);
|
||||
// const fd = self.control_socket_accepted_fd.?; // let's save some typing
|
||||
const fd = acceptSocket(self.sock_name, self.control_socket.?);
|
||||
const fd = acceptSocket(self.control_socket.?);
|
||||
defer std.os.close(fd);
|
||||
|
||||
var readcount = std.os.recv(fd, &control_buf, 0) catch unreachable;
|
||||
|
@ -162,8 +157,8 @@ pub fn startWatch(self: *Self) void {
|
|||
}
|
||||
}
|
||||
|
||||
fn acceptSocket(name: [:0]const u8, socket: std.os.socket_t) std.os.socket_t {
|
||||
var sockaddr = std.net.Address.initUnix(name) catch @panic("could not get sockaddr");
|
||||
fn acceptSocket(socket: std.os.socket_t) std.os.socket_t {
|
||||
var sockaddr = std.net.Address.initUnix(SOCK_NAME) catch @panic("could not get sockaddr");
|
||||
var sockaddr_len: std.os.socklen_t = sockaddr.getOsSockLen();
|
||||
log.debug("tid={d} accepting on socket fd {d}", .{ std.Thread.getCurrentId(), socket });
|
||||
return std.os.accept(
|
||||
|
@ -327,7 +322,7 @@ fn sendControl(self: Self, control: u8) !void {
|
|||
// log.debug("request to send control 0x{x}", .{control});
|
||||
if (self.control_socket == null) return; // nothing to do
|
||||
// log.debug("tid={d} opening stream", .{std.Thread.getCurrentId()});
|
||||
var stream = try std.net.connectUnixSocket(self.sock_name);
|
||||
var stream = try std.net.connectUnixSocket(SOCK_NAME);
|
||||
defer stream.close();
|
||||
log.debug("tid={d} sending control 0x{x} on socket fd={d}", .{ std.Thread.getCurrentId(), control, stream.handle });
|
||||
try stream.writer().writeByte(control);
|
||||
|
@ -378,7 +373,7 @@ fn addControlSocket(self: *Self, path: [:0]const u8) !void {
|
|||
|
||||
const sockaddr = try std.net.Address.initUnix(path);
|
||||
|
||||
log.debug("binding to path: {s}", .{path});
|
||||
// TODO: If this bind fails with EADDRINUSE we can probably delete the existing file
|
||||
try std.os.bind(sock, &sockaddr.any, sockaddr.getOsSockLen());
|
||||
try std.os.listen(sock, 10);
|
||||
self.control_socket = sock;
|
||||
|
|
|
@ -25,7 +25,7 @@ const requestDeinitFn = *const fn () void;
|
|||
|
||||
const timeout = 250;
|
||||
|
||||
var watcher: Watch = undefined;
|
||||
var watcher = Watch.init(executorChanged);
|
||||
var watcher_thread: ?std.Thread = null;
|
||||
|
||||
// Timer used by processRequest to provide ttfb/ttlb data in output
|
||||
|
@ -405,7 +405,6 @@ fn childMain(allocator: std.mem.Allocator) !void {
|
|||
defer allocator.free(executors);
|
||||
defer parsed_config.deinit();
|
||||
|
||||
watcher = Watch.init(executorChanged);
|
||||
watcher_thread = try std.Thread.spawn(.{}, Watch.startWatch, .{&watcher});
|
||||
|
||||
var server = std.http.Server.init(allocator, .{ .reuse_address = true });
|
||||
|
|
Loading…
Reference in New Issue
Block a user