game_sync/dbproxy/rpcstate.go

76 lines
1.8 KiB
Go

package main
import (
"sync"
"time"
)
const Timeout = 30 * time.Second
type RPCState struct {
RunTimes int64 //执行次数
TotalRuningTime int64 //总执行时间
MaxRuningTime int64 //最长执行时间
TimeoutTimes int64 //执行超时次数 大于30秒的次数
FailTimes int64 //执行失败次数
SuccessTimes int64 //执行成功次数
}
var RPCStateMgr = make(map[string]*RPCState)
var RPCStateMgrLock = sync.RWMutex{}
func GetPPCState() map[string]*RPCState {
ret := make(map[string]*RPCState)
RPCStateMgrLock.RLock()
defer RPCStateMgrLock.RUnlock()
for k, v := range RPCStateMgr {
e := *v // 复制一份
ret[k] = &e
}
return ret
}
//func loggingMiddleware(h http.Handler) http.Handler {
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// RPCStateMgrLock.Lock()
// state, ok := RPCStateMgr[r.URL.Path]
// if !ok {
// state = &RPCState{}
// RPCStateMgr[r.URL.Path] = state
// }
// RPCStateMgrLock.Unlock()
//
// // 记录请求的时间戳
// start := time.Now()
//
// var buf bytes.Buffer
// if r.Body != nil {
// tee := io.TeeReader(r.Body, &buf)
// r.Body = io.NopCloser(tee)
// }
//
// logger.Logger.Infof("==>RPC %s %s %s", r.Method, r.URL.Path, buf.String())
// // 包装响应写入器以捕获响应内容
// rw := &responseWriter{w, http.StatusOK}
// h.ServeHTTP(rw, r)
// // 记录请求完成时间和响应状态码
// duration := time.Since(start).Milliseconds()
//
// RPCStateMgrLock.Lock()
// state.RunTimes++
// state.TotalRuningTime += duration
// if duration > state.MaxRuningTime {
// state.MaxRuningTime = duration
// }
// if duration > Timeout.Milliseconds() {
// state.TimeoutTimes++
// }
// if rw.statusCode != http.StatusOK {
// state.FailTimes++
// } else {
// state.SuccessTimes++
// }
// RPCStateMgrLock.Unlock()
// })
//}