* Add periodic pruning of the lua state pool
This commit is contained in:
Alex Roitman 2017-10-05 14:13:02 -07:00 committed by Josh Baker
parent d6fe2eec96
commit bfa4bbe237
4 changed files with 63 additions and 35 deletions

View File

@ -319,30 +319,6 @@ func main() {
fmt.Println("SET SCRIPT: " + set_script)
}
redbench.Bench("EVALRO (get point)", addr, opts, prepFn,
func(buf []byte) []byte {
i := atomic.AddInt64(&i, 1)
return redbench.AppendCommand(buf, "EVALRO", get_script, "1", "key:bench", "id:"+strconv.FormatInt(i, 10))
},
)
redbench.Bench("EVALRO (get 4 points)", addr, opts, prepFn,
func(buf []byte) []byte {
i := atomic.AddInt64(&i, 1)
return redbench.AppendCommand(buf, "EVALRO", get4_script, "1",
"key:bench",
"id:"+strconv.FormatInt(i, 10),
"id:"+strconv.FormatInt(i+1, 10),
"id:"+strconv.FormatInt(i+2, 10),
"id:"+strconv.FormatInt(i+3, 10),
)
},
)
redbench.Bench("EVALNA (get point)", addr, opts, prepFn,
func(buf []byte) []byte {
i := atomic.AddInt64(&i, 1)
return redbench.AppendCommand(buf, "EVALNA", get_script, "1", "key:bench", "id:"+strconv.FormatInt(i, 10))
},
)
redbench.Bench("EVAL (set point)", addr, opts, prepFn,
func(buf []byte) []byte {
i := atomic.AddInt64(&i, 1)
@ -367,6 +343,30 @@ func main() {
)
},
)
redbench.Bench("EVALRO (get point)", addr, opts, prepFn,
func(buf []byte) []byte {
i := atomic.AddInt64(&i, 1)
return redbench.AppendCommand(buf, "EVALRO", get_script, "1", "key:bench", "id:"+strconv.FormatInt(i, 10))
},
)
redbench.Bench("EVALRO (get 4 points)", addr, opts, prepFn,
func(buf []byte) []byte {
i := atomic.AddInt64(&i, 1)
return redbench.AppendCommand(buf, "EVALRO", get4_script, "1",
"key:bench",
"id:"+strconv.FormatInt(i, 10),
"id:"+strconv.FormatInt(i+1, 10),
"id:"+strconv.FormatInt(i+2, 10),
"id:"+strconv.FormatInt(i+3, 10),
)
},
)
redbench.Bench("EVALNA (get point)", addr, opts, prepFn,
func(buf []byte) []byte {
i := atomic.AddInt64(&i, 1)
return redbench.AppendCommand(buf, "EVALNA", get_script, "1", "key:bench", "id:"+strconv.FormatInt(i, 10))
},
)
}
}
}

View File

@ -194,6 +194,7 @@ func ListenAndServeEx(host string, port int, dir string, ln *net.Listener, http
}()
go c.processLives()
go c.watchOutOfMemory()
go c.watchLuaStatePool()
go c.watchAutoGC()
go c.backgroundExpiring()
defer func() {
@ -318,6 +319,16 @@ func (c *Controller) watchOutOfMemory() {
}
}
func (c *Controller) watchLuaStatePool() {
t := time.NewTicker(time.Second * 10)
defer t.Stop()
for range t.C {
func() {
c.luapool.Prune()
}()
}
}
func (c *Controller) setCol(key string, col *collection.Collection) {
c.cols.ReplaceOrInsert(&collectionT{Key: key, Collection: col})
}

View File

@ -41,12 +41,12 @@ type lStatePool struct {
// NewPool returns a new pool of lua states
func (c *Controller) NewPool() *lStatePool {
pl := &lStatePool{
saved: make([]*lua.LState, 0),
saved: make([]*lua.LState, iniLuaPoolSize),
c: c,
}
// Fill the pool with some ready handlers
for i := 0; i < iniLuaPoolSize; i++ {
pl.Put(pl.New())
pl.saved[i] = pl.New()
pl.total++
}
return pl
@ -68,6 +68,23 @@ func (pl *lStatePool) Get() (*lua.LState, error) {
return x, nil
}
// Prune removes some of the idle lua states from the pool
func (pl *lStatePool) Prune() {
pl.m.Lock()
n := len(pl.saved)
if n > iniLuaPoolSize {
// drop half of the idle states that is above the minimum
dropNum := (n - iniLuaPoolSize) / 2
if dropNum < 1 {
dropNum = 1
}
newSaved := make([]*lua.LState, n - dropNum)
copy(newSaved, pl.saved[dropNum:])
pl.saved = newSaved
}
pl.m.Unlock()
}
func (pl *lStatePool) New() *lua.LState {
L := lua.NewState()

View File

@ -1332,7 +1332,7 @@
"multiple": true
}
],
"since": "1.9.2",
"since": "1.10.0",
"group": "scripting"
},
"EVALSHA":{
@ -1360,7 +1360,7 @@
"multiple": true
}
],
"since": "1.9.2",
"since": "1.10.0",
"group": "scripting"
},
"EVALRO":{
@ -1388,7 +1388,7 @@
"multiple": true
}
],
"since": "1.9.2",
"since": "1.10.0",
"group": "scripting"
},
"EVALROSHA":{
@ -1416,7 +1416,7 @@
"multiple": true
}
],
"since": "1.9.2",
"since": "1.10.0",
"group": "scripting"
},
"EVALNA":{
@ -1444,7 +1444,7 @@
"multiple": true
}
],
"since": "1.9.2",
"since": "1.10.0",
"group": "scripting"
},
"EVALNASHA":{
@ -1472,7 +1472,7 @@
"multiple": true
}
],
"since": "1.9.2",
"since": "1.10.0",
"group": "scripting"
},
"SCRIPT EXISTS":{
@ -1485,7 +1485,7 @@
"multiple": true
}
],
"since": "1.9.2",
"since": "1.10.0",
"group": "scripting"
},
"SCRIPT LOAD":{
@ -1497,13 +1497,13 @@
"type": "string"
}
],
"since": "1.9.2",
"since": "1.10.0",
"group": "scripting"
},
"SCRIPT FLUSH":{
"summary": "Flushes the server cache of Lua scripts",
"complexity": "O(1)",
"since": "1.9.2",
"since": "1.10.0",
"group": "scripting"
}
}