* 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) 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, redbench.Bench("EVAL (set point)", addr, opts, prepFn,
func(buf []byte) []byte { func(buf []byte) []byte {
i := atomic.AddInt64(&i, 1) 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.processLives()
go c.watchOutOfMemory() go c.watchOutOfMemory()
go c.watchLuaStatePool()
go c.watchAutoGC() go c.watchAutoGC()
go c.backgroundExpiring() go c.backgroundExpiring()
defer func() { 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) { func (c *Controller) setCol(key string, col *collection.Collection) {
c.cols.ReplaceOrInsert(&collectionT{Key: key, Collection: col}) 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 // NewPool returns a new pool of lua states
func (c *Controller) NewPool() *lStatePool { func (c *Controller) NewPool() *lStatePool {
pl := &lStatePool{ pl := &lStatePool{
saved: make([]*lua.LState, 0), saved: make([]*lua.LState, iniLuaPoolSize),
c: c, c: c,
} }
// Fill the pool with some ready handlers // Fill the pool with some ready handlers
for i := 0; i < iniLuaPoolSize; i++ { for i := 0; i < iniLuaPoolSize; i++ {
pl.Put(pl.New()) pl.saved[i] = pl.New()
pl.total++ pl.total++
} }
return pl return pl
@ -68,6 +68,23 @@ func (pl *lStatePool) Get() (*lua.LState, error) {
return x, nil 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 { func (pl *lStatePool) New() *lua.LState {
L := lua.NewState() L := lua.NewState()

View File

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