8 #include "serverpool.h"
10 #include "udpstream.h"
17 ServerPool::ServerPool(int size)
18 : servers(new Server[size]),
23 CubemapStateProto ServerPool::serialize()
25 CubemapStateProto state;
27 unordered_map<const string *, size_t> short_response_pool;
29 for (int i = 0; i < num_servers; ++i) {
30 CubemapStateProto local_state = servers[i].serialize(&short_response_pool);
32 // The stream state should be identical between the servers, so we only store it once,
33 // save for the fds, which we keep around to distribute to the servers after re-exec.
35 state.mutable_streams()->MergeFrom(local_state.streams());
37 assert(state.streams_size() == local_state.streams_size());
38 for (int j = 0; j < local_state.streams_size(); ++j) {
39 assert(local_state.streams(j).data_fds_size() == 1);
40 state.mutable_streams(j)->add_data_fds(local_state.streams(j).data_fds(0));
43 for (const ClientProto &client : local_state.clients()) {
44 state.add_clients()->MergeFrom(client);
46 for (const HLSZombieProto &hls_zombie : local_state.hls_zombies()) {
47 state.add_hls_zombies()->MergeFrom(hls_zombie);
51 for (size_t i = 0; i < short_response_pool.size(); ++i) {
52 state.mutable_short_response_pool()->Add();
54 for (const auto &string_and_index : short_response_pool) {
55 state.mutable_short_response_pool(string_and_index.second)->set_header_or_short_response(*string_and_index.first);
61 void ServerPool::add_client(int sock, Acceptor *acceptor)
63 servers[clients_added++ % num_servers].add_client_deferred(sock, acceptor);
66 void ServerPool::add_client_from_serialized(const ClientProto &client, const std::vector<std::shared_ptr<const std::string>> &short_responses)
68 servers[clients_added++ % num_servers].add_client_from_serialized(client, short_responses);
71 // It's fine to abuse clients_added here, since it's only ever used for round-robin purposes.
72 void ServerPool::add_hls_zombie_from_serialized(const HLSZombieProto &hls_zombie)
74 servers[clients_added++ % num_servers].add_hls_zombie_from_serialized(hls_zombie);
77 int ServerPool::lookup_stream_by_url(const string &url) const
79 assert(servers != nullptr);
80 return servers[0].lookup_stream_by_url(url);
83 int ServerPool::add_stream(const string &url,
84 const string &hls_url,
86 size_t prebuffering_bytes,
87 Stream::Encoding encoding,
88 Stream::Encoding src_encoding,
89 unsigned hls_frag_duration,
90 size_t hls_backlog_margin,
91 const string &allow_origin)
93 // Adding more HTTP streams after UDP streams would cause the UDP stream
94 // indices to move around, which is obviously not good.
95 assert(udp_streams.empty());
97 for (int i = 0; i < num_servers; ++i) {
98 int stream_index = servers[i].add_stream(url, hls_url, backlog_size, prebuffering_bytes, encoding, src_encoding, hls_frag_duration, hls_backlog_margin, allow_origin);
99 assert(stream_index == num_http_streams);
101 return num_http_streams++;
104 int ServerPool::add_stream_from_serialized(const StreamProto &stream, const vector<int> &data_fds)
106 // Adding more HTTP streams after UDP streams would cause the UDP stream
107 // indices to move around, which is obviously not good.
108 assert(udp_streams.empty());
110 assert(!data_fds.empty());
112 for (int i = 0; i < num_servers; ++i) {
114 if (i < int(data_fds.size())) {
115 // Reuse one of the existing file descriptors.
116 data_fd = data_fds[i];
118 // Clone the first one.
119 if (contents.empty()) {
120 if (!read_tempfile(data_fds[0], &contents)) {
124 data_fd = make_tempfile(contents);
127 int stream_index = servers[i].add_stream_from_serialized(stream, data_fd);
128 assert(stream_index == num_http_streams);
131 // Close and delete any leftovers, if the number of servers was reduced.
132 for (size_t i = num_servers; i < data_fds.size(); ++i) {
133 safe_close(data_fds[i]); // Implicitly deletes the file.
136 return num_http_streams++;
139 int ServerPool::add_udpstream(const sockaddr_in6 &dst, int pacing_rate, int ttl, int multicast_iface_index)
141 udp_streams.emplace_back(new UDPStream(dst, pacing_rate, ttl, multicast_iface_index));
142 return num_http_streams + udp_streams.size() - 1;
145 void ServerPool::set_header(int stream_index, const string &http_header, const string &stream_header)
147 assert(stream_index >= 0 && stream_index < ssize_t(num_http_streams + udp_streams.size()));
149 if (stream_index >= num_http_streams) {
150 // UDP stream. TODO: Log which stream this is.
151 if (!stream_header.empty()) {
152 log(WARNING, "Trying to send stream format with headers to a UDP destination. This is unlikely to work well.");
155 // Ignore the HTTP header.
160 for (int i = 0; i < num_servers; ++i) {
161 servers[i].set_header(stream_index, http_header, stream_header);
165 void ServerPool::add_data(int stream_index, const char *data, size_t bytes, uint16_t metacube_flags, const RationalPTS &pts)
167 assert(stream_index >= 0 && stream_index < ssize_t(num_http_streams + udp_streams.size()));
169 if (stream_index >= num_http_streams) {
171 udp_streams[stream_index - num_http_streams]->send(data, bytes);
176 for (int i = 0; i < num_servers; ++i) {
177 servers[i].add_data_deferred(stream_index, data, bytes, metacube_flags, pts);
181 void ServerPool::add_gen204(const std::string &url, const std::string &allow_origin)
183 for (int i = 0; i < num_servers; ++i) {
184 servers[i].add_gen204(url, allow_origin);
188 void ServerPool::create_tls_context_for_acceptor(const Acceptor *acceptor)
190 for (int i = 0; i < num_servers; ++i) {
191 servers[i].create_tls_context_for_acceptor(acceptor);
195 void ServerPool::run()
197 for (int i = 0; i < num_servers; ++i) {
202 void ServerPool::stop()
204 for (int i = 0; i < num_servers; ++i) {
209 vector<ClientStats> ServerPool::get_client_stats() const
211 vector<ClientStats> ret;
212 for (int i = 0; i < num_servers; ++i) {
213 vector<ClientStats> stats = servers[i].get_client_stats();
214 ret.insert(ret.end(), stats.begin(), stats.end());
219 vector<HLSZombie> ServerPool::get_hls_zombies() const
221 vector<HLSZombie> ret;
222 for (int i = 0; i < num_servers; ++i) {
223 vector<HLSZombie> stats = servers[i].get_hls_zombies();
224 ret.insert(ret.end(), stats.begin(), stats.end());
229 void ServerPool::set_pacing_rate(int stream_index, uint32_t pacing_rate)
231 for (int i = 0; i < num_servers; ++i) {
232 servers[i].set_pacing_rate(stream_index, pacing_rate);
236 void ServerPool::set_backlog_size(int stream_index, size_t new_size)
238 for (int i = 0; i < num_servers; ++i) {
239 servers[i].set_backlog_size(stream_index, new_size);
243 void ServerPool::set_prebuffering_bytes(int stream_index, size_t new_amount)
245 for (int i = 0; i < num_servers; ++i) {
246 servers[i].set_prebuffering_bytes(stream_index, new_amount);
250 void ServerPool::set_encoding(int stream_index, Stream::Encoding encoding)
252 for (int i = 0; i < num_servers; ++i) {
253 servers[i].set_encoding(stream_index, encoding);
257 void ServerPool::set_src_encoding(int stream_index, Stream::Encoding encoding)
259 for (int i = 0; i < num_servers; ++i) {
260 servers[i].set_src_encoding(stream_index, encoding);
264 void ServerPool::set_hls_frag_duration(int stream_index, unsigned hls_frag_duration)
266 for (int i = 0; i < num_servers; ++i) {
267 servers[i].set_hls_frag_duration(stream_index, hls_frag_duration);
271 void ServerPool::set_hls_backlog_margin(int stream_index, size_t hls_backlog_margin)
273 for (int i = 0; i < num_servers; ++i) {
274 servers[i].set_hls_backlog_margin(stream_index, hls_backlog_margin);
278 void ServerPool::set_allow_origin(int stream_index, const std::string &allow_origin)
280 for (int i = 0; i < num_servers; ++i) {
281 servers[i].set_allow_origin(stream_index, allow_origin);
285 void ServerPool::register_hls_url(int stream_index, const string &hls_url)
287 for (int i = 0; i < num_servers; ++i) {
288 servers[i].register_hls_url(stream_index, hls_url);