1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-09-16 01:56:55 +00:00

Refuse connections if not enough RAM

This commit is contained in:
Kane York 2015-11-17 11:01:42 -08:00
parent 0c1d6ed725
commit da790fbaa6
4 changed files with 40 additions and 3 deletions

View file

@ -310,6 +310,7 @@ func GenerateKeys(outputFile, serverID, theirPublicStr string) {
ListenAddr: "0.0.0.0:8001",
SocketOrigin: "localhost:8001",
BackendURL: "http://localhost:8002/ffz",
MinMemoryBytes: 1024 * 1024 * 24,
}
output.ServerID, err = strconv.Atoi(serverID)

View file

@ -107,6 +107,13 @@ var BannerHTML []byte
// It either uses the SocketUpgrader or writes out the BannerHTML.
func HTTPHandleRootURL(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Connection") == "Upgrade" {
updateSysMem()
if Statistics.SysMemTotal-Statistics.SysMemFree < Configuration.MinMemoryBytes {
w.WriteHeader(503)
return
}
conn, err := SocketUpgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Fprintf(w, "error: %v", err)

View file

@ -8,6 +8,7 @@ import (
"time"
linuxproc "github.com/c9s/goprocinfo/linux"
"sync"
)
type StatsData struct {
@ -24,6 +25,8 @@ type StatsData struct {
PubSubChannelCount int
SysMemTotal uint64
SysMemFree uint64
MemoryInUse uint64
MemoryRSS uint64
@ -72,6 +75,7 @@ func newStatsData() *StatsData {
}
}
// SetBuildStamp should be called from the main package to identify the git build hash and build time.
func SetBuildStamp(buildTime, buildHash string) {
Statistics.BuildTime = buildTime
Statistics.BuildHash = buildHash
@ -107,6 +111,7 @@ func updatePeriodicStats() {
Statistics.MemoryRSS = uint64(pstat.Rss * pageSize)
Statistics.MemoryPerClient = Statistics.MemoryRSS / Statistics.CurrentClientCount
}
updateSysMem()
}
{
@ -124,6 +129,27 @@ func updatePeriodicStats() {
}
}
var sysMemLastUpdate time.Time
var sysMemUpdateLock sync.Mutex
func updateSysMem() {
if time.Now().Add(-2 * time.Second).After(sysMemLastUpdate) {
sysMemUpdateLock.Lock()
defer sysMemUpdateLock.Unlock()
if !time.Now().Add(-2 * time.Second).After(sysMemLastUpdate) {
return
}
} else {
return
}
sysMemLastUpdate = time.Now()
memInfo, err := linuxproc.ReadMemInfo("/proc/meminfo")
if err == nil {
Statistics.SysMemTotal = memInfo.MemTotal
Statistics.SysMemFree = memInfo.MemAvailable
}
}
func HTTPShowStatistics(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

View file

@ -20,6 +20,9 @@ type ConfigFile struct {
// URL to the backend server
BackendURL string
// Minimum memory to accept a new connection
MinMemoryBytes uint64
// SSL/TLS
UseSSL bool
SSLCertificateFile string