Browse Source

move size stats writer into vio

Darien Raymond 7 years ago
parent
commit
dcae6c63dd
3 changed files with 53 additions and 8 deletions
  1. 6 7
      app/dispatcher/default.go
  2. 1 1
      common/vio/stats.go
  3. 46 0
      common/vio/stats_test.go

+ 6 - 7
app/dispatcher/default.go

@@ -15,12 +15,11 @@ import (
 	"v2ray.com/core/common/net"
 	"v2ray.com/core/common/net"
 	"v2ray.com/core/common/protocol"
 	"v2ray.com/core/common/protocol"
 	"v2ray.com/core/common/session"
 	"v2ray.com/core/common/session"
-	"v2ray.com/core/common/stats"
 	"v2ray.com/core/common/vio"
 	"v2ray.com/core/common/vio"
 	"v2ray.com/core/features/outbound"
 	"v2ray.com/core/features/outbound"
 	"v2ray.com/core/features/policy"
 	"v2ray.com/core/features/policy"
 	"v2ray.com/core/features/routing"
 	"v2ray.com/core/features/routing"
-	feature_stats "v2ray.com/core/features/stats"
+	"v2ray.com/core/features/stats"
 	"v2ray.com/core/transport/pipe"
 	"v2ray.com/core/transport/pipe"
 )
 )
 
 
@@ -87,7 +86,7 @@ type DefaultDispatcher struct {
 	ohm    outbound.Manager
 	ohm    outbound.Manager
 	router routing.Router
 	router routing.Router
 	policy policy.Manager
 	policy policy.Manager
-	stats  feature_stats.Manager
+	stats  stats.Manager
 }
 }
 
 
 // NewDefaultDispatcher create a new DefaultDispatcher.
 // NewDefaultDispatcher create a new DefaultDispatcher.
@@ -144,8 +143,8 @@ func (d *DefaultDispatcher) getLink(ctx context.Context) (*vio.Link, *vio.Link)
 		p := d.policy.ForLevel(user.Level)
 		p := d.policy.ForLevel(user.Level)
 		if p.Stats.UserUplink {
 		if p.Stats.UserUplink {
 			name := "user>>>" + user.Email + ">>>traffic>>>uplink"
 			name := "user>>>" + user.Email + ">>>traffic>>>uplink"
-			if c, _ := feature_stats.GetOrRegisterCounter(d.stats, name); c != nil {
-				inboundLink.Writer = &stats.SizeStatWriter{
+			if c, _ := stats.GetOrRegisterCounter(d.stats, name); c != nil {
+				inboundLink.Writer = &vio.SizeStatWriter{
 					Counter: c,
 					Counter: c,
 					Writer:  inboundLink.Writer,
 					Writer:  inboundLink.Writer,
 				}
 				}
@@ -153,8 +152,8 @@ func (d *DefaultDispatcher) getLink(ctx context.Context) (*vio.Link, *vio.Link)
 		}
 		}
 		if p.Stats.UserDownlink {
 		if p.Stats.UserDownlink {
 			name := "user>>>" + user.Email + ">>>traffic>>>downlink"
 			name := "user>>>" + user.Email + ">>>traffic>>>downlink"
-			if c, _ := feature_stats.GetOrRegisterCounter(d.stats, name); c != nil {
-				outboundLink.Writer = &stats.SizeStatWriter{
+			if c, _ := stats.GetOrRegisterCounter(d.stats, name); c != nil {
+				outboundLink.Writer = &vio.SizeStatWriter{
 					Counter: c,
 					Counter: c,
 					Writer:  outboundLink.Writer,
 					Writer:  outboundLink.Writer,
 				}
 				}

+ 1 - 1
common/stats/io.go → common/vio/stats.go

@@ -1,4 +1,4 @@
-package stats
+package vio
 
 
 import (
 import (
 	"v2ray.com/core/common"
 	"v2ray.com/core/common"

+ 46 - 0
common/vio/stats_test.go

@@ -0,0 +1,46 @@
+package vio_test
+
+import (
+	"testing"
+
+	"v2ray.com/core/common"
+	"v2ray.com/core/common/buf"
+	. "v2ray.com/core/common/vio"
+)
+
+type TestCounter int64
+
+func (c *TestCounter) Value() int64 {
+	return int64(*c)
+}
+
+func (c *TestCounter) Add(v int64) int64 {
+	x := int64(*c) + v
+	*c = TestCounter(x)
+	return x
+}
+
+func (c *TestCounter) Set(v int64) int64 {
+	*c = TestCounter(v)
+	return v
+}
+
+func TestStatsWriter(t *testing.T) {
+	var c TestCounter
+	writer := &SizeStatWriter{
+		Counter: &c,
+		Writer:  buf.Discard,
+	}
+
+	var mb buf.MultiBuffer
+	common.Must2(mb.Write([]byte("abcd")))
+	common.Must(writer.WriteMultiBuffer(mb))
+
+	mb.Release()
+	common.Must2(mb.Write([]byte("efg")))
+	common.Must(writer.WriteMultiBuffer(mb))
+
+	if c.Value() != 7 {
+		t.Fatal("unexpected counter value. want 7, but got ", c.Value())
+	}
+}