2020-09-15 11:04:26 -04:00
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
2020-10-02 15:06:32 -04:00
|
|
|
"context"
|
2020-09-15 11:04:26 -04:00
|
|
|
"sync"
|
2020-09-23 10:35:13 -04:00
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
2020-09-15 11:04:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Channel is an implementation of stats.Channel.
|
|
|
|
type Channel struct {
|
2020-10-02 15:06:32 -04:00
|
|
|
channel chan channelMessage
|
2020-09-23 10:35:13 -04:00
|
|
|
subscribers []chan interface{}
|
|
|
|
|
|
|
|
// Synchronization components
|
2020-09-15 11:04:26 -04:00
|
|
|
access sync.RWMutex
|
|
|
|
closed chan struct{}
|
|
|
|
|
2020-09-23 10:35:13 -04:00
|
|
|
// Channel options
|
2020-10-02 15:06:32 -04:00
|
|
|
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
|
2020-09-23 10:35:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewChannel creates an instance of Statistics Channel.
|
|
|
|
func NewChannel(config *ChannelConfig) *Channel {
|
|
|
|
return &Channel{
|
2020-10-02 15:06:32 -04:00
|
|
|
channel: make(chan channelMessage, config.BufferSize),
|
|
|
|
subsLimit: int(config.SubscriberLimit),
|
|
|
|
bufferSize: int(config.BufferSize),
|
|
|
|
blocking: config.Blocking,
|
2020-09-23 10:35:13 -04:00
|
|
|
}
|
2020-09-15 11:04:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribers implements stats.Channel.
|
|
|
|
func (c *Channel) Subscribers() []chan interface{} {
|
|
|
|
c.access.RLock()
|
|
|
|
defer c.access.RUnlock()
|
|
|
|
return c.subscribers
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe implements stats.Channel.
|
2020-09-23 10:35:13 -04:00
|
|
|
func (c *Channel) Subscribe() (chan interface{}, error) {
|
2020-09-15 11:04:26 -04:00
|
|
|
c.access.Lock()
|
|
|
|
defer c.access.Unlock()
|
2020-10-02 15:06:32 -04:00
|
|
|
if c.subsLimit > 0 && len(c.subscribers) >= c.subsLimit {
|
2020-09-23 10:35:13 -04:00
|
|
|
return nil, newError("Number of subscribers has reached limit")
|
|
|
|
}
|
2020-10-02 15:06:32 -04:00
|
|
|
subscriber := make(chan interface{}, c.bufferSize)
|
2020-09-15 11:04:26 -04:00
|
|
|
c.subscribers = append(c.subscribers, subscriber)
|
2020-09-23 10:35:13 -04:00
|
|
|
return subscriber, nil
|
2020-09-15 11:04:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unsubscribe implements stats.Channel.
|
2020-09-23 10:35:13 -04:00
|
|
|
func (c *Channel) Unsubscribe(subscriber chan interface{}) error {
|
2020-09-15 11:04:26 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2020-09-23 10:35:13 -04:00
|
|
|
return nil
|
2020-09-15 11:04:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Publish implements stats.Channel.
|
2020-10-02 15:06:32 -04:00
|
|
|
func (c *Channel) Publish(ctx context.Context, msg interface{}) {
|
2020-09-15 11:04:26 -04:00
|
|
|
select { // Early exit if channel closed
|
|
|
|
case <-c.closed:
|
|
|
|
return
|
|
|
|
default:
|
2020-10-02 15:06:32 -04:00
|
|
|
pub := channelMessage{context: ctx, message: msg}
|
|
|
|
if c.blocking {
|
|
|
|
pub.publish(c.channel)
|
|
|
|
} else {
|
|
|
|
pub.publishNonBlocking(c.channel)
|
|
|
|
}
|
2020-09-15 11:04:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2020-09-23 10:35:13 -04:00
|
|
|
if !c.Running() {
|
|
|
|
c.closed = make(chan struct{}) // Reset close signal
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2020-10-02 15:06:32 -04:00
|
|
|
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)
|
2020-09-23 10:35:13 -04:00
|
|
|
}
|
2020-09-15 11:04:26 -04:00
|
|
|
}
|
2020-09-23 10:35:13 -04:00
|
|
|
case <-c.closed: // Channel closed
|
|
|
|
for _, sub := range c.Subscribers() { // Remove all subscribers
|
|
|
|
common.Must(c.Unsubscribe(sub))
|
|
|
|
close(sub)
|
|
|
|
}
|
|
|
|
return
|
2020-09-15 11:04:26 -04:00
|
|
|
}
|
|
|
|
}
|
2020-09-23 10:35:13 -04:00
|
|
|
}()
|
|
|
|
}
|
2020-09-15 11:04:26 -04:00
|
|
|
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
|
|
|
|
}
|
2020-10-02 15:06:32 -04:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|