1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-09-15 17:46: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

@ -307,9 +307,10 @@ func httpError(statusCode int) error {
func GenerateKeys(outputFile, serverID, theirPublicStr string) { func GenerateKeys(outputFile, serverID, theirPublicStr string) {
var err error var err error
output := ConfigFile{ output := ConfigFile{
ListenAddr: "0.0.0.0:8001", ListenAddr: "0.0.0.0:8001",
SocketOrigin: "localhost:8001", SocketOrigin: "localhost:8001",
BackendURL: "http://localhost:8002/ffz", BackendURL: "http://localhost:8002/ffz",
MinMemoryBytes: 1024 * 1024 * 24,
} }
output.ServerID, err = strconv.Atoi(serverID) 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. // It either uses the SocketUpgrader or writes out the BannerHTML.
func HTTPHandleRootURL(w http.ResponseWriter, r *http.Request) { func HTTPHandleRootURL(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Connection") == "Upgrade" { 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) conn, err := SocketUpgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
fmt.Fprintf(w, "error: %v", err) fmt.Fprintf(w, "error: %v", err)

View file

@ -8,6 +8,7 @@ import (
"time" "time"
linuxproc "github.com/c9s/goprocinfo/linux" linuxproc "github.com/c9s/goprocinfo/linux"
"sync"
) )
type StatsData struct { type StatsData struct {
@ -24,6 +25,8 @@ type StatsData struct {
PubSubChannelCount int PubSubChannelCount int
SysMemTotal uint64
SysMemFree uint64
MemoryInUse uint64 MemoryInUse uint64
MemoryRSS 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) { func SetBuildStamp(buildTime, buildHash string) {
Statistics.BuildTime = buildTime Statistics.BuildTime = buildTime
Statistics.BuildHash = buildHash Statistics.BuildHash = buildHash
@ -107,6 +111,7 @@ func updatePeriodicStats() {
Statistics.MemoryRSS = uint64(pstat.Rss * pageSize) Statistics.MemoryRSS = uint64(pstat.Rss * pageSize)
Statistics.MemoryPerClient = Statistics.MemoryRSS / Statistics.CurrentClientCount 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) { func HTTPShowStatistics(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")

View file

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