Author: Lin Guanhong/The Ghost at my Fingertips. Transporters, please: Be sure to identify the source.

The Denver nuggets: juejin. Cn/user / 178526…

Blog: www.cnblogs.com/linguanh/

Making: github.com/af913337456…

Tencent cloud column: cloud.tencent.com/developer/u…


directory

  • Before the order
  • The core content
  • The text start
  • Supervisor services
  • View the number of Supervisor service file handles
  • Modify the program

Before the order

It has been more than a year since my last post. It’s not that I didn’t write technical articles anymore, but most of them were written in drafts and not sent out. Completed my second technical book: Blockchain DApp Development: Based on Ethereum and Bitcoin Public Chain

This article is about a common and insidious problem in back-end services: Too Many Open File handle opening errors.

The core content

When service A is started with Supervisor, the Max Open file Descriptors set by the system will not apply to service A if one of the following conditions is met:

  1. SupervisorNot started as root;
  2. After Supervisor starts, modify the server system configuration.
The solutionOne of the:
  1. First, modify the server systemMaximum number of file handles, see other articles on the web for tutorials;
  2. 1On the basis of, modifySupervisorThe startupsupervisord.confConfiguration file, in[supervisord]Add:Minfds = {number}, for example:
    [supervisord]
    ...
    minfds=102400
    ...
    Copy the code
  3. Restart the supervisor.

That’s the core of this article, and now to the didactic part.

The text start

If you are a backend student and have not encountered Too many Open file errors, the reasons may be as follows:

  1. Insufficient service requests;
  2. Multi-machine load balancing was done early in the service;
  3. Services are not deployed on Linux machines;
  4. A colleague takes care of it for you;
  5. .

There are a lot of general ways to deal with this error on the Internet, but there is less talk about it with conditions attached. When there is a problem, when we search for a solution on the Internet, we usually type the key information directly into the browser, which is a direct error message, such as:

Search: 'Too many open file' and 'Supervisor Too many Open file'Copy the code

Often is one word difference, can day difference, so it is easy to see and the reason is more hidden, because it is difficult to search to the point of the relevant article.

Here is a brief description of Too Many Open Files, a service deployed on a Linux machine that follows the rules of Linux:

  1. In Linux, all objects or resources exist in the form of files, such as messages, shared memory, connections, etc. Handles can be understood as Pointers to these files.
  2. Linux has a limit on the number of handles a single process can open.
  3. An error is thrown when the number of handles the process is trying to open has reached the upper limitToo many open file

Supervisor services

Supervisor is a well-known process daemon program. In many cases, we only use this function: when the server panic for some reason, and automatically pull up the program, restart, based on this function, to define a series of parameters.

Due to the Supervisor’s process management mechanism, it will fork out the child process as the parent, which is the service process specified by the consumer in the configuration file, as in the following example:

# my_server.conf
[program:my_server]
command=/home/server/my --config="".Copy the code

My_server will eventually run in a Supervisor child. Due to the parent-child relationship, the maximum number of files that my_server can open cannot exceed the Supervisor threshold. If you do not rely on the Supervisor to start the service, there is no threshold limit.

If the Supervisor version we are downloading is not specified in the original startup configuration file supervisord.conf for minfDS, and the startup user is not root, then the maximum number of files open is the default 1024 (default). The conclusion here comes from the notes to the document:

The minimum number of file descriptors that must be available before supervisord will start successfully. A call to setrlimit will be made to attempt to raise the soft and hard limits of the supervisord process to satisfy minfds. The hard limit may only be raised if supervisord is run as root. supervisord uses file descriptors liberally, and will enter a failure mode when one cannot be obtained from the OS, So it’s useful to be able to specify a minimum value to ensure it doesn’t run out of them during execution. These limits will be inherited by the managed subprocesses. This option is particularly useful on Solaris, which has a low per-process fd limit by default

If the Supervisor is not started by user root, you can set the minfds parameter to increase the number of file handles. In addition, when the Supervisor is launched by the root user, the raised if supervisord is run as root, while the raised if supervisord is designed to be the same as the Supervisor it is running as root, the raised if supervisord is designed to be the same as the Supervisor it is running as root. I just want you to try it out, see what happens.

View the number of Supervisor service file handles

To see the maximum number of file handles that can actually be opened by a service that relies on Supervisor, do not rely on the ulimit series of commands. Follow these steps to check:

  1. Ps the program name, such as: ps – ef | grep XXX;
  2. Get the process id of XXX: {PID};
  3. The cat/proc / {} PID/limits;
  4. Just watch the screen output

Modify the program

See the core content section above

After the