| /** @file | |
| A brief file description | |
| @par License | |
| Licensed to the Apache Software Foundation (ASF) under one | |
| or more contributor license agreements. See the NOTICE file | |
| distributed with this work for additional information | |
| regarding copyright ownership. The ASF licenses this file | |
| to you under the Apache License, Version 2.0 (the | |
| "License"); you may not use this file except in compliance | |
| with the License. You may obtain a copy of the License at | |
| http://www.apache.org/licenses/LICENSE-2.0 | |
| Unless required by applicable law or agreed to in writing, software | |
| distributed under the License is distributed on an "AS IS" BASIS, | |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| See the License for the specific language governing permissions and | |
| limitations under the License. | |
| */ | |
| #include "P_Net.h" | |
| #include "P_UnixNet.h" | |
| #include "iocore/net/NetHandler.h" | |
| #include "iocore/net/PollCont.h" | |
| #if TS_USE_LINUX_IO_URING | |
| #include "iocore/io_uring/IO_URING.h" | |
| #endif | |
| #include <algorithm> | |
| #include <atomic> | |
| #include <optional> | |
| using namespace std::literals; | |
| namespace | |
| { | |
| DbgCtl dbg_ctl_net_queue{"net_queue"}; | |
| DbgCtl dbg_ctl_v_net_queue{"v_net_queue"}; | |
| } // end anonymous namespace | |
| std::atomic<int32_t> NetHandler::additional_accepts{0}; | |
| std::atomic<uint32_t> NetHandler::per_client_max_connections_in{0}; | |
| // NetHandler method definitions | |
| NetHandler::NetHandler() : Continuation(nullptr) | |
| { | |
| SET_HANDLER(&NetHandler::mainNetEvent); | |
| } | |
| int | |
| NetHandler::startIO(NetEvent *ne) | |
| { | |
| ink_assert(this->mutex->thread_holding == this_ethread()); | |
| ink_assert(ne->get_thread() == this_ethread()); | |
| int res = 0; | |
| PollDescriptor *pd = get_PollDescriptor(this->thread); | |
| // The read and write masks intentionally share the epoll edge-trigger flag. | |
| if (ne->ep.start(pd, ne, this, EVENTIO_READ | EVENTIO_WRITE) < 0) { // NOLINT(misc-redundant-expression) | |
| res = errno; | |
| // EEXIST should be ok, though it should have been cleared before we got back here | |
| if (errno != EEXIST) { | |
| Dbg(dbg_ctl_iocore_net, "NetHandler::startIO : failed on EventIO::start, errno = [%d](%s)", errno, strerror(errno)); | |
| return -res; | |
| } | |
| } | |
| if (ne->read.triggered == 1) { | |
| read_ready_list.enqueue(ne); | |
| } | |
| ne->nh = this; | |
| return res; | |
| } | |
| void | |
| NetHandler::stopIO(NetEvent *ne) | |
| { | |
| ink_release_assert(ne->nh == this); | |
| ne->ep.stop(); | |
| read_ready_list.remove(ne); | |
| write_ready_list.remove(ne); | |
| if (ne->read.in_enabled_list) { | |
| read_enable_list.remove(ne); | |
| ne->read.in_enabled_list = 0; | |
| } | |
| if (ne->write.in_enabled_list) { | |
| write_enable_list.remove(ne); | |
| ne->write.in_enabled_list = 0; | |
| } | |
| if (ne->handoff.load() & NetEvent::HANDOFF_QUEUED) { | |
| handoff_list.remove(ne); | |
| ne->handoff.store(0); | |
| } | |
| ne->nh = nullptr; | |
| } | |
| void | |
| NetHandler::startCop(NetEvent *ne) | |
| { | |
| ink_assert(this->mutex->thread_holding == this_ethread()); | |
| ink_release_assert(ne->nh == this); | |
| ink_assert(!open_list.in(ne)); | |
| // Apply the global default here, not lazily in the cop: lazy application forced | |
| // the cop to visit every open connection regardless of deadlines, and left the | |
| // connection with no usable deadline at registration time. | |
| if (ne->default_inactivity_timeout_in.load(std::memory_order_relaxed) == -1) { | |
| ne->set_default_inactivity_timeout(HRTIME_SECONDS(config.default_inactivity_timeout)); | |
| } | |
| open_list.enqueue(ne); | |
| rearm_timer(ne); | |
| } | |
| void | |
| NetHandler::stopCop(NetEvent *ne) | |
| { | |
| ink_release_assert(ne->nh == this); | |
| open_list.remove(ne); | |
| remove_from_keep_alive_queue(ne); | |
| remove_from_active_queue(ne); | |
| timer_wheel.cancel(ne); | |
| } | |
| ink_hrtime | |
| NetHandler::_earliest_deadline(NetEvent *ne) const | |
| { | |
| ink_hrtime const inactivity = ne->next_inactivity_timeout_at; | |
| ink_hrtime const activity = ne->next_activity_timeout_at; | |
| if (inactivity == 0) { | |
| return activity; | |
| } | |
| if (activity == 0) { | |
| return inactivity; | |
| } | |
| return std::min(inactivity, activity); | |
| } | |
| void | |
| NetHandler::rearm_timer(NetEvent *ne) | |
| { | |
| ink_assert(thread == this_ethread()); | |
| // A closed NetEvent must be reaped on the next tick rather than at its | |
| // original deadline, or the fd lingers. | |
| ink_hrtime const deadline = ne->closed ? ink_get_hrtime() : _earliest_deadline(ne); | |
| if (deadline == 0) { | |
| timer_wheel.cancel(ne); | |
| } else { | |
| timer_wheel.schedule(ne, deadline); | |
| } | |
| } | |
| void | |
| NetHandler::defer_rearm(NetEvent *ne) | |
| { | |
| if (!(ne->handoff.fetch_or(NetEvent::HANDOFF_QUEUED) & NetEvent::HANDOFF_QUEUED)) { | |
| handoff_list.push(ne); | |
| } | |
| } | |
| void | |
| NetHandler::process_handoffs() | |
| { | |
| SList(NetEvent, handoff_link) q(handoff_list.popall()); | |
| while (NetEvent *ne = q.pop()) { | |
| uint8_t const state = ne->handoff.exchange(0); | |
| if (state & NetEvent::HANDOFF_CLOSING) { | |
| if (!ne->closed.load(std::memory_order_acquire)) { | |
| // Claimed by a closing thread that has not marked it closed yet. Keep | |
| // the claim, so nothing frees it under that thread, and look again on | |
| // the next loop. | |
| ne->handoff.store(state); | |
| handoff_list.push(ne); | |
| continue; | |
| } | |
| if (ne->handler_on_stack()) { | |
| // One of ne's own callbacks is running on this thread, and it reads ne | |
| // again as it unwinds. The queue managers call this nested from | |
| // add_to_*_queue(), which handlers reach from inside that callback, so | |
| // this is reachable whenever a foreign close lands during one. Keep the | |
| // claim and leave ne for the next top-level drain; the unwind's own | |
| // free_netevent() is a no-op while the claim is held. | |
| ne->handoff.store(state); | |
| handoff_list.push(ne); | |
| continue; | |
| } | |
| free_netevent(ne); | |
| continue; | |
| } | |
| rearm_timer(ne); | |
| } | |
| } | |
| void | |
| NetHandler::close_from_foreign_thread(NetEvent *ne, int closed_value) | |
| { | |
| // Claim first: once closed is visible the owner may try to free ne, and the | |
| // claim makes free_netevent() leave it for process_handoffs(). The claim is | |
| // only released after the owner pops ne, which needs the push below, so ne | |
| // stays valid until this function is done with it. | |
| uint8_t const prev = ne->handoff.fetch_or(NetEvent::HANDOFF_QUEUED | NetEvent::HANDOFF_CLOSING); | |
| // Release, so an owner that sees closed also sees the claim. | |
| ne->closed.store(closed_value, std::memory_order_release); | |
| if (!(prev & NetEvent::HANDOFF_QUEUED)) { | |
| handoff_list.push(ne); | |
| } | |
| if (likely(thread)) { | |
| thread->tail_cb->signalActivity(); | |
| } else if (trigger_event) { | |
| trigger_event->ethread->tail_cb->signalActivity(); | |
| } | |
| } | |
| int | |
| NetHandler::update_nethandler_config(const char *str, RecDataT, RecData data, void *) | |
| { | |
| std::optional<Config::Index> updated_index; // which config value the record maps to, if any. | |
| std::string_view name{str}; | |
| if (name == "proxy.config.net.max_connections_in"sv) { | |
| updated_index = Config::Index::MAX_CONNECTIONS_IN; | |
| Dbg(dbg_ctl_net_queue, "proxy.config.net.max_connections_in updated to %" PRId64, data.rec_int); | |
| } else if (name == "proxy.config.net.max_requests_in"sv) { | |
| updated_index = Config::Index::MAX_REQUESTS_IN; | |
| Dbg(dbg_ctl_net_queue, "proxy.config.net.max_requests_in updated to %" PRId64, data.rec_int); | |
| } else if (name == "proxy.config.net.default_inactivity_timeout"sv) { | |
| updated_index = Config::Index::DEFAULT_INACTIVITY_TIMEOUT; | |
| Dbg(dbg_ctl_net_queue, "proxy.config.net.default_inactivity_timeout updated to %" PRId64, data.rec_int); | |
| } else if (name == "proxy.config.net.inactivity_cop_budget"sv) { | |
| updated_index = Config::Index::INACTIVITY_COP_BUDGET; | |
| Dbg(dbg_ctl_net_queue, "proxy.config.net.inactivity_cop_budget updated to %" PRId64, data.rec_int); | |
| } else if (name == "proxy.config.net.additional_accepts"sv) { | |
| NetHandler::additional_accepts.store(data.rec_int, std::memory_order_relaxed); | |
| Dbg(dbg_ctl_net_queue, "proxy.config.net.additional_accepts updated to %" PRId64, data.rec_int); | |
| } else if (name == "proxy.config.net.per_client.max_connections_in"sv) { | |
| NetHandler::per_client_max_connections_in.store(data.rec_int, std::memory_order_relaxed); | |
| Dbg(dbg_ctl_net_queue, "proxy.config.net.per_client.max_connections_in updated to %" PRId64, data.rec_int); | |
| } | |
| if (updated_index) { | |
| global_config[*updated_index] = data.rec_int; // do the actual update. | |
| // portable form of the update, an index converted to <void*> so it can be passed as an event cookie. | |
| void *idx = reinterpret_cast<void *>(static_cast<intptr_t>(*updated_index)); | |
| // Signal the NetHandler instances, passing the index of the updated config value. | |
| for (int i = 0; i < eventProcessor.n_thread_groups; ++i) { | |
| if (!active_thread_types[i]) { | |
| continue; | |
| } | |
| for (EThread **tp = eventProcessor.thread_group[i]._thread, | |
| **limit = eventProcessor.thread_group[i]._thread + eventProcessor.thread_group[i]._count; | |
| tp < limit; ++tp) { | |
| NetHandler *nh = get_NetHandler(*tp); | |
| if (nh) { | |
| nh->thread->schedule_imm(nh, TS_EVENT_MGMT_UPDATE, idx); | |
| } | |
| } | |
| } | |
| } | |
| return REC_ERR_OKAY; | |
| } | |
| void | |
| NetHandler::init_for_process() | |
| { | |
| // read configuration values and setup callbacks for when they change | |
| global_config.max_connections_in = RecGetRecordInt("proxy.config.net.max_connections_in").value_or(0); | |
| global_config.max_requests_in = RecGetRecordInt("proxy.config.net.max_requests_in").value_or(0); | |
| global_config.default_inactivity_timeout = RecGetRecordInt("proxy.config.net.default_inactivity_timeout").value_or(0); | |
| global_config.inactivity_cop_budget = RecGetRecordInt("proxy.config.net.inactivity_cop_budget").value_or(4096); | |
| // Atomic configurations. | |
| { | |
| int32_t val = 0; | |
| val = RecGetRecordInt("proxy.config.net.additional_accepts").value_or(0); | |
| additional_accepts.store(val, std::memory_order_relaxed); | |
| } | |
| { | |
| uint32_t val = 0; | |
| val = RecGetRecordInt("proxy.config.net.per_client.max_connections_in").value_or(0); | |
| per_client_max_connections_in.store(val, std::memory_order_relaxed); | |
| } | |
| RecRegisterConfigUpdateCb("proxy.config.net.max_connections_in", update_nethandler_config, nullptr); | |
| RecRegisterConfigUpdateCb("proxy.config.net.max_requests_in", update_nethandler_config, nullptr); | |
| RecRegisterConfigUpdateCb("proxy.config.net.default_inactivity_timeout", update_nethandler_config, nullptr); | |
| RecRegisterConfigUpdateCb("proxy.config.net.inactivity_cop_budget", update_nethandler_config, nullptr); | |
| RecRegisterConfigUpdateCb("proxy.config.net.additional_accepts", update_nethandler_config, nullptr); | |
| RecRegisterConfigUpdateCb("proxy.config.net.per_client.max_connections_in", update_nethandler_config, nullptr); | |
| Dbg(dbg_ctl_net_queue, "proxy.config.net.max_connections_in updated to %d", global_config.max_connections_in); | |
| Dbg(dbg_ctl_net_queue, "proxy.config.net.max_requests_in updated to %d", global_config.max_requests_in); | |
| Dbg(dbg_ctl_net_queue, "proxy.config.net.default_inactivity_timeout updated to %d", global_config.default_inactivity_timeout); | |
| Dbg(dbg_ctl_net_queue, "proxy.config.net.additional_accepts updated to %d", additional_accepts.load(std::memory_order_relaxed)); | |
| Dbg(dbg_ctl_net_queue, "proxy.config.net.per_client.max_connections_in updated to %d", | |
| per_client_max_connections_in.load(std::memory_order_relaxed)); | |
| } | |
| // | |
| // Function used to release a NetEvent and free it. | |
| // | |
| void | |
| NetHandler::free_netevent(NetEvent *ne) | |
| { | |
| EThread *t = this->thread; | |
| ink_assert(t == this_ethread()); | |
| // A foreign close is still handing ne over; process_handoffs() frees it. | |
| // Acquire pairs with the release of closed in close_from_foreign_thread(). | |
| (void)ne->closed.load(std::memory_order_acquire); | |
| if (ne->handoff.load() & NetEvent::HANDOFF_CLOSING) { | |
| return; | |
| } | |
| ink_release_assert(ne->get_thread() == t); | |
| ink_release_assert(ne->nh == this); | |
| // Release ne from InactivityCop | |
| stopCop(ne); | |
| // Release ne from NetHandler | |
| stopIO(ne); | |
| // Clear and deallocate ne | |
| ne->free_thread(t); | |
| } | |
| // | |
| // Move VC's enabled on a different thread to the ready list | |
| // | |
| void | |
| NetHandler::process_enabled_list() | |
| { | |
| NetEvent *ne = nullptr; | |
| process_handoffs(); | |
| SListM(NetEvent, NetState, read, enable_link) rq(read_enable_list.popall()); | |
| while ((ne = rq.pop())) { | |
| // A closed NetEvent is only passing through to be freed, and a migrated one | |
| // has already had ep.stop() clear its event loop. | |
| if (!ne->closed) { | |
| ne->ep.modify(EVENTIO_READ); | |
| ne->ep.refresh(EVENTIO_READ); | |
| } | |
| ne->read.in_enabled_list = 0; | |
| if ((ne->read.enabled && ne->read.triggered) || ne->closed) { | |
| read_ready_list.in_or_enqueue(ne); | |
| } | |
| } | |
| SListM(NetEvent, NetState, write, enable_link) wq(write_enable_list.popall()); | |
| while ((ne = wq.pop())) { | |
| if (!ne->closed) { | |
| ne->ep.modify(EVENTIO_WRITE); | |
| ne->ep.refresh(EVENTIO_WRITE); | |
| } | |
| ne->write.in_enabled_list = 0; | |
| if ((ne->write.enabled && ne->write.triggered) || ne->closed) { | |
| write_ready_list.in_or_enqueue(ne); | |
| } | |
| } | |
| } | |
| // | |
| // Walk through the ready list | |
| // | |
| void | |
| NetHandler::process_ready_list() | |
| { | |
| NetEvent *ne = nullptr; | |
| #if defined(USE_EDGE_TRIGGER) | |
| // NetEvent * | |
| while ((ne = read_ready_list.dequeue())) { | |
| // Initialize the thread-local continuation flags | |
| set_cont_flags(ne->get_control_flags()); | |
| if (ne->closed) { | |
| free_netevent(ne); | |
| } else if (ne->read.enabled && ne->read.triggered) { | |
| ne->net_read_io(this); | |
| } else if (!ne->read.enabled) { | |
| read_ready_list.remove(ne); | |
| } | |
| } | |
| while ((ne = write_ready_list.dequeue())) { | |
| set_cont_flags(ne->get_control_flags()); | |
| if (ne->closed) { | |
| free_netevent(ne); | |
| } else if (ne->write.enabled && ne->write.triggered) { | |
| ne->net_write_io(this); | |
| } else if (!ne->write.enabled) { | |
| write_ready_list.remove(ne); | |
| } | |
| } | |
| #else /* !USE_EDGE_TRIGGER */ | |
| while ((ne = read_ready_list.dequeue())) { | |
| set_cont_flags(ne->get_control_flags()); | |
| if (ne->closed) | |
| free_netevent(ne); | |
| else if (ne->read.enabled && ne->read.triggered) | |
| ne->net_read_io(this, this->thread); | |
| else if (!ne->read.enabled) | |
| ne->ep.modify(-EVENTIO_READ); | |
| } | |
| while ((ne = write_ready_list.dequeue())) { | |
| set_cont_flags(ne->get_control_flags()); | |
| if (ne->closed) | |
| free_netevent(ne); | |
| else if (ne->write.enabled && ne->write.triggered) | |
| write_to_net(this, ne, this->thread); | |
| else if (!ne->write.enabled) | |
| ne->ep.modify(-EVENTIO_WRITE); | |
| } | |
| #endif /* !USE_EDGE_TRIGGER */ | |
| } | |
| // | |
| // The main event for NetHandler | |
| int | |
| NetHandler::mainNetEvent(int event, Event *e) | |
| { | |
| if (TS_EVENT_MGMT_UPDATE == event) { | |
| // Check the raw value before converting: narrowing to the underlying type | |
| // could otherwise wrap a bad cookie onto a valid index. | |
| auto raw = reinterpret_cast<intptr_t>(e->cookie); | |
| ink_release_assert(raw >= 0 && raw < CONFIG_ITEM_COUNT); | |
| auto idx = static_cast<Config::Index>(raw); | |
| // Copy the updated value to the instance struct. | |
| config[idx] = global_config[idx]; | |
| if (config_value_affects_per_thread_value[static_cast<size_t>(idx)]) { | |
| this->configure_per_thread_values(); | |
| } | |
| return EVENT_CONT; | |
| } else { | |
| ink_assert(trigger_event == e && (event == EVENT_INTERVAL || event == EVENT_POLL)); | |
| return this->waitForActivity(-1); | |
| } | |
| } | |
| int | |
| NetHandler::waitForActivity(ink_hrtime timeout) | |
| { | |
| EventIO *epd = nullptr; | |
| #if TS_USE_LINUX_IO_URING | |
| IOUringContext *ur = IOUringContext::local_context(); | |
| #endif | |
| Metrics::Counter::increment(net_rsb.handler_run); | |
| SCOPED_MUTEX_LOCK(lock, mutex, this->thread); | |
| process_enabled_list(); | |
| #if TS_USE_LINUX_IO_URING | |
| ur->submit(); | |
| #endif | |
| // Polling event by PollCont | |
| PollCont *p = get_PollCont(this->thread); | |
| ink_hrtime pre_poll = ink_get_hrtime(); | |
| { | |
| // Only do_poll blocks. Everything else in this function - the enabled list, dispatching ready | |
| // descriptors below, the ready list - is work the event loop is awake for. | |
| Watchdog::SleepScope sleep_scope{this->thread->heartbeat_state}; | |
| p->do_poll(timeout); | |
| } | |
| ink_hrtime post_poll = ink_get_hrtime(); | |
| ink_hrtime poll_time = post_poll - pre_poll; | |
| // Get & Process polling result | |
| PollDescriptor *pd = get_PollDescriptor(this->thread); | |
| for (int x = 0; x < pd->result; x++) { | |
| epd = static_cast<EventIO *> get_ev_data(pd, x); | |
| int flags = get_ev_events(pd, x); | |
| epd->process_event(flags); | |
| ev_next_event(pd, x); | |
| } | |
| pd->result = 0; | |
| process_ready_list(); | |
| ink_hrtime post_process = ink_get_hrtime(); | |
| ink_hrtime process_time = post_process - post_poll; | |
| this->thread->metrics.current_slice.load(std::memory_order_acquire)->record_io_stats(poll_time, process_time); | |
| #if TS_USE_LINUX_IO_URING | |
| ur->service(); | |
| #endif | |
| return EVENT_CONT; | |
| } | |
| void | |
| NetHandler::signalActivity() | |
| { | |
| #if HAVE_EVENTFD | |
| uint64_t counter = 1; | |
| ATS_UNUSED_RETURN(write(thread->evfd, &counter, sizeof(uint64_t))); | |
| #else | |
| char dummy = 1; | |
| ATS_UNUSED_RETURN(write(thread->evpipe[1], &dummy, 1)); | |
| #endif | |
| } | |
| bool | |
| NetHandler::manage_active_queue(NetEvent *enabling_ne, bool ignore_queue_size = false) | |
| { | |
| const int total_connections_in = active_queue_size + keep_alive_queue_size; | |
| Dbg(dbg_ctl_v_net_queue, | |
| "max_connections_per_thread_in: %d max_requests_per_thread_in: %d total_connections_in: %d " | |
| "active_queue_size: %d keep_alive_queue_size: %d", | |
| max_connections_per_thread_in, max_requests_per_thread_in, total_connections_in, active_queue_size, keep_alive_queue_size); | |
| if (!max_requests_per_thread_in) { | |
| // active queue has no max | |
| return true; | |
| } | |
| if (ignore_queue_size == false && max_requests_per_thread_in > active_queue_size) { | |
| return true; | |
| } | |
| // As in manage_keep_alive_queue(): drop connections closed on other threads | |
| // before deciding the queue is full. | |
| process_handoffs(); | |
| if (ignore_queue_size == false && max_requests_per_thread_in > active_queue_size) { | |
| return true; | |
| } | |
| ink_hrtime now = ink_get_hrtime(); | |
| // loop over the non-active connections and try to close them | |
| NetEvent *ne = active_queue.head; | |
| NetEvent *ne_next = nullptr; | |
| int closed = 0; | |
| int handle_event = 0; | |
| int total_idle_time = 0; | |
| int total_idle_count = 0; | |
| for (; ne != nullptr; ne = ne_next) { | |
| ne_next = ne->active_queue_link.next; | |
| // It seems dangerous closing the current ne at this point | |
| // Let the activity_cop deal with it | |
| if (ne == enabling_ne) { | |
| continue; | |
| } | |
| if ((ne->next_inactivity_timeout_at && ne->next_inactivity_timeout_at <= now) || | |
| (ne->next_activity_timeout_at && ne->next_activity_timeout_at <= now)) { | |
| _close_ne(ne, now, handle_event, closed, total_idle_time, total_idle_count); | |
| } | |
| if (ignore_queue_size == false && max_requests_per_thread_in > active_queue_size) { | |
| return true; | |
| } | |
| } | |
| if (max_requests_per_thread_in > active_queue_size) { | |
| return true; | |
| } | |
| return false; // failed to make room in the queue, all connections are active | |
| } | |
| void | |
| NetHandler::configure_per_thread_values() | |
| { | |
| // figure out the number of threads and calculate the number of connections per thread | |
| int threads = eventProcessor.thread_group[ET_NET]._count; | |
| max_connections_per_thread_in = config.max_connections_in / threads; | |
| max_requests_per_thread_in = config.max_requests_in / threads; | |
| Dbg(dbg_ctl_net_queue, "max_connections_per_thread_in updated to %d threads: %d", max_connections_per_thread_in, threads); | |
| Dbg(dbg_ctl_net_queue, "max_requests_per_thread_in updated to %d threads: %d", max_requests_per_thread_in, threads); | |
| } | |
| void | |
| NetHandler::manage_keep_alive_queue() | |
| { | |
| uint32_t total_connections_in = active_queue_size + keep_alive_queue_size; | |
| ink_hrtime now = ink_get_hrtime(); | |
| Dbg(dbg_ctl_v_net_queue, | |
| "max_connections_per_thread_in: %d total_connections_in: %d active_queue_size: %d keep_alive_queue_size: %d", | |
| max_connections_per_thread_in, total_connections_in, active_queue_size, keep_alive_queue_size); | |
| if (!max_connections_per_thread_in || total_connections_in <= max_connections_per_thread_in) { | |
| return; | |
| } | |
| // A connection closed on another thread stays queued, and counted, until its | |
| // handoff is processed. Free those first so a live one is not evicted in its | |
| // place. | |
| process_handoffs(); | |
| total_connections_in = active_queue_size + keep_alive_queue_size; | |
| if (total_connections_in <= max_connections_per_thread_in) { | |
| return; | |
| } | |
| // loop over the non-active connections and try to close them | |
| NetEvent *ne_next = nullptr; | |
| int closed = 0; | |
| int handle_event = 0; | |
| int total_idle_time = 0; | |
| int total_idle_count = 0; | |
| for (NetEvent *ne = keep_alive_queue.head; ne != nullptr; ne = ne_next) { | |
| ne_next = ne->keep_alive_queue_link.next; | |
| _close_ne(ne, now, handle_event, closed, total_idle_time, total_idle_count); | |
| total_connections_in = active_queue_size + keep_alive_queue_size; | |
| if (total_connections_in <= max_connections_per_thread_in) { | |
| break; | |
| } | |
| } | |
| if (total_idle_count > 0) { | |
| Dbg(dbg_ctl_net_queue, "max cons: %d active: %d idle: %d already closed: %d, close event: %d mean idle: %d", | |
| max_connections_per_thread_in, total_connections_in, keep_alive_queue_size, closed, handle_event, | |
| total_idle_time / total_idle_count); | |
| } | |
| } | |
| void | |
| NetHandler::_close_ne(NetEvent *ne, ink_hrtime now, int &handle_event, int &closed, int &total_idle_time, int &total_idle_count) | |
| { | |
| if (ne->get_thread() != this_ethread()) { | |
| return; | |
| } | |
| MUTEX_TRY_LOCK(lock, ne->get_mutex(), this_ethread()); | |
| if (!lock.is_locked()) { | |
| return; | |
| } | |
| ink_hrtime diff = (now - (ne->next_inactivity_timeout_at - ne->inactivity_timeout_in)) / HRTIME_SECOND; | |
| if (diff > 0) { | |
| total_idle_time += diff; | |
| ++total_idle_count; | |
| Metrics::Counter::increment(net_rsb.keep_alive_queue_timeout_total, diff); | |
| Metrics::Counter::increment(net_rsb.keep_alive_queue_timeout_count); | |
| } | |
| Dbg(dbg_ctl_net_queue, "closing connection NetEvent=%p idle: %u now: %" PRId64 " at: %" PRId64 " in: %" PRId64 " diff: %" PRId64, | |
| ne, keep_alive_queue_size, ink_hrtime_to_sec(now), ink_hrtime_to_sec(ne->next_inactivity_timeout_at), | |
| ink_hrtime_to_sec(ne->inactivity_timeout_in), diff); | |
| if (ne->closed) { | |
| free_netevent(ne); | |
| ++closed; | |
| } else { | |
| // Decide which timeout to report before clobbering the deadline below, | |
| // since that assignment destroys the evidence. Inactivity wins when both | |
| // have expired, matching InactivityCop's precedence. Neither expired means | |
| // this is a capacity eviction rather than a timeout, and inactivity is the | |
| // honest label for reclaiming an idle connection - reporting nothing at all | |
| // left the caller's queue over capacity with nothing closed. | |
| bool const inactivity_expired = ne->next_inactivity_timeout_at && ne->next_inactivity_timeout_at <= now; | |
| bool const active_expired = ne->next_activity_timeout_at && ne->next_activity_timeout_at <= now; | |
| int const timeout_event = (active_expired && !inactivity_expired) ? VC_EVENT_ACTIVE_TIMEOUT : VC_EVENT_INACTIVITY_TIMEOUT; | |
| ne->next_inactivity_timeout_at = now; | |
| ne->rearm_timer(); | |
| if (ne->callback(timeout_event) == EVENT_DONE) { | |
| ++handle_event; | |
| } | |
| } | |
| } | |
| void | |
| NetHandler::add_to_keep_alive_queue(NetEvent *ne) | |
| { | |
| Dbg(dbg_ctl_net_queue, "NetEvent: %p", ne); | |
| ink_assert(mutex->thread_holding == this_ethread()); | |
| if (keep_alive_queue.in(ne)) { | |
| // already in the keep-alive queue, move the head | |
| keep_alive_queue.remove(ne); | |
| } else { | |
| // in the active queue or no queue, new to this queue | |
| remove_from_active_queue(ne); | |
| ++keep_alive_queue_size; | |
| } | |
| keep_alive_queue.enqueue(ne); | |
| // if keep-alive queue is over size then close connections | |
| manage_keep_alive_queue(); | |
| } | |
| void | |
| NetHandler::remove_from_keep_alive_queue(NetEvent *ne) | |
| { | |
| Dbg(dbg_ctl_net_queue, "NetEvent: %p", ne); | |
| ink_assert(mutex->thread_holding == this_ethread()); | |
| if (keep_alive_queue.in(ne)) { | |
| keep_alive_queue.remove(ne); | |
| --keep_alive_queue_size; | |
| } | |
| } | |
| bool | |
| NetHandler::add_to_active_queue(NetEvent *ne) | |
| { | |
| Dbg(dbg_ctl_net_queue, "NetEvent: %p", ne); | |
| Dbg(dbg_ctl_net_queue, "max_connections_per_thread_in: %d active_queue_size: %d keep_alive_queue_size: %d", | |
| max_connections_per_thread_in, active_queue_size, keep_alive_queue_size); | |
| ink_assert(mutex->thread_holding == this_ethread()); | |
| bool active_queue_full = false; | |
| // if active queue is over size then close inactive connections | |
| if (manage_active_queue(ne) == false) { | |
| active_queue_full = true; | |
| } | |
| if (active_queue.in(ne)) { | |
| // already in the active queue, move the head | |
| active_queue.remove(ne); | |
| } else { | |
| if (active_queue_full) { | |
| // there is no room left in the queue | |
| Metrics::Counter::increment(net_rsb.requests_max_throttled_in); | |
| return false; | |
| } | |
| // in the keep-alive queue or no queue, new to this queue | |
| remove_from_keep_alive_queue(ne); | |
| ++active_queue_size; | |
| } | |
| active_queue.enqueue(ne); | |
| return true; | |
| } | |
| void | |
| NetHandler::remove_from_active_queue(NetEvent *ne) | |
| { | |
| Dbg(dbg_ctl_net_queue, "NetEvent: %p", ne); | |
| ink_assert(mutex->thread_holding == this_ethread()); | |
| if (active_queue.in(ne)) { | |
| active_queue.remove(ne); | |
| --active_queue_size; | |
| } | |
| } | |
| int | |
| NetHandler::get_additional_accepts() | |
| { | |
| int config_value = additional_accepts.load(std::memory_order_relaxed) + 1; | |
| return (config_value > 0 ? config_value : INT32_MAX - 1); | |
| } | |
| int | |
| NetHandler::get_per_client_max_connections_in() | |
| { | |
| int config_value = per_client_max_connections_in.load(std::memory_order_relaxed); | |
| return std::max(config_value, 0); | |
| } |