Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions llamafile/server/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,14 +520,23 @@ Client::send_response_finish()
//
// unlike send() this won't fail if binary content is detected.
bool
Client::send_binary(const void* p, size_t n)
{
ssize_t sent;
if ((sent = write(fd_, p, n)) != n) {
if (sent == -1 && errno != EAGAIN && errno != ECONNRESET)
SLOG("write failed %m");
close_connection_ = true;
return false;
Client::send_binary(const void* p, size_t n) {
const char* buf = (const char*)p;
size_t written = 0;
while (written < n) {
ssize_t sent = write(fd_, buf + written, n - written);
if (sent == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// no data can be written right now; retry
continue;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The right thing to do here would be to call poll() I think to wait until it's writable.

}
if (errno != ECONNRESET)
SLOG("write failed %m");
close_connection_ = true;
return false;
}
// sent ≥ 0
written += sent;
}
return true;
}
Expand Down Expand Up @@ -775,7 +784,7 @@ Client::dispatcher()
should_send_error_if_canceled_ = false;
if (!send(std::string_view(obuf_.p, p - obuf_.p)))
return false;
char buf[512];
char buf[16384];
size_t i, chunk;
for (i = 0; i < size; i += chunk) {
chunk = size - i;
Expand Down
7 changes: 0 additions & 7 deletions llamafile/server/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ Worker::begin()
tokens = tokenbucket_acquire(client_.client_ip_);
server_->lock();
dll_remove(&server_->idle_workers, &elem_);
if (dll_is_empty(server_->idle_workers)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered increasing the maximum number of threads? One of the design goals with the server is to have it be able to manage its resources. I'd ideally like to see us doing it in a better way than simply giving up on this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a good compromise here would be this: we can add a flag to disable this. That will get you unblocked fastest.

Dll* slowbro;
if ((slowbro = dll_last(server_->active_workers))) {
SLOG("all threads active! dropping oldest client");
WORKER(slowbro)->kill();
}
}
working_ = true;
if (tokens > FLAG_token_burst) {
dll_make_last(&server_->active_workers, &elem_);
Expand Down