Browse Source

优化数据统计

fancl 2 years ago
parent
commit
018cfe5200
6 changed files with 17 additions and 19 deletions
  1. 0 7
      gateway/gateway.go
  2. 0 5
      gateway/http/server.go
  3. 0 4
      gateway/rpc/server.go
  4. 17 0
      gateway/state.go
  5. 0 1
      go.mod
  6. 0 2
      go.sum

+ 0 - 7
gateway/gateway.go

@@ -26,13 +26,6 @@ type (
 		l       *Listener
 	}
 
-	State struct {
-		NumOfRequest int32 `json:"num_of_request"` //总请求次数
-		Concurrency  int32 `json:"concurrency"`    //当前并发数
-		InTraffic    int64 `json:"in_traffic"`     //读取流量
-		OutTraffic   int64 `json:"out_traffic"`    //写入流量
-	}
-
 	Gateway struct {
 		listeners []*listener
 		l         net.Listener

+ 0 - 5
gateway/http/server.go

@@ -3,12 +3,10 @@ package http
 import (
 	"context"
 	"git.nspix.com/golang/micro/gateway/http/router"
-	"git.nspix.com/golang/micro/gateway/state"
 	"golang.org/x/net/websocket"
 	"net"
 	"net/http"
 	"sync"
-	"sync/atomic"
 )
 
 var (
@@ -33,7 +31,6 @@ type Server struct {
 	middleware     []Middleware
 	keepAlive      bool
 	isSetKeepAlive bool
-	state          *state.State
 	hookHandle     http.Handler
 	router         *router.Router
 }
@@ -118,7 +115,6 @@ func (r *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
 		r.hookHandle.ServeHTTP(res, req)
 		return
 	}
-	atomic.AddInt64(&r.state.Counter.Request, 1)
 	if req.Method == http.MethodOptions {
 		res.Header().Add("Vary", "Origin")
 		res.Header().Add("Vary", "Access-Control-Request-Method")
@@ -184,7 +180,6 @@ func (r *Server) Shutdown(ctx context.Context) (err error) {
 func New(ctx context.Context) *Server {
 	return &Server{
 		ctx:    ctx,
-		state:  &state.State{},
 		router: router.New(),
 	}
 }

+ 0 - 4
gateway/rpc/server.go

@@ -2,7 +2,6 @@ package rpc
 
 import (
 	"context"
-	"git.nspix.com/golang/micro/gateway/state"
 	"net"
 	"net/http"
 	"sync"
@@ -18,7 +17,6 @@ type Server struct {
 	listener   net.Listener
 	ch         chan *Request
 	ctxPool    sync.Pool
-	state      *state.State
 	serviceMap sync.Map // map[string]HandleFunc
 	sessions   sync.Map
 	exitFlag   int32
@@ -58,7 +56,6 @@ func (svr *Server) handleRequest(req *Request) {
 		val interface{}
 		ctx *Context
 	)
-	atomic.AddInt64(&svr.state.Counter.Request, 1)
 	if val, ok = svr.serviceMap.Load(req.Method); ok {
 		cb = val.(HandleFunc)
 		ctx = svr.getContext()
@@ -170,7 +167,6 @@ func (svr *Server) Close() (err error) {
 func New(ctx context.Context) *Server {
 	return &Server{
 		ctx:      ctx,
-		state:    &state.State{},
 		ch:       make(chan *Request, 10),
 		exitChan: make(chan struct{}),
 	}

+ 17 - 0
gateway/state.go

@@ -0,0 +1,17 @@
+package gateway
+
+type (
+	State struct {
+		NumOfRequest int32 `json:"num_of_request"` //总请求次数
+		Concurrency  int32 `json:"concurrency"`    //当前并发数
+		InTraffic    int64 `json:"in_traffic"`     //读取流量
+		OutTraffic   int64 `json:"out_traffic"`    //写入流量
+	}
+)
+
+func (s *State) Reset() {
+	s.OutTraffic = 0
+	s.InTraffic = 0
+	s.NumOfRequest = 0
+	s.Concurrency = 0
+}

+ 0 - 1
go.mod

@@ -4,7 +4,6 @@ go 1.15
 
 require (
 	github.com/google/btree v1.0.1
-	github.com/judwhite/go-svc v1.2.1 // indirect
 	github.com/peterh/liner v1.2.1
 	github.com/prometheus/client_golang v1.11.0
 	golang.org/x/net v0.0.0-20210226172049-e18ecbb05110

+ 0 - 2
go.sum

@@ -44,8 +44,6 @@ github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX
 github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
 github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
 github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
-github.com/judwhite/go-svc v1.2.1 h1:a7fsJzYUa33sfDJRF2N/WXhA+LonCEEY8BJb1tuS5tA=
-github.com/judwhite/go-svc v1.2.1/go.mod h1:mo/P2JNX8C07ywpP9YtO2gnBgnUiFTHqtsZekJrUuTk=
 github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
 github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
 github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=