mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 09:18:34 +00:00
v1.0.0
This commit is contained in:
parent
47d23e9972
commit
c7f7c08ead
711 changed files with 82154 additions and 2 deletions
174
app/stats/channel.go
Normal file
174
app/stats/channel.go
Normal file
|
@ -0,0 +1,174 @@
|
|||
// +build !confonly
|
||||
|
||||
package stats
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
)
|
||||
|
||||
// Channel is an implementation of stats.Channel.
|
||||
type Channel struct {
|
||||
channel chan channelMessage
|
||||
subscribers []chan interface{}
|
||||
|
||||
// Synchronization components
|
||||
access sync.RWMutex
|
||||
closed chan struct{}
|
||||
|
||||
// Channel options
|
||||
blocking bool // Set blocking state if channel buffer reaches limit
|
||||
bufferSize int // Set to 0 as no buffering
|
||||
subsLimit int // Set to 0 as no subscriber limit
|
||||
}
|
||||
|
||||
// NewChannel creates an instance of Statistics Channel.
|
||||
func NewChannel(config *ChannelConfig) *Channel {
|
||||
return &Channel{
|
||||
channel: make(chan channelMessage, config.BufferSize),
|
||||
subsLimit: int(config.SubscriberLimit),
|
||||
bufferSize: int(config.BufferSize),
|
||||
blocking: config.Blocking,
|
||||
}
|
||||
}
|
||||
|
||||
// Subscribers implements stats.Channel.
|
||||
func (c *Channel) Subscribers() []chan interface{} {
|
||||
c.access.RLock()
|
||||
defer c.access.RUnlock()
|
||||
return c.subscribers
|
||||
}
|
||||
|
||||
// Subscribe implements stats.Channel.
|
||||
func (c *Channel) Subscribe() (chan interface{}, error) {
|
||||
c.access.Lock()
|
||||
defer c.access.Unlock()
|
||||
if c.subsLimit > 0 && len(c.subscribers) >= c.subsLimit {
|
||||
return nil, newError("Number of subscribers has reached limit")
|
||||
}
|
||||
subscriber := make(chan interface{}, c.bufferSize)
|
||||
c.subscribers = append(c.subscribers, subscriber)
|
||||
return subscriber, nil
|
||||
}
|
||||
|
||||
// Unsubscribe implements stats.Channel.
|
||||
func (c *Channel) Unsubscribe(subscriber chan interface{}) error {
|
||||
c.access.Lock()
|
||||
defer c.access.Unlock()
|
||||
for i, s := range c.subscribers {
|
||||
if s == subscriber {
|
||||
// Copy to new memory block to prevent modifying original data
|
||||
subscribers := make([]chan interface{}, len(c.subscribers)-1)
|
||||
copy(subscribers[:i], c.subscribers[:i])
|
||||
copy(subscribers[i:], c.subscribers[i+1:])
|
||||
c.subscribers = subscribers
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Publish implements stats.Channel.
|
||||
func (c *Channel) Publish(ctx context.Context, msg interface{}) {
|
||||
select { // Early exit if channel closed
|
||||
case <-c.closed:
|
||||
return
|
||||
default:
|
||||
pub := channelMessage{context: ctx, message: msg}
|
||||
if c.blocking {
|
||||
pub.publish(c.channel)
|
||||
} else {
|
||||
pub.publishNonBlocking(c.channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Running returns whether the channel is running.
|
||||
func (c *Channel) Running() bool {
|
||||
select {
|
||||
case <-c.closed: // Channel closed
|
||||
default: // Channel running or not initialized
|
||||
if c.closed != nil { // Channel initialized
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Start implements common.Runnable.
|
||||
func (c *Channel) Start() error {
|
||||
c.access.Lock()
|
||||
defer c.access.Unlock()
|
||||
if !c.Running() {
|
||||
c.closed = make(chan struct{}) // Reset close signal
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case pub := <-c.channel: // Published message received
|
||||
for _, sub := range c.Subscribers() { // Concurrency-safe subscribers retrievement
|
||||
if c.blocking {
|
||||
pub.broadcast(sub)
|
||||
} else {
|
||||
pub.broadcastNonBlocking(sub)
|
||||
}
|
||||
}
|
||||
case <-c.closed: // Channel closed
|
||||
for _, sub := range c.Subscribers() { // Remove all subscribers
|
||||
common.Must(c.Unsubscribe(sub))
|
||||
close(sub)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close implements common.Closable.
|
||||
func (c *Channel) Close() error {
|
||||
c.access.Lock()
|
||||
defer c.access.Unlock()
|
||||
if c.Running() {
|
||||
close(c.closed) // Send closed signal
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// channelMessage is the published message with guaranteed delivery.
|
||||
// message is discarded only when the context is early cancelled.
|
||||
type channelMessage struct {
|
||||
context context.Context
|
||||
message interface{}
|
||||
}
|
||||
|
||||
func (c channelMessage) publish(publisher chan channelMessage) {
|
||||
select {
|
||||
case publisher <- c:
|
||||
case <-c.context.Done():
|
||||
}
|
||||
}
|
||||
|
||||
func (c channelMessage) publishNonBlocking(publisher chan channelMessage) {
|
||||
select {
|
||||
case publisher <- c:
|
||||
default: // Create another goroutine to keep sending message
|
||||
go c.publish(publisher)
|
||||
}
|
||||
}
|
||||
|
||||
func (c channelMessage) broadcast(subscriber chan interface{}) {
|
||||
select {
|
||||
case subscriber <- c.message:
|
||||
case <-c.context.Done():
|
||||
}
|
||||
}
|
||||
|
||||
func (c channelMessage) broadcastNonBlocking(subscriber chan interface{}) {
|
||||
select {
|
||||
case subscriber <- c.message:
|
||||
default: // Create another goroutine to keep sending message
|
||||
go c.broadcast(subscriber)
|
||||
}
|
||||
}
|
405
app/stats/channel_test.go
Normal file
405
app/stats/channel_test.go
Normal file
|
@ -0,0 +1,405 @@
|
|||
package stats_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/xtls/xray-core/v1/app/stats"
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
"github.com/xtls/xray-core/v1/features/stats"
|
||||
)
|
||||
|
||||
func TestStatsChannel(t *testing.T) {
|
||||
// At most 2 subscribers could be registered
|
||||
c := NewChannel(&ChannelConfig{SubscriberLimit: 2, Blocking: true})
|
||||
|
||||
a, err := stats.SubscribeRunnableChannel(c)
|
||||
common.Must(err)
|
||||
if !c.Running() {
|
||||
t.Fatal("unexpected failure in running channel after first subscription")
|
||||
}
|
||||
|
||||
b, err := c.Subscribe()
|
||||
common.Must(err)
|
||||
|
||||
// Test that third subscriber is forbidden
|
||||
_, err = c.Subscribe()
|
||||
if err == nil {
|
||||
t.Fatal("unexpected successful subscription")
|
||||
}
|
||||
t.Log("expected error: ", err)
|
||||
|
||||
stopCh := make(chan struct{})
|
||||
errCh := make(chan string)
|
||||
|
||||
go func() {
|
||||
c.Publish(context.Background(), 1)
|
||||
c.Publish(context.Background(), 2)
|
||||
c.Publish(context.Background(), "3")
|
||||
c.Publish(context.Background(), []int{4})
|
||||
stopCh <- struct{}{}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
if v, ok := (<-a).(int); !ok || v != 1 {
|
||||
errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 1)
|
||||
}
|
||||
if v, ok := (<-a).(int); !ok || v != 2 {
|
||||
errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 2)
|
||||
}
|
||||
if v, ok := (<-a).(string); !ok || v != "3" {
|
||||
errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", "3")
|
||||
}
|
||||
if v, ok := (<-a).([]int); !ok || v[0] != 4 {
|
||||
errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", []int{4})
|
||||
}
|
||||
stopCh <- struct{}{}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
if v, ok := (<-b).(int); !ok || v != 1 {
|
||||
errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 1)
|
||||
}
|
||||
if v, ok := (<-b).(int); !ok || v != 2 {
|
||||
errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 2)
|
||||
}
|
||||
if v, ok := (<-b).(string); !ok || v != "3" {
|
||||
errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", "3")
|
||||
}
|
||||
if v, ok := (<-b).([]int); !ok || v[0] != 4 {
|
||||
errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", []int{4})
|
||||
}
|
||||
stopCh <- struct{}{}
|
||||
}()
|
||||
|
||||
timeout := time.After(2 * time.Second)
|
||||
for i := 0; i < 3; i++ {
|
||||
select {
|
||||
case <-timeout:
|
||||
t.Fatal("Test timeout after 2s")
|
||||
case e := <-errCh:
|
||||
t.Fatal(e)
|
||||
case <-stopCh:
|
||||
}
|
||||
}
|
||||
|
||||
// Test the unsubscription of channel
|
||||
common.Must(c.Unsubscribe(b))
|
||||
|
||||
// Test the last subscriber will close channel with `UnsubscribeClosableChannel`
|
||||
common.Must(stats.UnsubscribeClosableChannel(c, a))
|
||||
if c.Running() {
|
||||
t.Fatal("unexpected running channel after unsubscribing the last subscriber")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatsChannelUnsubcribe(t *testing.T) {
|
||||
c := NewChannel(&ChannelConfig{Blocking: true})
|
||||
common.Must(c.Start())
|
||||
defer c.Close()
|
||||
|
||||
a, err := c.Subscribe()
|
||||
common.Must(err)
|
||||
defer c.Unsubscribe(a)
|
||||
|
||||
b, err := c.Subscribe()
|
||||
common.Must(err)
|
||||
|
||||
pauseCh := make(chan struct{})
|
||||
stopCh := make(chan struct{})
|
||||
errCh := make(chan string)
|
||||
|
||||
{
|
||||
var aSet, bSet bool
|
||||
for _, s := range c.Subscribers() {
|
||||
if s == a {
|
||||
aSet = true
|
||||
}
|
||||
if s == b {
|
||||
bSet = true
|
||||
}
|
||||
}
|
||||
if !(aSet && bSet) {
|
||||
t.Fatal("unexpected subscribers: ", c.Subscribers())
|
||||
}
|
||||
}
|
||||
|
||||
go func() { // Blocking publish
|
||||
c.Publish(context.Background(), 1)
|
||||
<-pauseCh // Wait for `b` goroutine to resume sending message
|
||||
c.Publish(context.Background(), 2)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
if v, ok := (<-a).(int); !ok || v != 1 {
|
||||
errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 1)
|
||||
}
|
||||
if v, ok := (<-a).(int); !ok || v != 2 {
|
||||
errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 2)
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
if v, ok := (<-b).(int); !ok || v != 1 {
|
||||
errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 1)
|
||||
}
|
||||
// Unsubscribe `b` while publishing is paused
|
||||
c.Unsubscribe(b)
|
||||
{ // Test `b` is not in subscribers
|
||||
var aSet, bSet bool
|
||||
for _, s := range c.Subscribers() {
|
||||
if s == a {
|
||||
aSet = true
|
||||
}
|
||||
if s == b {
|
||||
bSet = true
|
||||
}
|
||||
}
|
||||
if !(aSet && !bSet) {
|
||||
errCh <- fmt.Sprint("unexpected subscribers: ", c.Subscribers())
|
||||
}
|
||||
}
|
||||
// Resume publishing progress
|
||||
close(pauseCh)
|
||||
// Test `b` is neither closed nor able to receive any data
|
||||
select {
|
||||
case v, ok := <-b:
|
||||
if ok {
|
||||
errCh <- fmt.Sprint("unexpected data received: ", v)
|
||||
} else {
|
||||
errCh <- fmt.Sprint("unexpected closed channel: ", b)
|
||||
}
|
||||
default:
|
||||
}
|
||||
close(stopCh)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("Test timeout after 2s")
|
||||
case e := <-errCh:
|
||||
t.Fatal(e)
|
||||
case <-stopCh:
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatsChannelBlocking(t *testing.T) {
|
||||
// Do not use buffer so as to create blocking scenario
|
||||
c := NewChannel(&ChannelConfig{BufferSize: 0, Blocking: true})
|
||||
common.Must(c.Start())
|
||||
defer c.Close()
|
||||
|
||||
a, err := c.Subscribe()
|
||||
common.Must(err)
|
||||
defer c.Unsubscribe(a)
|
||||
|
||||
pauseCh := make(chan struct{})
|
||||
stopCh := make(chan struct{})
|
||||
errCh := make(chan string)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
// Test blocking channel publishing
|
||||
go func() {
|
||||
// Dummy messsage with no subscriber receiving, will block broadcasting goroutine
|
||||
c.Publish(context.Background(), nil)
|
||||
|
||||
<-pauseCh
|
||||
|
||||
// Publishing should be blocked here, for last message was not cleared and buffer was full
|
||||
c.Publish(context.Background(), nil)
|
||||
|
||||
pauseCh <- struct{}{}
|
||||
|
||||
// Publishing should still be blocked here
|
||||
c.Publish(ctx, nil)
|
||||
|
||||
// Check publishing is done because context is canceled
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if ctx.Err() != context.Canceled {
|
||||
errCh <- fmt.Sprint("unexpected error: ", ctx.Err())
|
||||
}
|
||||
default:
|
||||
errCh <- "unexpected non-blocked publishing"
|
||||
}
|
||||
close(stopCh)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
pauseCh <- struct{}{}
|
||||
|
||||
select {
|
||||
case <-pauseCh:
|
||||
errCh <- "unexpected non-blocked publishing"
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
}
|
||||
|
||||
// Receive first published message
|
||||
<-a
|
||||
|
||||
select {
|
||||
case <-pauseCh:
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
errCh <- "unexpected blocking publishing"
|
||||
}
|
||||
|
||||
// Manually cancel the context to end publishing
|
||||
cancel()
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("Test timeout after 2s")
|
||||
case e := <-errCh:
|
||||
t.Fatal(e)
|
||||
case <-stopCh:
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatsChannelNonBlocking(t *testing.T) {
|
||||
// Do not use buffer so as to create blocking scenario
|
||||
c := NewChannel(&ChannelConfig{BufferSize: 0, Blocking: false})
|
||||
common.Must(c.Start())
|
||||
defer c.Close()
|
||||
|
||||
a, err := c.Subscribe()
|
||||
common.Must(err)
|
||||
defer c.Unsubscribe(a)
|
||||
|
||||
pauseCh := make(chan struct{})
|
||||
stopCh := make(chan struct{})
|
||||
errCh := make(chan string)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
// Test blocking channel publishing
|
||||
go func() {
|
||||
c.Publish(context.Background(), nil)
|
||||
c.Publish(context.Background(), nil)
|
||||
pauseCh <- struct{}{}
|
||||
<-pauseCh
|
||||
c.Publish(ctx, nil)
|
||||
c.Publish(ctx, nil)
|
||||
// Check publishing is done because context is canceled
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if ctx.Err() != context.Canceled {
|
||||
errCh <- fmt.Sprint("unexpected error: ", ctx.Err())
|
||||
}
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
errCh <- "unexpected non-cancelled publishing"
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
// Check publishing won't block even if there is no subscriber receiving message
|
||||
select {
|
||||
case <-pauseCh:
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
errCh <- "unexpected blocking publishing"
|
||||
}
|
||||
|
||||
// Receive first and second published message
|
||||
<-a
|
||||
<-a
|
||||
|
||||
pauseCh <- struct{}{}
|
||||
|
||||
// Manually cancel the context to end publishing
|
||||
cancel()
|
||||
|
||||
// Check third and forth published message is cancelled and cannot receive
|
||||
<-time.After(100 * time.Millisecond)
|
||||
select {
|
||||
case <-a:
|
||||
errCh <- "unexpected non-cancelled publishing"
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case <-a:
|
||||
errCh <- "unexpected non-cancelled publishing"
|
||||
default:
|
||||
}
|
||||
close(stopCh)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("Test timeout after 2s")
|
||||
case e := <-errCh:
|
||||
t.Fatal(e)
|
||||
case <-stopCh:
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatsChannelConcurrency(t *testing.T) {
|
||||
// Do not use buffer so as to create blocking scenario
|
||||
c := NewChannel(&ChannelConfig{BufferSize: 0, Blocking: true})
|
||||
common.Must(c.Start())
|
||||
defer c.Close()
|
||||
|
||||
a, err := c.Subscribe()
|
||||
common.Must(err)
|
||||
defer c.Unsubscribe(a)
|
||||
|
||||
b, err := c.Subscribe()
|
||||
common.Must(err)
|
||||
defer c.Unsubscribe(b)
|
||||
|
||||
stopCh := make(chan struct{})
|
||||
errCh := make(chan string)
|
||||
|
||||
go func() { // Blocking publish
|
||||
c.Publish(context.Background(), 1)
|
||||
c.Publish(context.Background(), 2)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
if v, ok := (<-a).(int); !ok || v != 1 {
|
||||
errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 1)
|
||||
}
|
||||
if v, ok := (<-a).(int); !ok || v != 2 {
|
||||
errCh <- fmt.Sprint("unexpected receiving: ", v, ", wanted ", 2)
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
// Block `b` for a time so as to ensure source channel is trying to send message to `b`.
|
||||
<-time.After(25 * time.Millisecond)
|
||||
// This causes concurrency scenario: unsubscribe `b` while trying to send message to it
|
||||
c.Unsubscribe(b)
|
||||
// Test `b` is not closed and can still receive data 1:
|
||||
// Because unsubscribe won't affect the ongoing process of sending message.
|
||||
select {
|
||||
case v, ok := <-b:
|
||||
if v1, ok1 := v.(int); !(ok && ok1 && v1 == 1) {
|
||||
errCh <- fmt.Sprint("unexpected failure in receiving data: ", 1)
|
||||
}
|
||||
default:
|
||||
errCh <- fmt.Sprint("unexpected block from receiving data: ", 1)
|
||||
}
|
||||
// Test `b` is not closed but cannot receive data 2:
|
||||
// Because in a new round of messaging, `b` has been unsubscribed.
|
||||
select {
|
||||
case v, ok := <-b:
|
||||
if ok {
|
||||
errCh <- fmt.Sprint("unexpected receiving: ", v)
|
||||
} else {
|
||||
errCh <- "unexpected closing of channel"
|
||||
}
|
||||
default:
|
||||
}
|
||||
close(stopCh)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("Test timeout after 2s")
|
||||
case e := <-errCh:
|
||||
t.Fatal(e)
|
||||
case <-stopCh:
|
||||
}
|
||||
}
|
127
app/stats/command/command.go
Normal file
127
app/stats/command/command.go
Normal file
|
@ -0,0 +1,127 @@
|
|||
// +build !confonly
|
||||
|
||||
package command
|
||||
|
||||
//go:generate go run github.com/xtls/xray-core/v1/common/errors/errorgen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
grpc "google.golang.org/grpc"
|
||||
|
||||
"github.com/xtls/xray-core/v1/app/stats"
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
"github.com/xtls/xray-core/v1/common/strmatcher"
|
||||
"github.com/xtls/xray-core/v1/core"
|
||||
feature_stats "github.com/xtls/xray-core/v1/features/stats"
|
||||
)
|
||||
|
||||
// statsServer is an implementation of StatsService.
|
||||
type statsServer struct {
|
||||
stats feature_stats.Manager
|
||||
startTime time.Time
|
||||
}
|
||||
|
||||
func NewStatsServer(manager feature_stats.Manager) StatsServiceServer {
|
||||
return &statsServer{
|
||||
stats: manager,
|
||||
startTime: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *statsServer) GetStats(ctx context.Context, request *GetStatsRequest) (*GetStatsResponse, error) {
|
||||
c := s.stats.GetCounter(request.Name)
|
||||
if c == nil {
|
||||
return nil, newError(request.Name, " not found.")
|
||||
}
|
||||
var value int64
|
||||
if request.Reset_ {
|
||||
value = c.Set(0)
|
||||
} else {
|
||||
value = c.Value()
|
||||
}
|
||||
return &GetStatsResponse{
|
||||
Stat: &Stat{
|
||||
Name: request.Name,
|
||||
Value: value,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *statsServer) QueryStats(ctx context.Context, request *QueryStatsRequest) (*QueryStatsResponse, error) {
|
||||
matcher, err := strmatcher.Substr.New(request.Pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response := &QueryStatsResponse{}
|
||||
|
||||
manager, ok := s.stats.(*stats.Manager)
|
||||
if !ok {
|
||||
return nil, newError("QueryStats only works its own stats.Manager.")
|
||||
}
|
||||
|
||||
manager.VisitCounters(func(name string, c feature_stats.Counter) bool {
|
||||
if matcher.Match(name) {
|
||||
var value int64
|
||||
if request.Reset_ {
|
||||
value = c.Set(0)
|
||||
} else {
|
||||
value = c.Value()
|
||||
}
|
||||
response.Stat = append(response.Stat, &Stat{
|
||||
Name: name,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (s *statsServer) GetSysStats(ctx context.Context, request *SysStatsRequest) (*SysStatsResponse, error) {
|
||||
var rtm runtime.MemStats
|
||||
runtime.ReadMemStats(&rtm)
|
||||
|
||||
uptime := time.Since(s.startTime)
|
||||
|
||||
response := &SysStatsResponse{
|
||||
Uptime: uint32(uptime.Seconds()),
|
||||
NumGoroutine: uint32(runtime.NumGoroutine()),
|
||||
Alloc: rtm.Alloc,
|
||||
TotalAlloc: rtm.TotalAlloc,
|
||||
Sys: rtm.Sys,
|
||||
Mallocs: rtm.Mallocs,
|
||||
Frees: rtm.Frees,
|
||||
LiveObjects: rtm.Mallocs - rtm.Frees,
|
||||
NumGC: rtm.NumGC,
|
||||
PauseTotalNs: rtm.PauseTotalNs,
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (s *statsServer) mustEmbedUnimplementedStatsServiceServer() {}
|
||||
|
||||
type service struct {
|
||||
statsManager feature_stats.Manager
|
||||
}
|
||||
|
||||
func (s *service) Register(server *grpc.Server) {
|
||||
RegisterStatsServiceServer(server, NewStatsServer(s.statsManager))
|
||||
}
|
||||
|
||||
func init() {
|
||||
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
|
||||
s := new(service)
|
||||
|
||||
core.RequireFeatures(ctx, func(sm feature_stats.Manager) {
|
||||
s.statsManager = sm
|
||||
})
|
||||
|
||||
return s, nil
|
||||
}))
|
||||
}
|
720
app/stats/command/command.pb.go
Normal file
720
app/stats/command/command.pb.go
Normal file
|
@ -0,0 +1,720 @@
|
|||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.25.0
|
||||
// protoc v3.14.0
|
||||
// source: app/stats/command/command.proto
|
||||
|
||||
package command
|
||||
|
||||
import (
|
||||
proto "github.com/golang/protobuf/proto"
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
// This is a compile-time assertion that a sufficiently up-to-date version
|
||||
// of the legacy proto package is being used.
|
||||
const _ = proto.ProtoPackageIsVersion4
|
||||
|
||||
type GetStatsRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
// Name of the stat counter.
|
||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||
// Whether or not to reset the counter to fetching its value.
|
||||
Reset_ bool `protobuf:"varint,2,opt,name=reset,proto3" json:"reset,omitempty"`
|
||||
}
|
||||
|
||||
func (x *GetStatsRequest) Reset() {
|
||||
*x = GetStatsRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *GetStatsRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetStatsRequest) ProtoMessage() {}
|
||||
|
||||
func (x *GetStatsRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use GetStatsRequest.ProtoReflect.Descriptor instead.
|
||||
func (*GetStatsRequest) Descriptor() ([]byte, []int) {
|
||||
return file_app_stats_command_command_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *GetStatsRequest) GetName() string {
|
||||
if x != nil {
|
||||
return x.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *GetStatsRequest) GetReset_() bool {
|
||||
if x != nil {
|
||||
return x.Reset_
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type Stat struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||
Value int64 `protobuf:"varint,2,opt,name=value,proto3" json:"value,omitempty"`
|
||||
}
|
||||
|
||||
func (x *Stat) Reset() {
|
||||
*x = Stat{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *Stat) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Stat) ProtoMessage() {}
|
||||
|
||||
func (x *Stat) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[1]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use Stat.ProtoReflect.Descriptor instead.
|
||||
func (*Stat) Descriptor() ([]byte, []int) {
|
||||
return file_app_stats_command_command_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *Stat) GetName() string {
|
||||
if x != nil {
|
||||
return x.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *Stat) GetValue() int64 {
|
||||
if x != nil {
|
||||
return x.Value
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type GetStatsResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Stat *Stat `protobuf:"bytes,1,opt,name=stat,proto3" json:"stat,omitempty"`
|
||||
}
|
||||
|
||||
func (x *GetStatsResponse) Reset() {
|
||||
*x = GetStatsResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *GetStatsResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetStatsResponse) ProtoMessage() {}
|
||||
|
||||
func (x *GetStatsResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[2]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use GetStatsResponse.ProtoReflect.Descriptor instead.
|
||||
func (*GetStatsResponse) Descriptor() ([]byte, []int) {
|
||||
return file_app_stats_command_command_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *GetStatsResponse) GetStat() *Stat {
|
||||
if x != nil {
|
||||
return x.Stat
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type QueryStatsRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Pattern string `protobuf:"bytes,1,opt,name=pattern,proto3" json:"pattern,omitempty"`
|
||||
Reset_ bool `protobuf:"varint,2,opt,name=reset,proto3" json:"reset,omitempty"`
|
||||
}
|
||||
|
||||
func (x *QueryStatsRequest) Reset() {
|
||||
*x = QueryStatsRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *QueryStatsRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*QueryStatsRequest) ProtoMessage() {}
|
||||
|
||||
func (x *QueryStatsRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[3]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use QueryStatsRequest.ProtoReflect.Descriptor instead.
|
||||
func (*QueryStatsRequest) Descriptor() ([]byte, []int) {
|
||||
return file_app_stats_command_command_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
func (x *QueryStatsRequest) GetPattern() string {
|
||||
if x != nil {
|
||||
return x.Pattern
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *QueryStatsRequest) GetReset_() bool {
|
||||
if x != nil {
|
||||
return x.Reset_
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type QueryStatsResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Stat []*Stat `protobuf:"bytes,1,rep,name=stat,proto3" json:"stat,omitempty"`
|
||||
}
|
||||
|
||||
func (x *QueryStatsResponse) Reset() {
|
||||
*x = QueryStatsResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[4]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *QueryStatsResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*QueryStatsResponse) ProtoMessage() {}
|
||||
|
||||
func (x *QueryStatsResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[4]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use QueryStatsResponse.ProtoReflect.Descriptor instead.
|
||||
func (*QueryStatsResponse) Descriptor() ([]byte, []int) {
|
||||
return file_app_stats_command_command_proto_rawDescGZIP(), []int{4}
|
||||
}
|
||||
|
||||
func (x *QueryStatsResponse) GetStat() []*Stat {
|
||||
if x != nil {
|
||||
return x.Stat
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type SysStatsRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
}
|
||||
|
||||
func (x *SysStatsRequest) Reset() {
|
||||
*x = SysStatsRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[5]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *SysStatsRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*SysStatsRequest) ProtoMessage() {}
|
||||
|
||||
func (x *SysStatsRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[5]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use SysStatsRequest.ProtoReflect.Descriptor instead.
|
||||
func (*SysStatsRequest) Descriptor() ([]byte, []int) {
|
||||
return file_app_stats_command_command_proto_rawDescGZIP(), []int{5}
|
||||
}
|
||||
|
||||
type SysStatsResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
NumGoroutine uint32 `protobuf:"varint,1,opt,name=NumGoroutine,proto3" json:"NumGoroutine,omitempty"`
|
||||
NumGC uint32 `protobuf:"varint,2,opt,name=NumGC,proto3" json:"NumGC,omitempty"`
|
||||
Alloc uint64 `protobuf:"varint,3,opt,name=Alloc,proto3" json:"Alloc,omitempty"`
|
||||
TotalAlloc uint64 `protobuf:"varint,4,opt,name=TotalAlloc,proto3" json:"TotalAlloc,omitempty"`
|
||||
Sys uint64 `protobuf:"varint,5,opt,name=Sys,proto3" json:"Sys,omitempty"`
|
||||
Mallocs uint64 `protobuf:"varint,6,opt,name=Mallocs,proto3" json:"Mallocs,omitempty"`
|
||||
Frees uint64 `protobuf:"varint,7,opt,name=Frees,proto3" json:"Frees,omitempty"`
|
||||
LiveObjects uint64 `protobuf:"varint,8,opt,name=LiveObjects,proto3" json:"LiveObjects,omitempty"`
|
||||
PauseTotalNs uint64 `protobuf:"varint,9,opt,name=PauseTotalNs,proto3" json:"PauseTotalNs,omitempty"`
|
||||
Uptime uint32 `protobuf:"varint,10,opt,name=Uptime,proto3" json:"Uptime,omitempty"`
|
||||
}
|
||||
|
||||
func (x *SysStatsResponse) Reset() {
|
||||
*x = SysStatsResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[6]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *SysStatsResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*SysStatsResponse) ProtoMessage() {}
|
||||
|
||||
func (x *SysStatsResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[6]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use SysStatsResponse.ProtoReflect.Descriptor instead.
|
||||
func (*SysStatsResponse) Descriptor() ([]byte, []int) {
|
||||
return file_app_stats_command_command_proto_rawDescGZIP(), []int{6}
|
||||
}
|
||||
|
||||
func (x *SysStatsResponse) GetNumGoroutine() uint32 {
|
||||
if x != nil {
|
||||
return x.NumGoroutine
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *SysStatsResponse) GetNumGC() uint32 {
|
||||
if x != nil {
|
||||
return x.NumGC
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *SysStatsResponse) GetAlloc() uint64 {
|
||||
if x != nil {
|
||||
return x.Alloc
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *SysStatsResponse) GetTotalAlloc() uint64 {
|
||||
if x != nil {
|
||||
return x.TotalAlloc
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *SysStatsResponse) GetSys() uint64 {
|
||||
if x != nil {
|
||||
return x.Sys
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *SysStatsResponse) GetMallocs() uint64 {
|
||||
if x != nil {
|
||||
return x.Mallocs
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *SysStatsResponse) GetFrees() uint64 {
|
||||
if x != nil {
|
||||
return x.Frees
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *SysStatsResponse) GetLiveObjects() uint64 {
|
||||
if x != nil {
|
||||
return x.LiveObjects
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *SysStatsResponse) GetPauseTotalNs() uint64 {
|
||||
if x != nil {
|
||||
return x.PauseTotalNs
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *SysStatsResponse) GetUptime() uint32 {
|
||||
if x != nil {
|
||||
return x.Uptime
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
}
|
||||
|
||||
func (x *Config) Reset() {
|
||||
*x = Config{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[7]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *Config) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Config) ProtoMessage() {}
|
||||
|
||||
func (x *Config) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_app_stats_command_command_proto_msgTypes[7]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use Config.ProtoReflect.Descriptor instead.
|
||||
func (*Config) Descriptor() ([]byte, []int) {
|
||||
return file_app_stats_command_command_proto_rawDescGZIP(), []int{7}
|
||||
}
|
||||
|
||||
var File_app_stats_command_command_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_app_stats_command_command_proto_rawDesc = []byte{
|
||||
0x0a, 0x1f, 0x61, 0x70, 0x70, 0x2f, 0x73, 0x74, 0x61, 0x74, 0x73, 0x2f, 0x63, 0x6f, 0x6d, 0x6d,
|
||||
0x61, 0x6e, 0x64, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2e, 0x70, 0x72, 0x6f, 0x74,
|
||||
0x6f, 0x12, 0x16, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x74, 0x61, 0x74,
|
||||
0x73, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x22, 0x3b, 0x0a, 0x0f, 0x47, 0x65, 0x74,
|
||||
0x53, 0x74, 0x61, 0x74, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04,
|
||||
0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65,
|
||||
0x12, 0x14, 0x0a, 0x05, 0x72, 0x65, 0x73, 0x65, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52,
|
||||
0x05, 0x72, 0x65, 0x73, 0x65, 0x74, 0x22, 0x30, 0x0a, 0x04, 0x53, 0x74, 0x61, 0x74, 0x12, 0x12,
|
||||
0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61,
|
||||
0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28,
|
||||
0x03, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x44, 0x0a, 0x10, 0x47, 0x65, 0x74, 0x53,
|
||||
0x74, 0x61, 0x74, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x30, 0x0a, 0x04,
|
||||
0x73, 0x74, 0x61, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x78, 0x72, 0x61,
|
||||
0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x74, 0x61, 0x74, 0x73, 0x2e, 0x63, 0x6f, 0x6d, 0x6d,
|
||||
0x61, 0x6e, 0x64, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x52, 0x04, 0x73, 0x74, 0x61, 0x74, 0x22, 0x43,
|
||||
0x0a, 0x11, 0x51, 0x75, 0x65, 0x72, 0x79, 0x53, 0x74, 0x61, 0x74, 0x73, 0x52, 0x65, 0x71, 0x75,
|
||||
0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x18, 0x01,
|
||||
0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x12, 0x14, 0x0a,
|
||||
0x05, 0x72, 0x65, 0x73, 0x65, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x72, 0x65,
|
||||
0x73, 0x65, 0x74, 0x22, 0x46, 0x0a, 0x12, 0x51, 0x75, 0x65, 0x72, 0x79, 0x53, 0x74, 0x61, 0x74,
|
||||
0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x30, 0x0a, 0x04, 0x73, 0x74, 0x61,
|
||||
0x74, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61,
|
||||
0x70, 0x70, 0x2e, 0x73, 0x74, 0x61, 0x74, 0x73, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64,
|
||||
0x2e, 0x53, 0x74, 0x61, 0x74, 0x52, 0x04, 0x73, 0x74, 0x61, 0x74, 0x22, 0x11, 0x0a, 0x0f, 0x53,
|
||||
0x79, 0x73, 0x53, 0x74, 0x61, 0x74, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0xa2,
|
||||
0x02, 0x0a, 0x10, 0x53, 0x79, 0x73, 0x53, 0x74, 0x61, 0x74, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f,
|
||||
0x6e, 0x73, 0x65, 0x12, 0x22, 0x0a, 0x0c, 0x4e, 0x75, 0x6d, 0x47, 0x6f, 0x72, 0x6f, 0x75, 0x74,
|
||||
0x69, 0x6e, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0c, 0x4e, 0x75, 0x6d, 0x47, 0x6f,
|
||||
0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x4e, 0x75, 0x6d, 0x47, 0x43,
|
||||
0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x05, 0x4e, 0x75, 0x6d, 0x47, 0x43, 0x12, 0x14, 0x0a,
|
||||
0x05, 0x41, 0x6c, 0x6c, 0x6f, 0x63, 0x18, 0x03, 0x20, 0x01, 0x28, 0x04, 0x52, 0x05, 0x41, 0x6c,
|
||||
0x6c, 0x6f, 0x63, 0x12, 0x1e, 0x0a, 0x0a, 0x54, 0x6f, 0x74, 0x61, 0x6c, 0x41, 0x6c, 0x6c, 0x6f,
|
||||
0x63, 0x18, 0x04, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0a, 0x54, 0x6f, 0x74, 0x61, 0x6c, 0x41, 0x6c,
|
||||
0x6c, 0x6f, 0x63, 0x12, 0x10, 0x0a, 0x03, 0x53, 0x79, 0x73, 0x18, 0x05, 0x20, 0x01, 0x28, 0x04,
|
||||
0x52, 0x03, 0x53, 0x79, 0x73, 0x12, 0x18, 0x0a, 0x07, 0x4d, 0x61, 0x6c, 0x6c, 0x6f, 0x63, 0x73,
|
||||
0x18, 0x06, 0x20, 0x01, 0x28, 0x04, 0x52, 0x07, 0x4d, 0x61, 0x6c, 0x6c, 0x6f, 0x63, 0x73, 0x12,
|
||||
0x14, 0x0a, 0x05, 0x46, 0x72, 0x65, 0x65, 0x73, 0x18, 0x07, 0x20, 0x01, 0x28, 0x04, 0x52, 0x05,
|
||||
0x46, 0x72, 0x65, 0x65, 0x73, 0x12, 0x20, 0x0a, 0x0b, 0x4c, 0x69, 0x76, 0x65, 0x4f, 0x62, 0x6a,
|
||||
0x65, 0x63, 0x74, 0x73, 0x18, 0x08, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0b, 0x4c, 0x69, 0x76, 0x65,
|
||||
0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x73, 0x12, 0x22, 0x0a, 0x0c, 0x50, 0x61, 0x75, 0x73, 0x65,
|
||||
0x54, 0x6f, 0x74, 0x61, 0x6c, 0x4e, 0x73, 0x18, 0x09, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0c, 0x50,
|
||||
0x61, 0x75, 0x73, 0x65, 0x54, 0x6f, 0x74, 0x61, 0x6c, 0x4e, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x55,
|
||||
0x70, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x55, 0x70, 0x74,
|
||||
0x69, 0x6d, 0x65, 0x22, 0x08, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x32, 0xba, 0x02,
|
||||
0x0a, 0x0c, 0x53, 0x74, 0x61, 0x74, 0x73, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x5f,
|
||||
0x0a, 0x08, 0x47, 0x65, 0x74, 0x53, 0x74, 0x61, 0x74, 0x73, 0x12, 0x27, 0x2e, 0x78, 0x72, 0x61,
|
||||
0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x74, 0x61, 0x74, 0x73, 0x2e, 0x63, 0x6f, 0x6d, 0x6d,
|
||||
0x61, 0x6e, 0x64, 0x2e, 0x47, 0x65, 0x74, 0x53, 0x74, 0x61, 0x74, 0x73, 0x52, 0x65, 0x71, 0x75,
|
||||
0x65, 0x73, 0x74, 0x1a, 0x28, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73,
|
||||
0x74, 0x61, 0x74, 0x73, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2e, 0x47, 0x65, 0x74,
|
||||
0x53, 0x74, 0x61, 0x74, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12,
|
||||
0x65, 0x0a, 0x0a, 0x51, 0x75, 0x65, 0x72, 0x79, 0x53, 0x74, 0x61, 0x74, 0x73, 0x12, 0x29, 0x2e,
|
||||
0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x74, 0x61, 0x74, 0x73, 0x2e, 0x63,
|
||||
0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x53, 0x74, 0x61, 0x74,
|
||||
0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x2a, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e,
|
||||
0x61, 0x70, 0x70, 0x2e, 0x73, 0x74, 0x61, 0x74, 0x73, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e,
|
||||
0x64, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x53, 0x74, 0x61, 0x74, 0x73, 0x52, 0x65, 0x73, 0x70,
|
||||
0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x62, 0x0a, 0x0b, 0x47, 0x65, 0x74, 0x53, 0x79, 0x73,
|
||||
0x53, 0x74, 0x61, 0x74, 0x73, 0x12, 0x27, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70,
|
||||
0x2e, 0x73, 0x74, 0x61, 0x74, 0x73, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2e, 0x53,
|
||||
0x79, 0x73, 0x53, 0x74, 0x61, 0x74, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x28,
|
||||
0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x74, 0x61, 0x74, 0x73, 0x2e,
|
||||
0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2e, 0x53, 0x79, 0x73, 0x53, 0x74, 0x61, 0x74, 0x73,
|
||||
0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x67, 0x0a, 0x1a, 0x63, 0x6f,
|
||||
0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x74, 0x61, 0x74, 0x73,
|
||||
0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x50, 0x01, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68,
|
||||
0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79,
|
||||
0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x76, 0x31, 0x2f, 0x61, 0x70, 0x70, 0x2f, 0x73, 0x74, 0x61,
|
||||
0x74, 0x73, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0xaa, 0x02, 0x16, 0x58, 0x72, 0x61,
|
||||
0x79, 0x2e, 0x41, 0x70, 0x70, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x73, 0x2e, 0x43, 0x6f, 0x6d, 0x6d,
|
||||
0x61, 0x6e, 0x64, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_app_stats_command_command_proto_rawDescOnce sync.Once
|
||||
file_app_stats_command_command_proto_rawDescData = file_app_stats_command_command_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_app_stats_command_command_proto_rawDescGZIP() []byte {
|
||||
file_app_stats_command_command_proto_rawDescOnce.Do(func() {
|
||||
file_app_stats_command_command_proto_rawDescData = protoimpl.X.CompressGZIP(file_app_stats_command_command_proto_rawDescData)
|
||||
})
|
||||
return file_app_stats_command_command_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_app_stats_command_command_proto_msgTypes = make([]protoimpl.MessageInfo, 8)
|
||||
var file_app_stats_command_command_proto_goTypes = []interface{}{
|
||||
(*GetStatsRequest)(nil), // 0: xray.app.stats.command.GetStatsRequest
|
||||
(*Stat)(nil), // 1: xray.app.stats.command.Stat
|
||||
(*GetStatsResponse)(nil), // 2: xray.app.stats.command.GetStatsResponse
|
||||
(*QueryStatsRequest)(nil), // 3: xray.app.stats.command.QueryStatsRequest
|
||||
(*QueryStatsResponse)(nil), // 4: xray.app.stats.command.QueryStatsResponse
|
||||
(*SysStatsRequest)(nil), // 5: xray.app.stats.command.SysStatsRequest
|
||||
(*SysStatsResponse)(nil), // 6: xray.app.stats.command.SysStatsResponse
|
||||
(*Config)(nil), // 7: xray.app.stats.command.Config
|
||||
}
|
||||
var file_app_stats_command_command_proto_depIdxs = []int32{
|
||||
1, // 0: xray.app.stats.command.GetStatsResponse.stat:type_name -> xray.app.stats.command.Stat
|
||||
1, // 1: xray.app.stats.command.QueryStatsResponse.stat:type_name -> xray.app.stats.command.Stat
|
||||
0, // 2: xray.app.stats.command.StatsService.GetStats:input_type -> xray.app.stats.command.GetStatsRequest
|
||||
3, // 3: xray.app.stats.command.StatsService.QueryStats:input_type -> xray.app.stats.command.QueryStatsRequest
|
||||
5, // 4: xray.app.stats.command.StatsService.GetSysStats:input_type -> xray.app.stats.command.SysStatsRequest
|
||||
2, // 5: xray.app.stats.command.StatsService.GetStats:output_type -> xray.app.stats.command.GetStatsResponse
|
||||
4, // 6: xray.app.stats.command.StatsService.QueryStats:output_type -> xray.app.stats.command.QueryStatsResponse
|
||||
6, // 7: xray.app.stats.command.StatsService.GetSysStats:output_type -> xray.app.stats.command.SysStatsResponse
|
||||
5, // [5:8] is the sub-list for method output_type
|
||||
2, // [2:5] is the sub-list for method input_type
|
||||
2, // [2:2] is the sub-list for extension type_name
|
||||
2, // [2:2] is the sub-list for extension extendee
|
||||
0, // [0:2] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_app_stats_command_command_proto_init() }
|
||||
func file_app_stats_command_command_proto_init() {
|
||||
if File_app_stats_command_command_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_app_stats_command_command_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*GetStatsRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_app_stats_command_command_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*Stat); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_app_stats_command_command_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*GetStatsResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_app_stats_command_command_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*QueryStatsRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_app_stats_command_command_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*QueryStatsResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_app_stats_command_command_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*SysStatsRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_app_stats_command_command_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*SysStatsResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_app_stats_command_command_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*Config); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_app_stats_command_command_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 8,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_app_stats_command_command_proto_goTypes,
|
||||
DependencyIndexes: file_app_stats_command_command_proto_depIdxs,
|
||||
MessageInfos: file_app_stats_command_command_proto_msgTypes,
|
||||
}.Build()
|
||||
File_app_stats_command_command_proto = out.File
|
||||
file_app_stats_command_command_proto_rawDesc = nil
|
||||
file_app_stats_command_command_proto_goTypes = nil
|
||||
file_app_stats_command_command_proto_depIdxs = nil
|
||||
}
|
55
app/stats/command/command.proto
Normal file
55
app/stats/command/command.proto
Normal file
|
@ -0,0 +1,55 @@
|
|||
syntax = "proto3";
|
||||
|
||||
package xray.app.stats.command;
|
||||
option csharp_namespace = "Xray.App.Stats.Command";
|
||||
option go_package = "github.com/xtls/xray-core/v1/app/stats/command";
|
||||
option java_package = "com.xray.app.stats.command";
|
||||
option java_multiple_files = true;
|
||||
|
||||
message GetStatsRequest {
|
||||
// Name of the stat counter.
|
||||
string name = 1;
|
||||
// Whether or not to reset the counter to fetching its value.
|
||||
bool reset = 2;
|
||||
}
|
||||
|
||||
message Stat {
|
||||
string name = 1;
|
||||
int64 value = 2;
|
||||
}
|
||||
|
||||
message GetStatsResponse {
|
||||
Stat stat = 1;
|
||||
}
|
||||
|
||||
message QueryStatsRequest {
|
||||
string pattern = 1;
|
||||
bool reset = 2;
|
||||
}
|
||||
|
||||
message QueryStatsResponse {
|
||||
repeated Stat stat = 1;
|
||||
}
|
||||
|
||||
message SysStatsRequest {}
|
||||
|
||||
message SysStatsResponse {
|
||||
uint32 NumGoroutine = 1;
|
||||
uint32 NumGC = 2;
|
||||
uint64 Alloc = 3;
|
||||
uint64 TotalAlloc = 4;
|
||||
uint64 Sys = 5;
|
||||
uint64 Mallocs = 6;
|
||||
uint64 Frees = 7;
|
||||
uint64 LiveObjects = 8;
|
||||
uint64 PauseTotalNs = 9;
|
||||
uint32 Uptime = 10;
|
||||
}
|
||||
|
||||
service StatsService {
|
||||
rpc GetStats(GetStatsRequest) returns (GetStatsResponse) {}
|
||||
rpc QueryStats(QueryStatsRequest) returns (QueryStatsResponse) {}
|
||||
rpc GetSysStats(SysStatsRequest) returns (SysStatsResponse) {}
|
||||
}
|
||||
|
||||
message Config {}
|
169
app/stats/command/command_grpc.pb.go
Normal file
169
app/stats/command/command_grpc.pb.go
Normal file
|
@ -0,0 +1,169 @@
|
|||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
|
||||
package command
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
const _ = grpc.SupportPackageIsVersion7
|
||||
|
||||
// StatsServiceClient is the client API for StatsService service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
type StatsServiceClient interface {
|
||||
GetStats(ctx context.Context, in *GetStatsRequest, opts ...grpc.CallOption) (*GetStatsResponse, error)
|
||||
QueryStats(ctx context.Context, in *QueryStatsRequest, opts ...grpc.CallOption) (*QueryStatsResponse, error)
|
||||
GetSysStats(ctx context.Context, in *SysStatsRequest, opts ...grpc.CallOption) (*SysStatsResponse, error)
|
||||
}
|
||||
|
||||
type statsServiceClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewStatsServiceClient(cc grpc.ClientConnInterface) StatsServiceClient {
|
||||
return &statsServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *statsServiceClient) GetStats(ctx context.Context, in *GetStatsRequest, opts ...grpc.CallOption) (*GetStatsResponse, error) {
|
||||
out := new(GetStatsResponse)
|
||||
err := c.cc.Invoke(ctx, "/xray.app.stats.command.StatsService/GetStats", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *statsServiceClient) QueryStats(ctx context.Context, in *QueryStatsRequest, opts ...grpc.CallOption) (*QueryStatsResponse, error) {
|
||||
out := new(QueryStatsResponse)
|
||||
err := c.cc.Invoke(ctx, "/xray.app.stats.command.StatsService/QueryStats", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *statsServiceClient) GetSysStats(ctx context.Context, in *SysStatsRequest, opts ...grpc.CallOption) (*SysStatsResponse, error) {
|
||||
out := new(SysStatsResponse)
|
||||
err := c.cc.Invoke(ctx, "/xray.app.stats.command.StatsService/GetSysStats", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// StatsServiceServer is the server API for StatsService service.
|
||||
// All implementations must embed UnimplementedStatsServiceServer
|
||||
// for forward compatibility
|
||||
type StatsServiceServer interface {
|
||||
GetStats(context.Context, *GetStatsRequest) (*GetStatsResponse, error)
|
||||
QueryStats(context.Context, *QueryStatsRequest) (*QueryStatsResponse, error)
|
||||
GetSysStats(context.Context, *SysStatsRequest) (*SysStatsResponse, error)
|
||||
mustEmbedUnimplementedStatsServiceServer()
|
||||
}
|
||||
|
||||
// UnimplementedStatsServiceServer must be embedded to have forward compatible implementations.
|
||||
type UnimplementedStatsServiceServer struct {
|
||||
}
|
||||
|
||||
func (UnimplementedStatsServiceServer) GetStats(context.Context, *GetStatsRequest) (*GetStatsResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method GetStats not implemented")
|
||||
}
|
||||
func (UnimplementedStatsServiceServer) QueryStats(context.Context, *QueryStatsRequest) (*QueryStatsResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method QueryStats not implemented")
|
||||
}
|
||||
func (UnimplementedStatsServiceServer) GetSysStats(context.Context, *SysStatsRequest) (*SysStatsResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method GetSysStats not implemented")
|
||||
}
|
||||
func (UnimplementedStatsServiceServer) mustEmbedUnimplementedStatsServiceServer() {}
|
||||
|
||||
// UnsafeStatsServiceServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to StatsServiceServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeStatsServiceServer interface {
|
||||
mustEmbedUnimplementedStatsServiceServer()
|
||||
}
|
||||
|
||||
func RegisterStatsServiceServer(s grpc.ServiceRegistrar, srv StatsServiceServer) {
|
||||
s.RegisterService(&_StatsService_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _StatsService_GetStats_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GetStatsRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(StatsServiceServer).GetStats(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/xray.app.stats.command.StatsService/GetStats",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(StatsServiceServer).GetStats(ctx, req.(*GetStatsRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StatsService_QueryStats_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(QueryStatsRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(StatsServiceServer).QueryStats(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/xray.app.stats.command.StatsService/QueryStats",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(StatsServiceServer).QueryStats(ctx, req.(*QueryStatsRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StatsService_GetSysStats_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(SysStatsRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(StatsServiceServer).GetSysStats(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/xray.app.stats.command.StatsService/GetSysStats",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(StatsServiceServer).GetSysStats(ctx, req.(*SysStatsRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
var _StatsService_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "xray.app.stats.command.StatsService",
|
||||
HandlerType: (*StatsServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "GetStats",
|
||||
Handler: _StatsService_GetStats_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "QueryStats",
|
||||
Handler: _StatsService_QueryStats_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "GetSysStats",
|
||||
Handler: _StatsService_GetSysStats_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "app/stats/command/command.proto",
|
||||
}
|
92
app/stats/command/command_test.go
Normal file
92
app/stats/command/command_test.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
package command_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
|
||||
"github.com/xtls/xray-core/v1/app/stats"
|
||||
. "github.com/xtls/xray-core/v1/app/stats/command"
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
)
|
||||
|
||||
func TestGetStats(t *testing.T) {
|
||||
m, err := stats.NewManager(context.Background(), &stats.Config{})
|
||||
common.Must(err)
|
||||
|
||||
sc, err := m.RegisterCounter("test_counter")
|
||||
common.Must(err)
|
||||
|
||||
sc.Set(1)
|
||||
|
||||
s := NewStatsServer(m)
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
reset bool
|
||||
value int64
|
||||
err bool
|
||||
}{
|
||||
{
|
||||
name: "counterNotExist",
|
||||
err: true,
|
||||
},
|
||||
{
|
||||
name: "test_counter",
|
||||
reset: true,
|
||||
value: 1,
|
||||
},
|
||||
{
|
||||
name: "test_counter",
|
||||
value: 0,
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
resp, err := s.GetStats(context.Background(), &GetStatsRequest{
|
||||
Name: tc.name,
|
||||
Reset_: tc.reset,
|
||||
})
|
||||
if tc.err {
|
||||
if err == nil {
|
||||
t.Error("nil error: ", tc.name)
|
||||
}
|
||||
} else {
|
||||
common.Must(err)
|
||||
if r := cmp.Diff(resp.Stat, &Stat{Name: tc.name, Value: tc.value}, cmpopts.IgnoreUnexported(Stat{})); r != "" {
|
||||
t.Error(r)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryStats(t *testing.T) {
|
||||
m, err := stats.NewManager(context.Background(), &stats.Config{})
|
||||
common.Must(err)
|
||||
|
||||
sc1, err := m.RegisterCounter("test_counter")
|
||||
common.Must(err)
|
||||
sc1.Set(1)
|
||||
|
||||
sc2, err := m.RegisterCounter("test_counter_2")
|
||||
common.Must(err)
|
||||
sc2.Set(2)
|
||||
|
||||
sc3, err := m.RegisterCounter("test_counter_3")
|
||||
common.Must(err)
|
||||
sc3.Set(3)
|
||||
|
||||
s := NewStatsServer(m)
|
||||
resp, err := s.QueryStats(context.Background(), &QueryStatsRequest{
|
||||
Pattern: "counter_",
|
||||
})
|
||||
common.Must(err)
|
||||
if r := cmp.Diff(resp.Stat, []*Stat{
|
||||
{Name: "test_counter_2", Value: 2},
|
||||
{Name: "test_counter_3", Value: 3},
|
||||
}, cmpopts.SortSlices(func(s1, s2 *Stat) bool { return s1.Name < s2.Name }),
|
||||
cmpopts.IgnoreUnexported(Stat{})); r != "" {
|
||||
t.Error(r)
|
||||
}
|
||||
}
|
9
app/stats/command/errors.generated.go
Normal file
9
app/stats/command/errors.generated.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package command
|
||||
|
||||
import "github.com/xtls/xray-core/v1/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
225
app/stats/config.pb.go
Normal file
225
app/stats/config.pb.go
Normal file
|
@ -0,0 +1,225 @@
|
|||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.25.0
|
||||
// protoc v3.14.0
|
||||
// source: app/stats/config.proto
|
||||
|
||||
package stats
|
||||
|
||||
import (
|
||||
proto "github.com/golang/protobuf/proto"
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
// This is a compile-time assertion that a sufficiently up-to-date version
|
||||
// of the legacy proto package is being used.
|
||||
const _ = proto.ProtoPackageIsVersion4
|
||||
|
||||
type Config struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
}
|
||||
|
||||
func (x *Config) Reset() {
|
||||
*x = Config{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_app_stats_config_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *Config) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Config) ProtoMessage() {}
|
||||
|
||||
func (x *Config) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_app_stats_config_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use Config.ProtoReflect.Descriptor instead.
|
||||
func (*Config) Descriptor() ([]byte, []int) {
|
||||
return file_app_stats_config_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
type ChannelConfig struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Blocking bool `protobuf:"varint,1,opt,name=Blocking,proto3" json:"Blocking,omitempty"`
|
||||
SubscriberLimit int32 `protobuf:"varint,2,opt,name=SubscriberLimit,proto3" json:"SubscriberLimit,omitempty"`
|
||||
BufferSize int32 `protobuf:"varint,3,opt,name=BufferSize,proto3" json:"BufferSize,omitempty"`
|
||||
}
|
||||
|
||||
func (x *ChannelConfig) Reset() {
|
||||
*x = ChannelConfig{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_app_stats_config_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *ChannelConfig) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*ChannelConfig) ProtoMessage() {}
|
||||
|
||||
func (x *ChannelConfig) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_app_stats_config_proto_msgTypes[1]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use ChannelConfig.ProtoReflect.Descriptor instead.
|
||||
func (*ChannelConfig) Descriptor() ([]byte, []int) {
|
||||
return file_app_stats_config_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *ChannelConfig) GetBlocking() bool {
|
||||
if x != nil {
|
||||
return x.Blocking
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (x *ChannelConfig) GetSubscriberLimit() int32 {
|
||||
if x != nil {
|
||||
return x.SubscriberLimit
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *ChannelConfig) GetBufferSize() int32 {
|
||||
if x != nil {
|
||||
return x.BufferSize
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
var File_app_stats_config_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_app_stats_config_proto_rawDesc = []byte{
|
||||
0x0a, 0x16, 0x61, 0x70, 0x70, 0x2f, 0x73, 0x74, 0x61, 0x74, 0x73, 0x2f, 0x63, 0x6f, 0x6e, 0x66,
|
||||
0x69, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61,
|
||||
0x70, 0x70, 0x2e, 0x73, 0x74, 0x61, 0x74, 0x73, 0x22, 0x08, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66,
|
||||
0x69, 0x67, 0x22, 0x75, 0x0a, 0x0d, 0x43, 0x68, 0x61, 0x6e, 0x6e, 0x65, 0x6c, 0x43, 0x6f, 0x6e,
|
||||
0x66, 0x69, 0x67, 0x12, 0x1a, 0x0a, 0x08, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x69, 0x6e, 0x67, 0x18,
|
||||
0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x69, 0x6e, 0x67, 0x12,
|
||||
0x28, 0x0a, 0x0f, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x72, 0x4c, 0x69, 0x6d,
|
||||
0x69, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0f, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72,
|
||||
0x69, 0x62, 0x65, 0x72, 0x4c, 0x69, 0x6d, 0x69, 0x74, 0x12, 0x1e, 0x0a, 0x0a, 0x42, 0x75, 0x66,
|
||||
0x66, 0x65, 0x72, 0x53, 0x69, 0x7a, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x42,
|
||||
0x75, 0x66, 0x66, 0x65, 0x72, 0x53, 0x69, 0x7a, 0x65, 0x42, 0x4f, 0x0a, 0x12, 0x63, 0x6f, 0x6d,
|
||||
0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x74, 0x61, 0x74, 0x73, 0x50,
|
||||
0x01, 0x5a, 0x26, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74,
|
||||
0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x76, 0x31, 0x2f,
|
||||
0x61, 0x70, 0x70, 0x2f, 0x73, 0x74, 0x61, 0x74, 0x73, 0xaa, 0x02, 0x0e, 0x58, 0x72, 0x61, 0x79,
|
||||
0x2e, 0x41, 0x70, 0x70, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74,
|
||||
0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_app_stats_config_proto_rawDescOnce sync.Once
|
||||
file_app_stats_config_proto_rawDescData = file_app_stats_config_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_app_stats_config_proto_rawDescGZIP() []byte {
|
||||
file_app_stats_config_proto_rawDescOnce.Do(func() {
|
||||
file_app_stats_config_proto_rawDescData = protoimpl.X.CompressGZIP(file_app_stats_config_proto_rawDescData)
|
||||
})
|
||||
return file_app_stats_config_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_app_stats_config_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
|
||||
var file_app_stats_config_proto_goTypes = []interface{}{
|
||||
(*Config)(nil), // 0: xray.app.stats.Config
|
||||
(*ChannelConfig)(nil), // 1: xray.app.stats.ChannelConfig
|
||||
}
|
||||
var file_app_stats_config_proto_depIdxs = []int32{
|
||||
0, // [0:0] is the sub-list for method output_type
|
||||
0, // [0:0] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_app_stats_config_proto_init() }
|
||||
func file_app_stats_config_proto_init() {
|
||||
if File_app_stats_config_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_app_stats_config_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*Config); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_app_stats_config_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*ChannelConfig); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_app_stats_config_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 2,
|
||||
NumExtensions: 0,
|
||||
NumServices: 0,
|
||||
},
|
||||
GoTypes: file_app_stats_config_proto_goTypes,
|
||||
DependencyIndexes: file_app_stats_config_proto_depIdxs,
|
||||
MessageInfos: file_app_stats_config_proto_msgTypes,
|
||||
}.Build()
|
||||
File_app_stats_config_proto = out.File
|
||||
file_app_stats_config_proto_rawDesc = nil
|
||||
file_app_stats_config_proto_goTypes = nil
|
||||
file_app_stats_config_proto_depIdxs = nil
|
||||
}
|
15
app/stats/config.proto
Normal file
15
app/stats/config.proto
Normal file
|
@ -0,0 +1,15 @@
|
|||
syntax = "proto3";
|
||||
|
||||
package xray.app.stats;
|
||||
option csharp_namespace = "Xray.App.Stats";
|
||||
option go_package = "github.com/xtls/xray-core/v1/app/stats";
|
||||
option java_package = "com.xray.app.stats";
|
||||
option java_multiple_files = true;
|
||||
|
||||
message Config {}
|
||||
|
||||
message ChannelConfig {
|
||||
bool Blocking = 1;
|
||||
int32 SubscriberLimit = 2;
|
||||
int32 BufferSize = 3;
|
||||
}
|
25
app/stats/counter.go
Normal file
25
app/stats/counter.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
// +build !confonly
|
||||
|
||||
package stats
|
||||
|
||||
import "sync/atomic"
|
||||
|
||||
// Counter is an implementation of stats.Counter.
|
||||
type Counter struct {
|
||||
value int64
|
||||
}
|
||||
|
||||
// Value implements stats.Counter.
|
||||
func (c *Counter) Value() int64 {
|
||||
return atomic.LoadInt64(&c.value)
|
||||
}
|
||||
|
||||
// Set implements stats.Counter.
|
||||
func (c *Counter) Set(newValue int64) int64 {
|
||||
return atomic.SwapInt64(&c.value, newValue)
|
||||
}
|
||||
|
||||
// Add implements stats.Counter.
|
||||
func (c *Counter) Add(delta int64) int64 {
|
||||
return atomic.AddInt64(&c.value, delta)
|
||||
}
|
31
app/stats/counter_test.go
Normal file
31
app/stats/counter_test.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package stats_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
. "github.com/xtls/xray-core/v1/app/stats"
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
"github.com/xtls/xray-core/v1/features/stats"
|
||||
)
|
||||
|
||||
func TestStatsCounter(t *testing.T) {
|
||||
raw, err := common.CreateObject(context.Background(), &Config{})
|
||||
common.Must(err)
|
||||
|
||||
m := raw.(stats.Manager)
|
||||
c, err := m.RegisterCounter("test.counter")
|
||||
common.Must(err)
|
||||
|
||||
if v := c.Add(1); v != 1 {
|
||||
t.Fatal("unpexcted Add(1) return: ", v, ", wanted ", 1)
|
||||
}
|
||||
|
||||
if v := c.Set(0); v != 1 {
|
||||
t.Fatal("unexpected Set(0) return: ", v, ", wanted ", 1)
|
||||
}
|
||||
|
||||
if v := c.Value(); v != 0 {
|
||||
t.Fatal("unexpected Value() return: ", v, ", wanted ", 0)
|
||||
}
|
||||
}
|
9
app/stats/errors.generated.go
Normal file
9
app/stats/errors.generated.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package stats
|
||||
|
||||
import "github.com/xtls/xray-core/v1/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
169
app/stats/stats.go
Normal file
169
app/stats/stats.go
Normal file
|
@ -0,0 +1,169 @@
|
|||
// +build !confonly
|
||||
|
||||
package stats
|
||||
|
||||
//go:generate go run github.com/xtls/xray-core/v1/common/errors/errorgen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
"github.com/xtls/xray-core/v1/common/errors"
|
||||
"github.com/xtls/xray-core/v1/features/stats"
|
||||
)
|
||||
|
||||
// Manager is an implementation of stats.Manager.
|
||||
type Manager struct {
|
||||
access sync.RWMutex
|
||||
counters map[string]*Counter
|
||||
channels map[string]*Channel
|
||||
running bool
|
||||
}
|
||||
|
||||
// NewManager creates an instance of Statistics Manager.
|
||||
func NewManager(ctx context.Context, config *Config) (*Manager, error) {
|
||||
m := &Manager{
|
||||
counters: make(map[string]*Counter),
|
||||
channels: make(map[string]*Channel),
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Type implements common.HasType.
|
||||
func (*Manager) Type() interface{} {
|
||||
return stats.ManagerType()
|
||||
}
|
||||
|
||||
// RegisterCounter implements stats.Manager.
|
||||
func (m *Manager) RegisterCounter(name string) (stats.Counter, error) {
|
||||
m.access.Lock()
|
||||
defer m.access.Unlock()
|
||||
|
||||
if _, found := m.counters[name]; found {
|
||||
return nil, newError("Counter ", name, " already registered.")
|
||||
}
|
||||
newError("create new counter ", name).AtDebug().WriteToLog()
|
||||
c := new(Counter)
|
||||
m.counters[name] = c
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// UnregisterCounter implements stats.Manager.
|
||||
func (m *Manager) UnregisterCounter(name string) error {
|
||||
m.access.Lock()
|
||||
defer m.access.Unlock()
|
||||
|
||||
if _, found := m.counters[name]; found {
|
||||
newError("remove counter ", name).AtDebug().WriteToLog()
|
||||
delete(m.counters, name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCounter implements stats.Manager.
|
||||
func (m *Manager) GetCounter(name string) stats.Counter {
|
||||
m.access.RLock()
|
||||
defer m.access.RUnlock()
|
||||
|
||||
if c, found := m.counters[name]; found {
|
||||
return c
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// VisitCounters calls visitor function on all managed counters.
|
||||
func (m *Manager) VisitCounters(visitor func(string, stats.Counter) bool) {
|
||||
m.access.RLock()
|
||||
defer m.access.RUnlock()
|
||||
|
||||
for name, c := range m.counters {
|
||||
if !visitor(name, c) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterChannel implements stats.Manager.
|
||||
func (m *Manager) RegisterChannel(name string) (stats.Channel, error) {
|
||||
m.access.Lock()
|
||||
defer m.access.Unlock()
|
||||
|
||||
if _, found := m.channels[name]; found {
|
||||
return nil, newError("Channel ", name, " already registered.")
|
||||
}
|
||||
newError("create new channel ", name).AtDebug().WriteToLog()
|
||||
c := NewChannel(&ChannelConfig{BufferSize: 64, Blocking: false})
|
||||
m.channels[name] = c
|
||||
if m.running {
|
||||
return c, c.Start()
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// UnregisterChannel implements stats.Manager.
|
||||
func (m *Manager) UnregisterChannel(name string) error {
|
||||
m.access.Lock()
|
||||
defer m.access.Unlock()
|
||||
|
||||
if c, found := m.channels[name]; found {
|
||||
newError("remove channel ", name).AtDebug().WriteToLog()
|
||||
delete(m.channels, name)
|
||||
return c.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetChannel implements stats.Manager.
|
||||
func (m *Manager) GetChannel(name string) stats.Channel {
|
||||
m.access.RLock()
|
||||
defer m.access.RUnlock()
|
||||
|
||||
if c, found := m.channels[name]; found {
|
||||
return c
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start implements common.Runnable.
|
||||
func (m *Manager) Start() error {
|
||||
m.access.Lock()
|
||||
defer m.access.Unlock()
|
||||
m.running = true
|
||||
errs := []error{}
|
||||
for _, channel := range m.channels {
|
||||
if err := channel.Start(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
if len(errs) != 0 {
|
||||
return errors.Combine(errs...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close implement common.Closable.
|
||||
func (m *Manager) Close() error {
|
||||
m.access.Lock()
|
||||
defer m.access.Unlock()
|
||||
m.running = false
|
||||
errs := []error{}
|
||||
for name, channel := range m.channels {
|
||||
newError("remove channel ", name).AtDebug().WriteToLog()
|
||||
delete(m.channels, name)
|
||||
if err := channel.Close(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
if len(errs) != 0 {
|
||||
return errors.Combine(errs...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
||||
return NewManager(ctx, config.(*Config))
|
||||
}))
|
||||
}
|
86
app/stats/stats_test.go
Normal file
86
app/stats/stats_test.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
package stats_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/xtls/xray-core/v1/app/stats"
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
"github.com/xtls/xray-core/v1/features/stats"
|
||||
)
|
||||
|
||||
func TestInterface(t *testing.T) {
|
||||
_ = (stats.Manager)(new(Manager))
|
||||
}
|
||||
|
||||
func TestStatsChannelRunnable(t *testing.T) {
|
||||
raw, err := common.CreateObject(context.Background(), &Config{})
|
||||
common.Must(err)
|
||||
|
||||
m := raw.(stats.Manager)
|
||||
|
||||
ch1, err := m.RegisterChannel("test.channel.1")
|
||||
c1 := ch1.(*Channel)
|
||||
common.Must(err)
|
||||
|
||||
if c1.Running() {
|
||||
t.Fatalf("unexpected running channel: test.channel.%d", 1)
|
||||
}
|
||||
|
||||
common.Must(m.Start())
|
||||
|
||||
if !c1.Running() {
|
||||
t.Fatalf("unexpected non-running channel: test.channel.%d", 1)
|
||||
}
|
||||
|
||||
ch2, err := m.RegisterChannel("test.channel.2")
|
||||
c2 := ch2.(*Channel)
|
||||
common.Must(err)
|
||||
|
||||
if !c2.Running() {
|
||||
t.Fatalf("unexpected non-running channel: test.channel.%d", 2)
|
||||
}
|
||||
|
||||
s1, err := c1.Subscribe()
|
||||
common.Must(err)
|
||||
common.Must(c1.Close())
|
||||
|
||||
if c1.Running() {
|
||||
t.Fatalf("unexpected running channel: test.channel.%d", 1)
|
||||
}
|
||||
|
||||
select { // Check all subscribers in closed channel are closed
|
||||
case _, ok := <-s1:
|
||||
if ok {
|
||||
t.Fatalf("unexpected non-closed subscriber in channel: test.channel.%d", 1)
|
||||
}
|
||||
case <-time.After(500 * time.Millisecond):
|
||||
t.Fatalf("unexpected non-closed subscriber in channel: test.channel.%d", 1)
|
||||
}
|
||||
|
||||
if len(c1.Subscribers()) != 0 { // Check subscribers in closed channel are emptied
|
||||
t.Fatalf("unexpected non-empty subscribers in channel: test.channel.%d", 1)
|
||||
}
|
||||
|
||||
common.Must(m.Close())
|
||||
|
||||
if c2.Running() {
|
||||
t.Fatalf("unexpected running channel: test.channel.%d", 2)
|
||||
}
|
||||
|
||||
ch3, err := m.RegisterChannel("test.channel.3")
|
||||
c3 := ch3.(*Channel)
|
||||
common.Must(err)
|
||||
|
||||
if c3.Running() {
|
||||
t.Fatalf("unexpected running channel: test.channel.%d", 3)
|
||||
}
|
||||
|
||||
common.Must(c3.Start())
|
||||
common.Must(m.UnregisterChannel("test.channel.3"))
|
||||
|
||||
if c3.Running() { // Test that unregistering will close the channel.
|
||||
t.Fatalf("unexpected running channel: test.channel.%d", 3)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue