mirror of
https://github.com/ggerganov/whisper.cpp.git
synced 2025-06-16 06:36:47 +02:00
server: update abort mechanism to handle HTTP connection closure (#3112)
This commit is contained in:
parent
934d4b3083
commit
bcf1ed0163
@ -843,33 +843,25 @@ int main(int argc, char ** argv) {
|
|||||||
wparams.progress_callback_user_data = &user_data;
|
wparams.progress_callback_user_data = &user_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// examples for abort mechanism
|
// tell whisper to abort if the HTTP connection closed
|
||||||
// in examples below, we do not abort the processing, but we could if the flag is set to true
|
wparams.abort_callback = [](void *user_data) {
|
||||||
|
// user_data is a pointer to our Request
|
||||||
// the callback is called before every encoder run - if it returns false, the processing is aborted
|
auto req_ptr = static_cast<const httplib::Request*>(user_data);
|
||||||
{
|
return req_ptr->is_connection_closed();
|
||||||
static bool is_aborted = false; // NOTE: this should be atomic to avoid data race
|
};
|
||||||
|
wparams.abort_callback_user_data = (void*)&req;
|
||||||
wparams.encoder_begin_callback = [](struct whisper_context * /*ctx*/, struct whisper_state * /*state*/, void * user_data) {
|
|
||||||
bool is_aborted = *(bool*)user_data;
|
|
||||||
return !is_aborted;
|
|
||||||
};
|
|
||||||
wparams.encoder_begin_callback_user_data = &is_aborted;
|
|
||||||
}
|
|
||||||
|
|
||||||
// the callback is called before every computation - if it returns true, the computation is aborted
|
|
||||||
{
|
|
||||||
static bool is_aborted = false; // NOTE: this should be atomic to avoid data race
|
|
||||||
|
|
||||||
wparams.abort_callback = [](void * user_data) {
|
|
||||||
bool is_aborted = *(bool*)user_data;
|
|
||||||
return is_aborted;
|
|
||||||
};
|
|
||||||
wparams.abort_callback_user_data = &is_aborted;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (whisper_full_parallel(ctx, wparams, pcmf32.data(), pcmf32.size(), params.n_processors) != 0) {
|
if (whisper_full_parallel(ctx, wparams, pcmf32.data(), pcmf32.size(), params.n_processors) != 0) {
|
||||||
|
// handle failure or early abort
|
||||||
|
if (req.is_connection_closed()) {
|
||||||
|
// log client disconnect
|
||||||
|
fprintf(stderr, "client disconnected, aborted processing\n");
|
||||||
|
res.status = 499; // Client Closed Request (nginx convention)
|
||||||
|
res.set_content("{\"error\":\"client disconnected\"}", "application/json");
|
||||||
|
return;
|
||||||
|
}
|
||||||
fprintf(stderr, "%s: failed to process audio\n", argv[0]);
|
fprintf(stderr, "%s: failed to process audio\n", argv[0]);
|
||||||
|
res.status = 500; // Internal Server Error
|
||||||
const std::string error_resp = "{\"error\":\"failed to process audio\"}";
|
const std::string error_resp = "{\"error\":\"failed to process audio\"}";
|
||||||
res.set_content(error_resp, "application/json");
|
res.set_content(error_resp, "application/json");
|
||||||
return;
|
return;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user