Skip to content

Commit 40cb924

Browse files
Merge pull request #17 from PeerPrep/ajay/executor-reuse-dirs
Executor reuse directories
2 parents f1915b4 + 3484d7e commit 40cb924

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

executor/src/exec_endpoint.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <unistd.h>
1818
#include <fcntl.h>
1919

20+
constexpr long DIR_NAME_LEN = 12;
21+
2022
constexpr long BUF_LEN = 1024;
2123
constexpr long MAX_RESPONSE_SIZE = 10 * 1024;
2224

@@ -44,9 +46,10 @@ bool read_to_buffer(std::array<uint8_t, BUFFER_SIZE>& buffer, long& read_size, i
4446
return read_size > 0;
4547
}
4648

47-
cppevent::awaitable_task<void> upload(std::string_view dir, std::string_view name,
48-
cppevent::output& o_stdout) {
49-
int fd = executor::open_file(dir, name, O_RDONLY);
49+
cppevent::awaitable_task<void> executor::exec_endpoint::upload(std::string_view dir,
50+
std::string_view name,
51+
cppevent::output& o_stdout) {
52+
int fd = open_file(dir, name, O_RDONLY);
5053
std::array<uint8_t, BUF_LEN> buffer;
5154
long response_size = 0;
5255
long read_size;
@@ -55,6 +58,7 @@ cppevent::awaitable_task<void> upload(std::string_view dir, std::string_view nam
5558
response_size += read_size;
5659
}
5760
close(fd);
61+
m_unused_dirs.push(std::string { dir });
5862
}
5963

6064
const std::unordered_map<std::string_view, std::string_view> source_file_names = {
@@ -74,20 +78,24 @@ cppevent::awaitable_task<void> executor::exec_endpoint::process(const cppevent::
7478
co_await o_stdout.write("status: 400\ncontent-length: 16\n\nunknown language");
7579
co_return;
7680
}
77-
char dir_name[12];
78-
strcpy(dir_name, "code_XXXXXX");
79-
if (mkdtemp(dir_name) == NULL) {
81+
82+
char dir_name[DIR_NAME_LEN];
83+
strncpy(dir_name, "code_XXXXXX", DIR_NAME_LEN);
84+
if (!m_unused_dirs.empty()) {
85+
strncpy(dir_name, m_unused_dirs.front().c_str(), DIR_NAME_LEN);
86+
m_unused_dirs.pop();
87+
} else if (mkdtemp(dir_name) == NULL) {
8088
co_await o_stdout.write("status: 500\ncontent-type: text/plain\n\n");
8189
co_await o_stdout.write(strerror(errno));
8290
co_return;
8391
}
8492

8593
const std::string_view& source_file_name = it->second;
86-
const int source_fd = open_file(dir_name, source_file_name, O_WRONLY | O_CREAT);
94+
const int source_fd = open_file(dir_name, source_file_name, O_WRONLY | O_CREAT | O_TRUNC);
8795
co_await download(content_len, s_stdin, source_fd);
8896
close(source_fd);
8997

90-
const int compile_err_fd = open_file(dir_name, COMPILE_ERR, O_WRONLY | O_CREAT);
98+
const int compile_err_fd = open_file(dir_name, COMPILE_ERR, O_WRONLY | O_CREAT | O_TRUNC);
9199
bool compiled_success = co_await await_compile(compile_err_fd, m_loop, dir_name, lang);
92100
close(compile_err_fd);
93101

@@ -97,7 +105,7 @@ cppevent::awaitable_task<void> executor::exec_endpoint::process(const cppevent::
97105
co_return;
98106
}
99107

100-
const int run_out_fd = open_file(dir_name, RUN_STDOUT, O_WRONLY | O_CREAT);
108+
const int run_out_fd = open_file(dir_name, RUN_STDOUT, O_WRONLY | O_CREAT | O_TRUNC);
101109
CODE_EXEC_STATUS run_status = co_await await_run(run_out_fd, m_loop, dir_name, lang);
102110
close(run_out_fd);
103111

@@ -112,6 +120,5 @@ cppevent::awaitable_task<void> executor::exec_endpoint::process(const cppevent::
112120
co_await o_stdout.write("status: 200\nx-exec-status: success\ncontent-type: text/plain\n\n");
113121
break;
114122
}
115-
116123
co_await upload(dir_name, RUN_STDOUT, o_stdout);
117124
}

executor/src/exec_endpoint.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <cppevent_fcgi/endpoint.hpp>
55

66
#include <string_view>
7+
#include <string>
8+
#include <queue>
79

810
namespace cppevent {
911

@@ -16,6 +18,10 @@ namespace executor {
1618
class exec_endpoint : public cppevent::endpoint {
1719
private:
1820
cppevent::event_loop& m_loop;
21+
std::queue<std::string> m_unused_dirs;
22+
23+
cppevent::awaitable_task<void> upload(std::string_view dir, std::string_view name,
24+
cppevent::output& o_stdout);
1925
public:
2026
exec_endpoint(cppevent::event_loop& e_loop);
2127

0 commit comments

Comments
 (0)