Add set_resource_limit.
GitOrigin-RevId: 29cf122b31ff86ccc8f6c1fc3b71c28e89b8054f
This commit is contained in:
parent
480f826d16
commit
7cd42fc518
@ -58,6 +58,7 @@ set(TDUTILS_SOURCE
|
|||||||
td/utils/port/MemoryMapping.cpp
|
td/utils/port/MemoryMapping.cpp
|
||||||
td/utils/port/path.cpp
|
td/utils/port/path.cpp
|
||||||
td/utils/port/PollFlags.cpp
|
td/utils/port/PollFlags.cpp
|
||||||
|
td/utils/port/rlimit.cpp
|
||||||
td/utils/port/ServerSocketFd.cpp
|
td/utils/port/ServerSocketFd.cpp
|
||||||
td/utils/port/signals.cpp
|
td/utils/port/signals.cpp
|
||||||
td/utils/port/sleep.cpp
|
td/utils/port/sleep.cpp
|
||||||
@ -133,6 +134,7 @@ set(TDUTILS_SOURCE
|
|||||||
td/utils/port/Poll.h
|
td/utils/port/Poll.h
|
||||||
td/utils/port/PollBase.h
|
td/utils/port/PollBase.h
|
||||||
td/utils/port/PollFlags.h
|
td/utils/port/PollFlags.h
|
||||||
|
td/utils/port/rlimit.h
|
||||||
td/utils/port/RwMutex.h
|
td/utils/port/RwMutex.h
|
||||||
td/utils/port/ServerSocketFd.h
|
td/utils/port/ServerSocketFd.h
|
||||||
td/utils/port/signals.h
|
td/utils/port/signals.h
|
||||||
|
57
tdutils/td/utils/port/rlimit.cpp
Normal file
57
tdutils/td/utils/port/rlimit.cpp
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
//
|
||||||
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
#include "td/utils/port/rlimit.h"
|
||||||
|
|
||||||
|
#include "td/utils/port/config.h"
|
||||||
|
|
||||||
|
#include "td/utils/misc.h"
|
||||||
|
|
||||||
|
#if TD_PORT_POSIX
|
||||||
|
#include <sys/resource.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
#if TD_PORT_POSIX
|
||||||
|
static int get_resource(ResourceLimitType type) {
|
||||||
|
switch (type) {
|
||||||
|
case ResourceLimitType::NoFile:
|
||||||
|
return RLIMIT_NOFILE;
|
||||||
|
default:
|
||||||
|
UNREACHABLE();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Status set_resource_limit(ResourceLimitType type, uint64 value) {
|
||||||
|
#if TD_PORT_POSIX
|
||||||
|
int resource = get_resource(type);
|
||||||
|
|
||||||
|
rlimit rlim;
|
||||||
|
if (getrlimit(resource, &rlim) == -1) {
|
||||||
|
return OS_ERROR("Failed to get current resource limit");
|
||||||
|
}
|
||||||
|
|
||||||
|
TRY_RESULT(new_value, narrow_cast_safe<rlim_t>(value));
|
||||||
|
if (rlim.rlim_max < new_value) {
|
||||||
|
rlim.rlim_max = new_value;
|
||||||
|
}
|
||||||
|
rlim.rlim_cur = new_value;
|
||||||
|
|
||||||
|
if (setrlimit(resource, &rlim) < 0) {
|
||||||
|
return OS_ERROR("Failed to set resource limit");
|
||||||
|
}
|
||||||
|
return Status::OK();
|
||||||
|
#elif TD_PORT_WINDOWS
|
||||||
|
return Status::OK(); // Windows has no limits
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace td
|
18
tdutils/td/utils/port/rlimit.h
Normal file
18
tdutils/td/utils/port/rlimit.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
//
|
||||||
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "td/utils/common.h"
|
||||||
|
#include "td/utils/Status.h"
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
enum class ResourceLimitType { NoFile };
|
||||||
|
|
||||||
|
Status set_resource_limit(ResourceLimitType type, uint64 value);
|
||||||
|
|
||||||
|
} // namespace td
|
Loading…
x
Reference in New Issue
Block a user