Просмотр исходного кода

server : Support listening on a unix socket (#12613)

* server : Bump cpp-httplib to include AF_UNIX windows support

Signed-off-by: Piotr Stankiewicz <piotr.stankiewicz@docker.com>

* server : Allow running the server example on a unix socket

Signed-off-by: Piotr Stankiewicz <piotr.stankiewicz@docker.com>

---------

Signed-off-by: Piotr Stankiewicz <piotr.stankiewicz@docker.com>
Piotr 9 месяцев назад
Родитель
Сommit
2099a9d5db
3 измененных файлов с 262 добавлено и 188 удалено
  1. 1 1
      common/arg.cpp
  2. 245 180
      examples/server/httplib.h
  3. 16 7
      examples/server/server.cpp

+ 1 - 1
common/arg.cpp

@@ -1979,7 +1979,7 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
     ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
     ).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
     add_opt(common_arg(
     add_opt(common_arg(
         {"--host"}, "HOST",
         {"--host"}, "HOST",
-        string_format("ip address to listen (default: %s)", params.hostname.c_str()),
+        string_format("ip address to listen, or bind to an UNIX socket if the address ends with .sock (default: %s)", params.hostname.c_str()),
         [](common_params & params, const std::string & value) {
         [](common_params & params, const std::string & value) {
             params.hostname = value;
             params.hostname = value;
         }
         }

Разница между файлами не показана из-за своего большого размера
+ 245 - 180
examples/server/httplib.h


+ 16 - 7
examples/server/server.cpp

@@ -4459,15 +4459,24 @@ int main(int argc, char ** argv) {
         llama_backend_free();
         llama_backend_free();
     };
     };
 
 
-    // bind HTTP listen port
     bool was_bound = false;
     bool was_bound = false;
-    if (params.port == 0) {
-        int bound_port = svr->bind_to_any_port(params.hostname);
-        if ((was_bound = (bound_port >= 0))) {
-            params.port = bound_port;
-        }
+    if (string_ends_with(std::string(params.hostname), ".sock")) {
+        LOG_INF("%s: setting address family to AF_UNIX\n", __func__);
+        svr->set_address_family(AF_UNIX);
+        // bind_to_port requires a second arg, any value other than 0 should
+        // simply get ignored
+        was_bound = svr->bind_to_port(params.hostname, 8080);
     } else {
     } else {
-        was_bound = svr->bind_to_port(params.hostname, params.port);
+        LOG_INF("%s: binding port with default address family\n", __func__);
+        // bind HTTP listen port
+        if (params.port == 0) {
+            int bound_port = svr->bind_to_any_port(params.hostname);
+            if ((was_bound = (bound_port >= 0))) {
+                params.port = bound_port;
+            }
+        } else {
+            was_bound = svr->bind_to_port(params.hostname, params.port);
+        }
     }
     }
 
 
     if (!was_bound) {
     if (!was_bound) {

Некоторые файлы не были показаны из-за большого количества измененных файлов