Skip to content

Commit

Permalink
new: add multiple receiver for having multiple command client group
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddify-com committed Feb 12, 2024
1 parent 8a565a8 commit 4c4820c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -196,4 +196,4 @@ clean:
update:
git fetch
git reset FETCH_HEAD --hard
git clean -fdx
git clean -fdx
14 changes: 8 additions & 6 deletions common/urltest/urltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -30,7 +31,7 @@ func NewHistoryStorage() *HistoryStorage {
}
}

func (s *HistoryStorage) SetHook(hook chan<- struct{}) {
func (s *HistoryStorage) SetHook(hook observer.Property) {
s.updateHook = hook
}

Expand Down Expand Up @@ -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:
// }
}
}

Expand Down
3 changes: 2 additions & 1 deletion experimental/libbox/command_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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():
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions experimental/libbox/command_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{}
}
Expand All @@ -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),
}
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 4c4820c

Please sign in to comment.