Răsfoiți Sursa

Change PeriodicGarbageCollectionSeconds behavior

- Calls debug.FreeOSMemory to return memory to
  the OS in addition to garbage collection

- Default on with a 2 minute period.
Rod Hynes 6 ani în urmă
părinte
comite
4ff980cff1
2 a modificat fișierele cu 18 adăugiri și 9 ștergeri
  1. 15 7
      psiphon/server/config.go
  2. 3 2
      psiphon/server/services.go

+ 15 - 7
psiphon/server/config.go

@@ -57,6 +57,7 @@ const (
 	SSH_PASSWORD_BYTE_LENGTH             = 32
 	SSH_RSA_HOST_KEY_BITS                = 2048
 	SSH_OBFUSCATED_KEY_BYTE_LENGTH       = 32
+	PERIODIC_GARBAGE_COLLECTION          = 120 * time.Second
 )
 
 // Config specifies the configuration and behavior of a Psiphon
@@ -308,10 +309,11 @@ type Config struct {
 	// The default, 0 is no limit.
 	MaxConcurrentSSHHandshakes int
 
-	// PeriodicGarbageCollectionSeconds turns on periodic calls to runtime.GC,
-	// every specified number of seconds, to force garbage collection.
-	// The default, 0 is off.
-	PeriodicGarbageCollectionSeconds int
+	// PeriodicGarbageCollectionSeconds turns on periodic calls to
+	// debug.FreeOSMemory, every specified number of seconds, to force garbage
+	// collection and memory scavenging. Specify 0 to disable. The default is
+	// PERIODIC_GARBAGE_COLLECTION.
+	PeriodicGarbageCollectionSeconds *int
 
 	// AccessControlVerificationKeyRing is the access control authorization
 	// verification key ring used to verify signed authorizations presented
@@ -349,8 +351,9 @@ type Config struct {
 	// entries are stored on a Psiphon server.
 	OwnEncodedServerEntries map[string]string
 
-	sshBeginHandshakeTimeout time.Duration
-	sshHandshakeTimeout      time.Duration
+	sshBeginHandshakeTimeout  time.Duration
+	sshHandshakeTimeout       time.Duration
+	periodicGarbageCollection time.Duration
 }
 
 // RunWebServer indicates whether to run a web server component.
@@ -365,7 +368,7 @@ func (config *Config) RunLoadMonitor() bool {
 
 // RunPeriodicGarbageCollection indicates whether to run periodic garbage collection.
 func (config *Config) RunPeriodicGarbageCollection() bool {
-	return config.PeriodicGarbageCollectionSeconds > 0
+	return config.periodicGarbageCollection > 0
 }
 
 // GetOwnEncodedServerEntry returns one of the server's own server entries, as
@@ -485,6 +488,11 @@ func LoadConfig(configJSON []byte) (*Config, error) {
 		}
 	}
 
+	config.periodicGarbageCollection = PERIODIC_GARBAGE_COLLECTION
+	if config.PeriodicGarbageCollectionSeconds != nil {
+		config.periodicGarbageCollection = time.Duration(*config.PeriodicGarbageCollectionSeconds) * time.Second
+	}
+
 	err = accesscontrol.ValidateVerificationKeyRing(&config.AccessControlVerificationKeyRing)
 	if err != nil {
 		return nil, errors.Tracef(

+ 3 - 2
psiphon/server/services.go

@@ -29,6 +29,7 @@ import (
 	"os"
 	"os/signal"
 	"runtime"
+	"runtime/debug"
 	"sync"
 	"syscall"
 	"time"
@@ -135,14 +136,14 @@ func RunServices(configJSON []byte) error {
 		waitGroup.Add(1)
 		go func() {
 			waitGroup.Done()
-			ticker := time.NewTicker(time.Duration(config.PeriodicGarbageCollectionSeconds) * time.Second)
+			ticker := time.NewTicker(config.periodicGarbageCollection)
 			defer ticker.Stop()
 			for {
 				select {
 				case <-shutdownBroadcast:
 					return
 				case <-ticker.C:
-					runtime.GC()
+					debug.FreeOSMemory()
 				}
 			}
 		}()