mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-06-08 04:04:55 +03:00
Add xhttp transport
This commit is contained in:
64
common/xray/task/task.go
Normal file
64
common/xray/task/task.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/sagernet/sing-box/common/xray/signal/semaphore"
|
||||
)
|
||||
|
||||
// OnSuccess executes g() after f() returns nil.
|
||||
func OnSuccess(f func() error, g func() error) func() error {
|
||||
return func() error {
|
||||
if err := f(); err != nil {
|
||||
return err
|
||||
}
|
||||
return g()
|
||||
}
|
||||
}
|
||||
|
||||
// Run executes a list of tasks in parallel, returns the first error encountered or nil if all tasks pass.
|
||||
func Run(ctx context.Context, tasks ...func() error) error {
|
||||
n := len(tasks)
|
||||
s := semaphore.New(n)
|
||||
done := make(chan error, 1)
|
||||
|
||||
for _, task := range tasks {
|
||||
<-s.Wait()
|
||||
go func(f func() error) {
|
||||
err := f()
|
||||
if err == nil {
|
||||
s.Signal()
|
||||
return
|
||||
}
|
||||
|
||||
select {
|
||||
case done <- err:
|
||||
default:
|
||||
}
|
||||
}(task)
|
||||
}
|
||||
|
||||
/*
|
||||
if altctx := ctx.Value("altctx"); altctx != nil {
|
||||
ctx = altctx.(context.Context)
|
||||
}
|
||||
*/
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
select {
|
||||
case err := <-done:
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-s.Wait():
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
if cancel := ctx.Value("cancel"); cancel != nil {
|
||||
cancel.(context.CancelFunc)()
|
||||
}
|
||||
*/
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user