/
gls.go
152 lines (140 loc) · 2.98 KB
/
gls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package glweb
import (
"io"
"net/http"
"sync"
"time"
log "github.com/nzlov/glog"
"github.com/nzlov/go/gllog"
"github.com/nzlov/go/glstr"
"github.com/layeh/gopher-json"
"github.com/layeh/gopher-luar"
"github.com/yuin/gopher-lua"
)
type Task struct {
Name string
Method string
Action string
Version string
Fn string
Info string
}
func (self *Task) exec(l *lua.LState, w http.ResponseWriter, r *http.Request, m map[string]string) {
log.Infoln("Task", self.Name, self.Version, self.Method,
self.Action, self.Fn, m, "Exec")
if err := l.CallByParam(lua.P{
Fn: l.GetGlobal(self.Fn),
NRet: 3,
Protect: true,
}, luar.New(l, luaResponseWriter{w}), luar.New(l, r), luar.New(l, m)); err != nil {
io.WriteString(w, "Error:"+err.Error())
log.Errorln("Task", self.Name, self.Version, self.Method,
self.Action, self.Fn, "Call", err)
} else {
log.Infoln("Task", self.Name, self.Version, self.Method,
self.Action, self.Fn, m, "Exec OK")
}
}
type Tasks struct {
Name string
Task []*Task
}
type glf struct {
name string
path string
script string
tasks map[string]*Task
m sync.Mutex
lstates []*lua.LState
t *time.Ticker
isRunning bool
}
func newglf(p, script string) *glf {
g := &glf{path: p, script: script}
g.tasks = make(map[string]*Task)
g.m = sync.Mutex{}
g.lstates = make([]*lua.LState, 0)
g.t = time.NewTicker(time.Second)
g.isRunning = true
go g.cleanl()
return g
}
func (self *glf) cleanl() {
for self.isRunning {
select {
case <-self.t.C:
self.m.Lock()
o := len(self.lstates)
dr := self.lstates[0 : o/2]
self.lstates = self.lstates[o/2 : o]
self.m.Unlock()
for _, l := range dr {
l.Close()
}
}
}
}
func (self *glf) addtask(t *Task) {
self.tasks[t.Name] = t
}
func (self *glf) close() {
self.m.Lock()
self.isRunning = false
for _, l := range self.lstates {
l.Close()
}
self.m.Unlock()
}
func (self *glf) put(L *lua.LState) {
self.m.Lock()
L.SetTop(0)
if self.isRunning {
self.lstates = append(self.lstates, L)
} else {
L.Close()
}
self.m.Unlock()
}
func (self *glf) get() *lua.LState {
if !self.isRunning {
return nil
}
self.m.Lock()
n := len(self.lstates)
if n == 0 {
self.m.Unlock()
return self.new()
}
x := self.lstates[n-1]
self.lstates = self.lstates[0 : n-1]
self.m.Unlock()
return x
}
func (self *glf) new() *lua.LState {
l := lua.NewState()
json.Preload(l)
gllog.Preload(l)
glstr.Preload(l)
l.PreloadModule("http", glhttp.Loader)
// l.SetGlobal("VPASS", luar.New(l, vpass))
if db != nil {
l.SetGlobal("GLDB", luar.New(l, db))
}
if err := l.DoString(self.script); err != nil {
log.Errorln("GLF", self.path, "DoString", err)
return nil
}
return l
}
func (self *glf) exec(name string, w http.ResponseWriter, r *http.Request, m map[string]string) {
if t, ok := self.tasks[name]; ok {
log.Debugln("glf", "lstate", len(self.lstates))
l := self.get()
if l == nil {
http.NotFound(w, r)
return
}
t.exec(l, w, r, m)
self.put(l)
}
}