Skip to content

Commit

Permalink
Package Config:
Browse files Browse the repository at this point in the history
- component/request: fix IsStarted function
- component/head: replace mutex to atomic

Package LDAP:
- fix bug if ldapPort for TLS is not set
- fix bug if ldapPort for StartTLS or noTLS is not set
  • Loading branch information
nabbar committed Mar 27, 2024
1 parent eed9a8e commit cce6ba5
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 66 deletions.
20 changes: 1 addition & 19 deletions config/components/head/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ import (
)

func (o *componentHead) _getKey() string {
o.m.RLock()
defer o.m.RUnlock()

if i, l := o.x.Load(keyCptKey); !l {
return ""
} else if i == nil {
Expand All @@ -49,9 +46,6 @@ func (o *componentHead) _getKey() string {
}

func (o *componentHead) _getFctVpr() libvpr.FuncViper {
o.m.RLock()
defer o.m.RUnlock()

if i, l := o.x.Load(keyFctViper); !l {
return nil
} else if i == nil {
Expand Down Expand Up @@ -84,9 +78,6 @@ func (o *componentHead) _getSPFViper() *spfvbr.Viper {
}

func (o *componentHead) _getFctCpt() cfgtps.FuncCptGet {
o.m.RLock()
defer o.m.RUnlock()

if i, l := o.x.Load(keyFctGetCpt); !l {
return nil
} else if i == nil {
Expand All @@ -99,9 +90,6 @@ func (o *componentHead) _getFctCpt() cfgtps.FuncCptGet {
}

func (o *componentHead) _getVersion() libver.Version {
o.m.RLock()
defer o.m.RUnlock()

if i, l := o.x.Load(keyCptVersion); !l {
return nil
} else if i == nil {
Expand All @@ -122,9 +110,6 @@ func (o *componentHead) _getFct() (cfgtps.FuncCptEvent, cfgtps.FuncCptEvent) {
}

func (o *componentHead) _getFctEvt(key uint8) cfgtps.FuncCptEvent {
o.m.RLock()
defer o.m.RUnlock()

if i, l := o.x.Load(key); !l {
return nil
} else if i == nil {
Expand All @@ -148,10 +133,7 @@ func (o *componentHead) _runCli() error {
if cfg, err := o._getConfig(); err != nil {
return ErrorParamInvalid.Error(err)
} else {
o.m.Lock()
defer o.m.Unlock()

o.h = cfg.New()
o.SetHeaders(cfg.New())
return nil
}
}
Expand Down
29 changes: 3 additions & 26 deletions config/components/head/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,6 @@ func (o *componentHead) Type() string {
}

func (o *componentHead) Init(key string, ctx libctx.FuncContext, get cfgtps.FuncCptGet, vpr libvpr.FuncViper, vrs libver.Version, log liblog.FuncLog) {
o.m.Lock()
defer o.m.Unlock()

if o.x == nil {
o.x = libctx.NewConfig[uint8](ctx)
} else {
x := libctx.NewConfig[uint8](ctx)
x.Merge(o.x)
o.x = x
}

o.x.Store(keyCptKey, key)
o.x.Store(keyFctGetCpt, get)
o.x.Store(keyFctViper, vpr)
Expand All @@ -84,10 +73,7 @@ func (o *componentHead) RegisterFuncReload(before, after cfgtps.FuncCptEvent) {
}

func (o *componentHead) IsStarted() bool {
o.m.RLock()
defer o.m.RUnlock()

return o.h != nil
return len(o.GetHeaders().Header()) > 0
}

func (o *componentHead) IsRunning() bool {
Expand All @@ -103,17 +89,11 @@ func (o *componentHead) Reload() error {
}

func (o *componentHead) Stop() {
o.m.Lock()
defer o.m.Unlock()

o.h = nil
o.SetHeaders(nil)
return
}

func (o *componentHead) Dependencies() []string {
o.m.RLock()
defer o.m.RUnlock()

var def = make([]string, 0)

if o == nil {
Expand All @@ -132,10 +112,7 @@ func (o *componentHead) Dependencies() []string {
}

func (o *componentHead) SetDependencies(d []string) error {
o.m.RLock()
defer o.m.RUnlock()

if o.x == nil {
if o == nil {
return ErrorComponentNotInitialized.Error(nil)
} else {
o.x.Store(keyCptDependencies, d)
Expand Down
14 changes: 8 additions & 6 deletions config/components/head/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
package head

import (
"sync"
"sync/atomic"

libctx "github.com/nabbar/golib/context"

libcfg "github.com/nabbar/golib/config"
cfgtps "github.com/nabbar/golib/config/types"
Expand All @@ -41,19 +43,19 @@ type ComponentHead interface {
SetHeaders(head librtr.Headers)
}

func New() ComponentHead {
func New(ctx libctx.FuncContext) ComponentHead {
return &componentHead{
m: sync.RWMutex{},
h: nil,
x: libctx.NewConfig[uint8](ctx),
h: new(atomic.Value),
}
}

func Register(cfg libcfg.Config, key string, cpt ComponentHead) {
cfg.ComponentSet(key, cpt)
}

func RegisterNew(cfg libcfg.Config, key string) {
cfg.ComponentSet(key, New())
func RegisterNew(ctx libctx.FuncContext, cfg libcfg.Config, key string) {
cfg.ComponentSet(key, New(ctx))
}

func Load(getCpt cfgtps.FuncCptGet, key string) ComponentHead {
Expand Down
23 changes: 13 additions & 10 deletions config/components/head/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,31 @@
package head

import (
"sync"
"sync/atomic"

libctx "github.com/nabbar/golib/context"
librtr "github.com/nabbar/golib/router/header"
)

type componentHead struct {
m sync.RWMutex
x libctx.Config[uint8]
h librtr.Headers
h *atomic.Value // librtr.Headers
}

func (o *componentHead) GetHeaders() librtr.Headers {
o.m.RLock()
defer o.m.RUnlock()

return o.h
if i := o.h.Load(); i == nil {
return librtr.NewHeaders()
} else if v, k := i.(librtr.Headers); !k {
return librtr.NewHeaders()
} else {
return v
}
}

func (o *componentHead) SetHeaders(head librtr.Headers) {
o.m.Lock()
defer o.m.Unlock()
if head == nil {
head = librtr.NewHeaders()
}

o.h = head
o.h.Store(head)
}
2 changes: 1 addition & 1 deletion config/components/request/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (o *componentRequest) RegisterFuncReload(before, after cfgtps.FuncCptEvent)
}

func (o *componentRequest) IsStarted() bool {
return o != nil && o.r != nil
return o.getRequest() != nil
}

func (o *componentRequest) IsRunning() bool {
Expand Down
14 changes: 12 additions & 2 deletions ldap/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,13 @@ func (lc *HelperLDAP) ForceTLSMode(tlsMode TLSMode, tlsConfig *tls.Config) {

func (lc *HelperLDAP) dialTLS() (*ldap.Conn, liberr.Error) {
d := net.Dialer{}
adr := lc.config.ServerAddr(true)

c, err := d.DialContext(lc.ctx, "tcp", lc.config.ServerAddr(true))
if len(adr) < 3 {
return nil, ErrorLDAPServerTLS.Error(fmt.Errorf("invalid port for LDAPS"))
}

c, err := d.DialContext(lc.ctx, "tcp", adr)

if err != nil {
if c != nil {
Expand Down Expand Up @@ -218,8 +223,13 @@ func (lc *HelperLDAP) dialTLS() (*ldap.Conn, liberr.Error) {

func (lc *HelperLDAP) dial() (*ldap.Conn, liberr.Error) {
d := net.Dialer{}
adr := lc.config.ServerAddr(false)

if len(adr) < 3 {
return nil, ErrorLDAPServerTLS.Error(fmt.Errorf("invalid port for LDAP / LDAP+STARTLS"))
}

c, err := d.DialContext(lc.ctx, "tcp", lc.config.ServerAddr(false))
c, err := d.DialContext(lc.ctx, "tcp", adr)

if err != nil {
if c != nil {
Expand Down
6 changes: 4 additions & 2 deletions ldap/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ func (cnf Config) BaseDN() string {
}

func (cnf Config) ServerAddr(withTls bool) string {
if withTls {
if withTls && cnf.Portldaps > 0 {
return fmt.Sprintf("%s:%d", cnf.Uri, cnf.Portldaps)
} else if !withTls && cnf.PortLdap > 0 {
return fmt.Sprintf("%s:%d", cnf.Uri, cnf.PortLdap)
}

return fmt.Sprintf("%s:%d", cnf.Uri, cnf.PortLdap)
return ""
}

func (cnf Config) PatternFilterGroup() string {
Expand Down

0 comments on commit cce6ba5

Please sign in to comment.