From 4c673df4c825d045a569be6123dd625ee1c99e25 Mon Sep 17 00:00:00 2001
From: Ubuntu <ubuntu@ip-172-31-7-118.ap-southeast-2.compute.internal>
Date: Sat, 21 Nov 2015 13:05:09 +0000
Subject: [PATCH] allow setting threshold ram usage via flag. Fix a bug with
 nil interface conversion.

---
 posting/lists.go | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/posting/lists.go b/posting/lists.go
index 8abab343..96342559 100644
--- a/posting/lists.go
+++ b/posting/lists.go
@@ -17,6 +17,7 @@
 package posting
 
 import (
+	"flag"
 	"math/rand"
 	"runtime"
 	"runtime/debug"
@@ -31,6 +32,9 @@ import (
 	"github.com/zond/gotomic"
 )
 
+var maxmemory = flag.Uint64("threshold_ram_mb", 3072,
+	"If RAM usage exceeds this, we stop the world, and flush our buffers.")
+
 type counters struct {
 	ticker *time.Ticker
 	added  uint64
@@ -58,7 +62,7 @@ var MIB uint64
 
 func checkMemoryUsage() {
 	MIB = 1 << 20
-	MAX_MEMORY = 3 * (1 << 30)
+	MAX_MEMORY = *maxmemory * (1 << 20)
 
 	for _ = range time.Tick(5 * time.Second) {
 		var ms runtime.MemStats
@@ -126,6 +130,9 @@ func GetOrCreate(key []byte) *List {
 
 func processOne(k gotomic.Hashable, c *counters) {
 	ret, _ := lhmap.Delete(k)
+	if ret == nil {
+		return
+	}
 	l := ret.(*List)
 	if l == nil {
 		return
-- 
GitLab