Skip to content

Commit

Permalink
use go once in init
Browse files Browse the repository at this point in the history
  • Loading branch information
rucciva committed Nov 6, 2023
1 parent f1dc712 commit 9df4659
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,28 @@ type Config struct {
Injector configInjector `json:"injector" yaml:"injector"`

omniplug *Omniplug
mut sync.Mutex
once sync.Once
onceErr error
}

func (conf *Config) Init() (err error) {
if conf.omniplug != nil {
return
}

conf.mut.Lock()
defer conf.mut.Unlock()
if conf.omniplug == nil {
conf.once.Do(func() {
v, err := json.Marshal(conf)
if err != nil {
return err
conf.onceErr = err
return
}

conf.omniplug = &Omniplug{}
if err = json.Unmarshal(v, conf.omniplug); err != nil {
return err
if conf.onceErr = json.Unmarshal(v, conf.omniplug); conf.onceErr != nil {
return
}

if err = conf.omniplug.Init(); err != nil {
return err
if conf.onceErr = conf.omniplug.Init(); conf.onceErr != nil {
return
}
}
return
})
return conf.onceErr
}

func (conf *Config) Access(kong *pdk.PDK) {
Expand Down

0 comments on commit 9df4659

Please sign in to comment.