Skip to content

Commit 1fb4a10

Browse files
authored
Merge pull request #138 from devfeel/develop
Version 1.5.5
2 parents e82c705 + 79fa181 commit 1fb4a10

File tree

4 files changed

+58
-45
lines changed

4 files changed

+58
-45
lines changed

core/state.go

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ func init() {
2424
ServerStartTime: time.Now(),
2525
TotalRequestCount: 0,
2626
TotalErrorCount: 0,
27+
CurrentRequestCount: 0,
2728
IntervalRequestData: NewItemMap(),
2829
DetailRequestURLData: NewItemMap(),
2930
IntervalErrorData: NewItemMap(),
3031
DetailErrorPageData: NewItemMap(),
3132
DetailErrorData: NewItemMap(),
3233
DetailHTTPCodeData: NewItemMap(),
33-
dataChan_Request: make(chan *RequestInfo, 1000),
34+
dataChan_Request: make(chan *RequestInfo, 2000),
3435
dataChan_Error: make(chan *ErrorInfo, 1000),
35-
dataChan_HttpCode: make(chan *HttpCodeInfo, 1000),
3636
infoPool: &pool{
3737
requestInfo: sync.Pool{
3838
New: func() interface{} {
@@ -44,11 +44,6 @@ func init() {
4444
return &ErrorInfo{}
4545
},
4646
},
47-
httpCodeInfo: sync.Pool{
48-
New: func() interface{} {
49-
return &HttpCodeInfo{}
50-
},
51-
},
5247
},
5348
}
5449
go GlobalState.handleInfo()
@@ -76,12 +71,6 @@ type ErrorInfo struct {
7671
Num uint64
7772
}
7873

79-
//httpcode count info
80-
type HttpCodeInfo struct {
81-
URL string
82-
Code int
83-
Num uint64
84-
}
8574

8675
//服务器状态信息
8776
type ServerStateInfo struct {
@@ -91,6 +80,8 @@ type ServerStateInfo struct {
9180
EnabledDetailRequestData bool
9281
//该运行期间总访问次数
9382
TotalRequestCount uint64
83+
//当前活跃的请求数
84+
CurrentRequestCount uint64
9485
//单位时间内请求数据 - 按分钟为单位
9586
IntervalRequestData *ItemMap
9687
//明细请求页面数据 - 以不带参数的访问url为key
@@ -108,7 +99,6 @@ type ServerStateInfo struct {
10899

109100
dataChan_Request chan *RequestInfo
110101
dataChan_Error chan *ErrorInfo
111-
dataChan_HttpCode chan *HttpCodeInfo
112102
//对象池
113103
infoPool *pool
114104
}
@@ -124,6 +114,8 @@ func (state *ServerStateInfo) ShowHtmlData(version string) string {
124114
data += "<br>"
125115
data += "TotalRequestCount : " + strconv.FormatUint(state.TotalRequestCount, 10)
126116
data += "<br>"
117+
data += "CurrentRequestCount : " + strconv.FormatUint(state.CurrentRequestCount, 10)
118+
data += "<br>"
127119
data += "TotalErrorCount : " + strconv.FormatUint(state.TotalErrorCount, 10)
128120
data += "<br>"
129121
state.IntervalRequestData.RLock()
@@ -164,13 +156,20 @@ func (state *ServerStateInfo) QueryIntervalErrorData(queryKey string) uint64 {
164156
}
165157

166158
//AddRequestCount 增加请求数
167-
func (state *ServerStateInfo) AddRequestCount(page string, code int, num uint64) uint64 {
168-
if strings.Index(page, "/dotweb/") != 0 {
169-
atomic.AddUint64(&state.TotalRequestCount, num)
170-
state.addRequestData(page, code, num)
171-
state.addHTTPCodeData(page, code, num)
172-
}
173-
return state.TotalRequestCount
159+
func (state *ServerStateInfo) AddRequestCount(page string, code int, num uint64) {
160+
state.addRequestData(page, code, num)
161+
}
162+
163+
//AddCurrentRequest 增加请求数
164+
func (state *ServerStateInfo) AddCurrentRequest(num uint64) uint64 {
165+
atomic.AddUint64(&state.CurrentRequestCount, num)
166+
return state.CurrentRequestCount
167+
}
168+
169+
//SubCurrentRequest 消除请求数
170+
func (state *ServerStateInfo) SubCurrentRequest(num uint64) uint64 {
171+
atomic.AddUint64(&state.CurrentRequestCount, ^uint64(num-1))
172+
return state.CurrentRequestCount
174173
}
175174

176175
//AddErrorCount 增加错误数
@@ -198,21 +197,16 @@ func (state *ServerStateInfo) addErrorData(page string, err error, num uint64) {
198197
state.dataChan_Error <- info
199198
}
200199

201-
func (state *ServerStateInfo) addHTTPCodeData(page string, code int, num uint64) {
202-
//get from pool
203-
info := state.infoPool.httpCodeInfo.Get().(*HttpCodeInfo)
204-
info.URL = page
205-
info.Code = code
206-
info.Num = num
207-
state.dataChan_HttpCode <- info
208-
}
209200

210201
//处理日志内部函数
211202
func (state *ServerStateInfo) handleInfo() {
212203
for {
213204
select {
214205
case info := <-state.dataChan_Request:
215206
{
207+
if strings.Index(info.URL, "/dotweb/") != 0 {
208+
atomic.AddUint64(&state.TotalRequestCount, info.Num)
209+
}
216210
//fixed #63 状态数据,当url较多时,导致内存占用过大
217211
if state.EnabledDetailRequestData {
218212
//ignore 404 request
@@ -228,6 +222,11 @@ func (state *ServerStateInfo) handleInfo() {
228222
val := state.IntervalRequestData.GetUInt64(key)
229223
state.IntervalRequestData.Set(key, val+info.Num)
230224

225+
//set code data
226+
key = strconv.Itoa(info.Code)
227+
val = state.DetailHTTPCodeData.GetUInt64(key)
228+
state.DetailHTTPCodeData.Set(key, val+info.Num)
229+
231230
//put info obj
232231
state.infoPool.requestInfo.Put(info)
233232
}
@@ -251,16 +250,6 @@ func (state *ServerStateInfo) handleInfo() {
251250
//put info obj
252251
state.infoPool.errorInfo.Put(info)
253252
}
254-
case info := <-state.dataChan_HttpCode:
255-
{
256-
//set detail error page data
257-
key := strconv.Itoa(info.Code)
258-
val := state.DetailHTTPCodeData.GetUInt64(key)
259-
state.DetailHTTPCodeData.Set(key, val+info.Num)
260-
261-
//put info obj
262-
state.infoPool.httpCodeInfo.Put(info)
263-
}
264253
}
265254
}
266255
}

core/state_test.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,25 @@ func Test_AddRequestCount_1(t *testing.T) {
2424

2525
func addRequestCount(wg *sync.WaitGroup, count int) {
2626
for i := 0; i < count; i++ {
27-
GlobalState.AddRequestCount("test", 1)
27+
GlobalState.AddRequestCount("test", 200, 1)
2828
}
2929
wg.Add(-1)
3030
}
3131

32+
func Test_CurrentRequestCount(t *testing.T) {
33+
//var num uint64 = 1
34+
GlobalState.AddCurrentRequest(1000465)
35+
t.Log(GlobalState.CurrentRequestCount)
36+
GlobalState.SubCurrentRequest(2561)
37+
t.Log(GlobalState.CurrentRequestCount)
38+
}
39+
3240
func Test_AddRequestCount_2(t *testing.T) {
3341
var num uint64 = 1
34-
var count uint64
3542
for i := 0; i < 100; i++ {
36-
count = GlobalState.AddRequestCount("test", num)
43+
GlobalState.AddRequestCount("test", 200, num)
3744
num++
3845
}
39-
t.Log("TotalRequestCount:", count)
4046
}
4147

4248
func Test_AddErrorCount_1(t *testing.T) {
@@ -92,16 +98,26 @@ func Benchmark_AddErrorCount_Parallel(b *testing.B) {
9298
func Benchmark_AddRequestCount_1(b *testing.B) {
9399
var num uint64 = 1
94100
for i := 0; i < b.N; i++ {
95-
GlobalState.AddRequestCount("test", num)
101+
GlobalState.AddRequestCount("test", 200, num)
102+
}
103+
}
104+
105+
106+
//基准测试
107+
func Benchmark_AddCurrentRequestCount_1(b *testing.B) {
108+
var num uint64 = 1
109+
for i := 0; i < b.N; i++ {
110+
GlobalState.AddCurrentRequest(num)
96111
}
97112
}
98113

114+
99115
// 测试并发效率
100116
func Benchmark_AddRequestCount_Parallel(b *testing.B) {
101117
b.RunParallel(func(pb *testing.PB) {
102118
var num uint64 = 1
103119
for pb.Next() {
104-
GlobalState.AddRequestCount("test", num)
120+
GlobalState.AddRequestCount("test", 200, num)
105121
}
106122
})
107123
}

server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ func (server *HttpServer) ListenAndServeTLS(addr string, certFile, keyFile strin
126126

127127
// ServeHTTP make sure request can be handled correctly
128128
func (server *HttpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
129+
core.GlobalState.AddCurrentRequest(1)
130+
defer core.GlobalState.SubCurrentRequest(1)
131+
129132
//针对websocket与调试信息特殊处理
130133
if checkIsWebSocketRequest(req) {
131134
http.DefaultServeMux.ServeHTTP(w, req)

version.MD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## dotweb版本记录:
22

3+
#### Version 1.5.5
4+
* New feature: /dotweb/state add CurrentRequestCount data
5+
* Update: improve 30% performance on app's metric
6+
* 2018-08-09 15:00
7+
38
#### Version 1.5.4
49
* New feature: Session.StoreConfig support CookieName, used to set custom cookie name which sessionid store, default is dotweb_sessionId
510
* Update: Config.SessionNode add CookieName, used to set custom cookie name which sessionid store

0 commit comments

Comments
 (0)