From 4c4820c60f1e1aa635cb79ca6de8f86f2dec008f Mon Sep 17 00:00:00 2001 From: hiddify Date: Mon, 12 Feb 2024 13:45:05 +0100 Subject: [PATCH] new: add multiple receiver for having multiple command client group --- Makefile | 4 ++-- common/urltest/urltest.go | 14 ++++++++------ experimental/libbox/command_group.go | 3 ++- experimental/libbox/command_server.go | 14 ++++++++------ go.mod | 1 + go.sum | 2 ++ 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index d2aa65d988..f5c878191d 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ NAME = sing-box COMMIT = $(shell git rev-parse --short HEAD) TAGS_GO118 = with_gvisor,with_dhcp,with_wireguard,with_reality_server,with_clash_api TAGS_GO120 = with_quic,with_ech,with_utls -TAGS ?= $(TAGS_GO118),$(TAGS_GO120) +TAGS ?= $(TAGS_GO118),$(TAGS_GO120),with_gvisor,with_quic,with_wireguard,with_ech,with_utls,with_clash_api,with_grpc TAGS_TEST ?= with_gvisor,with_quic,with_wireguard,with_grpc,with_ech,with_utls,with_reality_server GOHOSTOS = $(shell go env GOHOSTOS) @@ -196,4 +196,4 @@ clean: update: git fetch git reset FETCH_HEAD --hard - git clean -fdx \ No newline at end of file + git clean -fdx diff --git a/common/urltest/urltest.go b/common/urltest/urltest.go index 001f2e15a6..bfeb13ba3d 100644 --- a/common/urltest/urltest.go +++ b/common/urltest/urltest.go @@ -8,6 +8,7 @@ import ( "sync" "time" + "github.com/imkira/go-observer" "github.com/sagernet/sing/common" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" @@ -21,7 +22,7 @@ type History struct { type HistoryStorage struct { access sync.RWMutex delayHistory map[string]*History - updateHook chan<- struct{} + updateHook observer.Property } func NewHistoryStorage() *HistoryStorage { @@ -30,7 +31,7 @@ func NewHistoryStorage() *HistoryStorage { } } -func (s *HistoryStorage) SetHook(hook chan<- struct{}) { +func (s *HistoryStorage) SetHook(hook observer.Property) { s.updateHook = hook } @@ -60,10 +61,11 @@ func (s *HistoryStorage) StoreURLTestHistory(tag string, history *History) { func (s *HistoryStorage) notifyUpdated() { updateHook := s.updateHook if updateHook != nil { - select { - case updateHook <- struct{}{}: - default: - } + updateHook.Update(1) + // select { + // case updateHook <- struct{}{}: + // default: + // } } } diff --git a/experimental/libbox/command_group.go b/experimental/libbox/command_group.go index 8cb86c937c..191fb8dc99 100644 --- a/experimental/libbox/command_group.go +++ b/experimental/libbox/command_group.go @@ -68,6 +68,7 @@ func (s *CommandServer) handleGroupConn(conn net.Conn, onlyGroupItems bool) erro ticker := time.NewTicker(time.Duration(interval)) defer ticker.Stop() ctx := connKeepAlive(conn) + urlTestUpdateStream := s.urlTestUpdate.Observe() for { service := s.service if service != nil { @@ -89,7 +90,7 @@ func (s *CommandServer) handleGroupConn(conn net.Conn, onlyGroupItems bool) erro select { case <-ctx.Done(): return ctx.Err() - case <-s.urlTestUpdate: + case <-urlTestUpdateStream.Changes(): } } } diff --git a/experimental/libbox/command_server.go b/experimental/libbox/command_server.go index df25fa72aa..430613ddb3 100644 --- a/experimental/libbox/command_server.go +++ b/experimental/libbox/command_server.go @@ -7,6 +7,7 @@ import ( "path/filepath" "sync" + "github.com/imkira/go-observer" "github.com/sagernet/sing-box/common/urltest" "github.com/sagernet/sing-box/experimental/clashapi" "github.com/sagernet/sing-box/log" @@ -30,7 +31,7 @@ type CommandServer struct { service *BoxService // These channels only work with a single client. if multi-client support is needed, replace with Subscriber/Observer - urlTestUpdate chan struct{} + urlTestUpdate observer.Property modeUpdate chan struct{} logReset chan struct{} } @@ -46,7 +47,7 @@ func NewCommandServer(handler CommandServerHandler, maxLines int32) *CommandServ handler: handler, maxLines: int(maxLines), subscriber: observable.NewSubscriber[string](128), - urlTestUpdate: make(chan struct{}, 1), + urlTestUpdate: observer.NewProperty(0), modeUpdate: make(chan struct{}, 1), logReset: make(chan struct{}, 1), } @@ -69,10 +70,11 @@ func (s *CommandServer) SetService(newService *BoxService) { } func (s *CommandServer) notifyURLTestUpdate() { - select { - case s.urlTestUpdate <- struct{}{}: - default: - } + // select { + // case s.urlTestUpdate <- struct{}{}: + // default: + // } + s.urlTestUpdate.Update(1) } func (s *CommandServer) Start() error { diff --git a/go.mod b/go.mod index 3ace9c2e8a..174451cf41 100644 --- a/go.mod +++ b/go.mod @@ -71,6 +71,7 @@ require ( github.com/google/btree v1.1.2 // indirect github.com/google/pprof v0.0.0-20231101202521-4ca4178f5c7a // indirect github.com/hashicorp/yamux v0.1.1 // indirect + github.com/imkira/go-observer v1.0.3 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/josharian/native v1.1.0 // indirect github.com/klauspost/compress v1.17.4 // indirect diff --git a/go.sum b/go.sum index f4cbee8b4f..be4a69e7af 100644 --- a/go.sum +++ b/go.sum @@ -49,6 +49,8 @@ github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= github.com/hiddify/wireguard-go v0.0.0-20240125143346-481d18d77fe1 h1:neOb+wzHbWLNZ2sHFEV4+GTuqORO7/MndQLFW8FjUY8= github.com/hiddify/wireguard-go v0.0.0-20240125143346-481d18d77fe1/go.mod h1:K4J7/npM+VAMUeUmTa2JaA02JmyheP0GpRBOUvn3ecc= +github.com/imkira/go-observer v1.0.3 h1:l45TYAEeAB4L2xF6PR2gRLn2NE5tYhudh33MLmC7B80= +github.com/imkira/go-observer v1.0.3/go.mod h1:zLzElv2cGTHufQG17IEILJMPDg32TD85fFgKyFv00wU= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/insomniacslk/dhcp v0.0.0-20231206064809-8c70d406f6d2 h1:9K06NfxkBh25x56yVhWWlKFE8YpicaSfHwoV8SFbueA=