Skip to content

Commit ddce0fe

Browse files
committed
server : Allow running the server example on a unix socket
Signed-off-by: Piotr Stankiewicz <[email protected]>
1 parent 8a17ecc commit ddce0fe

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

common/arg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1979,7 +1979,7 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
19791979
).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
19801980
add_opt(common_arg(
19811981
{"--host"}, "HOST",
1982-
string_format("ip address to listen (default: %s)", params.hostname.c_str()),
1982+
string_format("ip address to listen, or bind to an UNIX socket if the address ends with .sock (default: %s)", params.hostname.c_str()),
19831983
[](common_params & params, const std::string & value) {
19841984
params.hostname = value;
19851985
}

examples/server/server.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4459,15 +4459,24 @@ int main(int argc, char ** argv) {
44594459
llama_backend_free();
44604460
};
44614461

4462-
// bind HTTP listen port
44634462
bool was_bound = false;
4464-
if (params.port == 0) {
4465-
int bound_port = svr->bind_to_any_port(params.hostname);
4466-
if ((was_bound = (bound_port >= 0))) {
4467-
params.port = bound_port;
4468-
}
4463+
if (string_ends_with(std::string(params.hostname), ".sock")) {
4464+
LOG_INF("%s: setting address family to AF_UNIX\n", __func__);
4465+
svr->set_address_family(AF_UNIX);
4466+
// bind_to_port requires a second arg, any value other than 0 should
4467+
// simply get ignored
4468+
was_bound = svr->bind_to_port(params.hostname, 8080);
44694469
} else {
4470-
was_bound = svr->bind_to_port(params.hostname, params.port);
4470+
LOG_INF("%s: binding port with default address family\n", __func__);
4471+
// bind HTTP listen port
4472+
if (params.port == 0) {
4473+
int bound_port = svr->bind_to_any_port(params.hostname);
4474+
if ((was_bound = (bound_port >= 0))) {
4475+
params.port = bound_port;
4476+
}
4477+
} else {
4478+
was_bound = svr->bind_to_port(params.hostname, params.port);
4479+
}
44714480
}
44724481

44734482
if (!was_bound) {

0 commit comments

Comments
 (0)