Package Config:

- 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
This commit is contained in:
Nicolas JUHEL
2024-03-27 17:33:32 +01:00
parent eed9a8e160
commit cce6ba5348
7 changed files with 42 additions and 66 deletions

View File

@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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
}
}

View File

@@ -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)
@@ -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 {
@@ -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 {
@@ -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)

View File

@@ -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"
@@ -41,10 +43,10 @@ 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),
}
}
@@ -52,8 +54,8 @@ 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 {

View File

@@ -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)
}

View File

@@ -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 {

View File

@@ -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 {
@@ -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)
c, err := d.DialContext(lc.ctx, "tcp", 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", adr)
if err != nil {
if c != nil {

View File

@@ -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 {