Browse Source

Add optional explicit garbage collection

Rod Hynes 8 years ago
parent
commit
168d780f4f
3 changed files with 30 additions and 0 deletions
  1. 10 0
      psiphon/server/config.go
  2. 3 0
      psiphon/server/server_test.go
  3. 17 0
      psiphon/server/services.go

+ 10 - 0
psiphon/server/config.go

@@ -284,6 +284,11 @@ type Config struct {
 	// to drop below the limit.
 	// 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
 }
 
 // RunWebServer indicates whether to run a web server component.
@@ -296,6 +301,11 @@ func (config *Config) RunLoadMonitor() bool {
 	return config.LoadMonitorPeriodSeconds > 0
 }
 
+// RunPeriodicGarbageCollection indicates whether to run periodic garbage collection.
+func (config *Config) RunPeriodicGarbageCollection() bool {
+	return config.PeriodicGarbageCollectionSeconds > 0
+}
+
 // LoadConfig loads and validates a JSON encoded server config.
 func LoadConfig(configJSON []byte) (*Config, error) {
 

+ 3 - 0
psiphon/server/server_test.go

@@ -347,6 +347,9 @@ func runServer(t *testing.T, runConfig *runServerConfig) {
 	// TODO: test that the concurrency limit is correctly enforced.
 	serverConfig["MaxConcurrentSSHHandshakes"] = 1
 
+	// Exercise this option.
+	serverConfig["PeriodicGarbageCollectionSeconds"] = 1
+
 	serverConfigJSON, _ = json.Marshal(serverConfig)
 
 	// run server

+ 17 - 0
psiphon/server/services.go

@@ -129,6 +129,23 @@ func RunServices(configJSON []byte) error {
 		}()
 	}
 
+	if config.RunPeriodicGarbageCollection() {
+		waitGroup.Add(1)
+		go func() {
+			waitGroup.Done()
+			ticker := time.NewTicker(time.Duration(config.PeriodicGarbageCollectionSeconds) * time.Second)
+			defer ticker.Stop()
+			for {
+				select {
+				case <-shutdownBroadcast:
+					return
+				case <-ticker.C:
+					runtime.GC()
+				}
+			}
+		}()
+	}
+
 	if config.RunWebServer() {
 		waitGroup.Add(1)
 		go func() {