forked from doreamon-design/clash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change: proxy gruop strategy improvement
- Loading branch information
Showing
7 changed files
with
198 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package outboundgroup | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/Dreamacro/clash/adapters/provider" | ||
C "github.com/Dreamacro/clash/constant" | ||
) | ||
|
||
const ( | ||
defaultGetProxiesDuration = time.Second * 5 | ||
) | ||
|
||
func getProvidersProxies(providers []provider.ProxyProvider) []C.Proxy { | ||
proxies := []C.Proxy{} | ||
for _, provider := range providers { | ||
proxies = append(proxies, provider.Proxies()...) | ||
} | ||
return proxies | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package singledo | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type call struct { | ||
wg sync.WaitGroup | ||
val interface{} | ||
err error | ||
} | ||
|
||
type Single struct { | ||
mux sync.Mutex | ||
last int64 | ||
wait int64 | ||
call *call | ||
result *Result | ||
} | ||
|
||
type Result struct { | ||
Val interface{} | ||
Err error | ||
} | ||
|
||
func (s *Single) Do(fn func() (interface{}, error)) (v interface{}, err error, shared bool) { | ||
s.mux.Lock() | ||
now := time.Now().Unix() | ||
if now < s.last+s.wait { | ||
s.mux.Unlock() | ||
return s.result.Val, s.result.Err, true | ||
} | ||
|
||
if call := s.call; call != nil { | ||
s.mux.Unlock() | ||
call.wg.Wait() | ||
return call.val, call.err, true | ||
} | ||
|
||
call := &call{} | ||
call.wg.Add(1) | ||
s.call = call | ||
s.mux.Unlock() | ||
call.val, call.err = fn() | ||
s.call = nil | ||
s.result = &Result{call.val, call.err} | ||
s.last = now | ||
return call.val, call.err, false | ||
} | ||
|
||
func NewSingle(wait time.Duration) *Single { | ||
return &Single{wait: int64(wait)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package singledo | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBasic(t *testing.T) { | ||
single := NewSingle(time.Millisecond * 30) | ||
foo := 0 | ||
shardCount := 0 | ||
call := func() (interface{}, error) { | ||
foo++ | ||
return nil, nil | ||
} | ||
|
||
var wg sync.WaitGroup | ||
const n = 10 | ||
wg.Add(n) | ||
for i := 0; i < n; i++ { | ||
go func() { | ||
_, _, shard := single.Do(call) | ||
if shard { | ||
shardCount++ | ||
} | ||
wg.Done() | ||
}() | ||
} | ||
|
||
wg.Wait() | ||
assert.Equal(t, 1, foo) | ||
assert.Equal(t, 9, shardCount) | ||
} | ||
|
||
func TestTimer(t *testing.T) { | ||
single := NewSingle(time.Millisecond * 30) | ||
foo := 0 | ||
call := func() (interface{}, error) { | ||
foo++ | ||
return nil, nil | ||
} | ||
|
||
single.Do(call) | ||
time.Sleep(10 * time.Millisecond) | ||
_, _, shard := single.Do(call) | ||
|
||
assert.Equal(t, 1, foo) | ||
assert.True(t, shard) | ||
} |