Fix server-side proxy inappropriate quit when met accept: too many open files error (#2467)

This commit is contained in:
Fishbone 2021-07-05 10:27:15 +08:00 committed by GitHub
parent 3dd888a9ea
commit df5859b5f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 2 deletions

View File

@ -9,6 +9,7 @@ Restart=on-failure
RestartSec=5s RestartSec=5s
ExecStart=/usr/bin/frpc -c /etc/frp/frpc.ini ExecStart=/usr/bin/frpc -c /etc/frp/frpc.ini
ExecReload=/usr/bin/frpc reload -c /etc/frp/frpc.ini ExecReload=/usr/bin/frpc reload -c /etc/frp/frpc.ini
LimitNOFILE=1048576
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@ -3,12 +3,13 @@ Description=Frp Client Service
After=network.target After=network.target
[Service] [Service]
Type=idle Type=simple
User=nobody User=nobody
Restart=on-failure Restart=on-failure
RestartSec=5s RestartSec=5s
ExecStart=/usr/bin/frpc -c /etc/frp/%i.ini ExecStart=/usr/bin/frpc -c /etc/frp/%i.ini
ExecReload=/usr/bin/frpc reload -c /etc/frp/%i.ini ExecReload=/usr/bin/frpc reload -c /etc/frp/%i.ini
LimitNOFILE=1048576
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@ -8,6 +8,7 @@ User=nobody
Restart=on-failure Restart=on-failure
RestartSec=5s RestartSec=5s
ExecStart=/usr/bin/frps -c /etc/frp/frps.ini ExecStart=/usr/bin/frps -c /etc/frp/frps.ini
LimitNOFILE=1048576
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@ -8,6 +8,7 @@ User=nobody
Restart=on-failure Restart=on-failure
RestartSec=5s RestartSec=5s
ExecStart=/usr/bin/frps -c /etc/frp/%i.ini ExecStart=/usr/bin/frps -c /etc/frp/%i.ini
LimitNOFILE=1048576
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@ -21,6 +21,7 @@ import (
"net" "net"
"strconv" "strconv"
"sync" "sync"
"time"
"github.com/fatedier/frp/pkg/config" "github.com/fatedier/frp/pkg/config"
"github.com/fatedier/frp/pkg/msg" "github.com/fatedier/frp/pkg/msg"
@ -151,12 +152,28 @@ func (pxy *BaseProxy) startListenHandler(p Proxy, handler func(Proxy, net.Conn,
xl := xlog.FromContextSafe(pxy.ctx) xl := xlog.FromContextSafe(pxy.ctx)
for _, listener := range pxy.listeners { for _, listener := range pxy.listeners {
go func(l net.Listener) { go func(l net.Listener) {
var tempDelay time.Duration // how long to sleep on accept failure
for { for {
// block // block
// if listener is closed, err returned // if listener is closed, err returned
c, err := l.Accept() c, err := l.Accept()
if err != nil { if err != nil {
xl.Info("listener is closed") if err, ok := err.(interface{ Temporary() bool }); ok && err.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
xl.Info("met temporary error: %s, sleep for %s ...", err, tempDelay)
time.Sleep(tempDelay)
continue
}
xl.Warn("listener is closed: %s", err)
return return
} }
xl.Info("get a user connection [%s]", c.RemoteAddr().String()) xl.Info("get a user connection [%s]", c.RemoteAddr().String())