Skip to content

Commit 07a026e

Browse files
Update server.cpp
defer() --> defer_task() Signed-off-by: Brad Hutchings <[email protected]>
1 parent a764505 commit 07a026e

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

examples/server/server.cpp

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
#include <unordered_map>
3232
#include <unordered_set>
3333

34+
// llama-server-one START
35+
#ifdef COSMOCC
36+
#include <cosmo.h>
37+
#endif
38+
// llama-server-one END
39+
3440
using json = nlohmann::ordered_json;
3541

3642
constexpr int HTTP_POLLING_SECONDS = 1;
@@ -1594,13 +1600,15 @@ struct server_queue {
15941600
return 0;
15951601
}
15961602

1603+
// llama-server-one START - defer() --> defer_task() to make Cosmo STL happy.
15971604
// Add a new task, but defer until one slot is available
1598-
void defer(server_task task) {
1605+
void defer_task(server_task task) {
15991606
std::unique_lock<std::mutex> lock(mutex_tasks);
16001607
QUE_DBG("defer task, id = %d\n", task.id);
16011608
queue_tasks_deferred.push_back(std::move(task));
16021609
condition_tasks.notify_one();
16031610
}
1611+
// llama-server-one END
16041612

16051613
// Get the next id for creating a new task
16061614
int get_new_id() {
@@ -2637,13 +2645,17 @@ struct server_context {
26372645
if (slot == nullptr) {
26382646
// if no slot is available, we defer this task for processing later
26392647
SRV_DBG("no slot is available, defer task, id_task = %d\n", task.id);
2640-
queue_tasks.defer(task);
2648+
// llama-server-one START
2649+
queue_tasks.defer_task(task);
2650+
// llama-server-one END
26412651
break;
26422652
}
26432653
if (slot->is_processing()) {
26442654
// if requested slot is unavailable, we defer this task for processing later
26452655
SRV_DBG("requested slot is unavailable, defer task, id_task = %d\n", task.id);
2646-
queue_tasks.defer(task);
2656+
// llama-server-one START
2657+
queue_tasks.defer_task(task);
2658+
// llama-server-one END
26472659
break;
26482660
}
26492661

@@ -2726,7 +2738,9 @@ struct server_context {
27262738
if (slot->is_processing()) {
27272739
// if requested slot is unavailable, we defer this task for processing later
27282740
SRV_DBG("requested slot is unavailable, defer task, id_task = %d\n", task.id);
2729-
queue_tasks.defer(task);
2741+
// llama-server-one START
2742+
queue_tasks.defer_task(task);
2743+
// llama-server-one END
27302744
break;
27312745
}
27322746

@@ -2762,7 +2776,9 @@ struct server_context {
27622776
if (slot->is_processing()) {
27632777
// if requested slot is unavailable, we defer this task for processing later
27642778
SRV_DBG("requested slot is unavailable, defer task, id_task = %d\n", task.id);
2765-
queue_tasks.defer(task);
2779+
// llama-server-one START
2780+
queue_tasks.defer_task(task);
2781+
// llama-server-one END
27662782
break;
27672783
}
27682784

@@ -2805,7 +2821,9 @@ struct server_context {
28052821
if (slot->is_processing()) {
28062822
// if requested slot is unavailable, we defer this task for processing later
28072823
SRV_DBG("requested slot is unavailable, defer task, id_task = %d\n", task.id);
2808-
queue_tasks.defer(task);
2824+
// llama-server-one START
2825+
queue_tasks.defer_task(task);
2826+
// llama-server-one END
28092827
break;
28102828
}
28112829

@@ -3427,6 +3445,37 @@ inline void signal_handler(int signal) {
34273445
}
34283446

34293447
int main(int argc, char ** argv) {
3448+
// llama-server-one START
3449+
// This implements an args file feature inspired by llamafile's.
3450+
#ifdef COSMOCC
3451+
// Args files if present. The names are different to remove confusion during packaging.
3452+
const std::string& argsFilename = "llama-server-one-args";
3453+
const std::string& zipArgsFilename = "/zip/default-args";
3454+
struct stat buffer;
3455+
3456+
// At this point, argc, argv represent:
3457+
// command (User supplied args)
3458+
3459+
if (stat (argsFilename.c_str(), &buffer) == 0) {
3460+
argc = cosmo_args(argsFilename.c_str(), &argv);
3461+
}
3462+
3463+
// At this point, argc, argv represent:
3464+
// command (argsFilename args) (User supplied args)
3465+
3466+
if (stat (zipArgsFilename.c_str(), &buffer) == 0) {
3467+
argc = cosmo_args(zipArgsFilename.c_str(), &argv);
3468+
}
3469+
3470+
// At this point, argc, argv represent:
3471+
// command (zipArgsFilename args) (argsFilename args) (User supplied args)
3472+
3473+
// Yep, this is counterintuitive, but how the cosmo_args command works.
3474+
// argsFilename args override zipArgsFilename file args.
3475+
// User supplied args override argsFilename and zipArgsFilename args.
3476+
#endif
3477+
// llama-server-one END
3478+
34303479
// own arguments required by this example
34313480
common_params params;
34323481

@@ -4452,6 +4501,26 @@ int main(int argc, char ** argv) {
44524501
}
44534502
}
44544503

4504+
// llama-server-one START
4505+
svr->Get("/chat", [](const httplib::Request & req, httplib::Response & res) {
4506+
if (req.get_header_value("Accept-Encoding").find("gzip") == std::string::npos) {
4507+
res.set_content("Error: gzip is not supported by this browser", "text/plain");
4508+
} else {
4509+
res.set_header("Content-Encoding", "gzip");
4510+
// COEP and COOP headers, required by pyodide (python interpreter)
4511+
res.set_header("Cross-Origin-Embedder-Policy", "require-corp");
4512+
res.set_header("Cross-Origin-Opener-Policy", "same-origin");
4513+
res.set_content(reinterpret_cast<const char*>(index_html_gz), index_html_gz_len, "text/html; charset=utf-8");
4514+
}
4515+
return false;
4516+
});
4517+
4518+
svr->Get("/chat/", [](const httplib::Request & req, httplib::Response & res) {
4519+
res.set_redirect("/chat");
4520+
return false;
4521+
});
4522+
// llama-server-one END
4523+
44554524
// register API routes
44564525
svr->Get ("/health", handle_health); // public endpoint (no API key check)
44574526
svr->Get ("/metrics", handle_metrics);

0 commit comments

Comments
 (0)