mirror of https://github.com/v2ray/v2ray-core
more clear logic of space initialization
parent
8b88f63280
commit
54f98fe11b
28
app/space.go
28
app/space.go
|
@ -2,7 +2,6 @@ package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"sync/atomic"
|
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common"
|
"github.com/v2ray/v2ray-core/common"
|
||||||
)
|
)
|
||||||
|
@ -41,40 +40,27 @@ type Space interface {
|
||||||
|
|
||||||
type spaceImpl struct {
|
type spaceImpl struct {
|
||||||
cache map[ID]Application
|
cache map[ID]Application
|
||||||
initSignal chan struct{}
|
appInit []ApplicationInitializer
|
||||||
initErrors chan error
|
|
||||||
appsToInit int32
|
|
||||||
appsDone int32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSpace() Space {
|
func NewSpace() Space {
|
||||||
return &spaceImpl{
|
return &spaceImpl{
|
||||||
cache: make(map[ID]Application),
|
cache: make(map[ID]Application),
|
||||||
initSignal: make(chan struct{}),
|
appInit: make([]ApplicationInitializer, 0, 32),
|
||||||
initErrors: make(chan error, 1),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *spaceImpl) InitializeApplication(f ApplicationInitializer) {
|
func (this *spaceImpl) InitializeApplication(f ApplicationInitializer) {
|
||||||
atomic.AddInt32(&(this.appsToInit), 1)
|
this.appInit = append(this.appInit, f)
|
||||||
go func() {
|
|
||||||
<-this.initSignal
|
|
||||||
err := f()
|
|
||||||
if err != nil {
|
|
||||||
this.initErrors <- err
|
|
||||||
}
|
|
||||||
count := atomic.AddInt32(&(this.appsDone), 1)
|
|
||||||
if count == this.appsToInit {
|
|
||||||
close(this.initErrors)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *spaceImpl) Initialize() error {
|
func (this *spaceImpl) Initialize() error {
|
||||||
close(this.initSignal)
|
for _, f := range this.appInit {
|
||||||
if err, open := <-this.initErrors; open {
|
err := f()
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue