Skip to content

Commit

Permalink
Fix: Remove closed-channel error handling
Browse files Browse the repository at this point in the history
Eliminate redundant select statements for closed-channel error handling across LDAP and Lua operations. This simplifies channel communication by directly sending requests and replies, assuming open channels. The unnecessary error definitions and imports related to closed channels are also removed.

Signed-off-by: Christian Roessner <c@roessner.co>
  • Loading branch information
Christian Roessner committed Sep 26, 2024
1 parent 13a203a commit f490fda
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 68 deletions.
33 changes: 6 additions & 27 deletions server/backend/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -1196,10 +1196,7 @@ func (l *LDAPConnection) modifyAdd(ldapRequest *LDAPRequest) (err error) {
// Then it sets the state of the connection to global.LDAPStateFree.
// Finally, it unlocks the state of the connection using the ldapPool.conn[index].Mu.Unlock() method.
func sendLDAPReplyAndUnlockState[T PoolRequest[T]](ldapPool *LDAPPool, index int, request T, ldapReply *LDAPReply) {
select {
case request.GetLDAPReplyChan() <- ldapReply:
default:
}
request.GetLDAPReplyChan() <- ldapReply

ldapPool.conn[index].Mu.Lock()

Expand Down Expand Up @@ -1297,10 +1294,7 @@ func (l *LDAPPool) proccessLookupRequest(index int, ldapRequest *LDAPRequest, ld
ldapReply := &LDAPReply{}

if ldapReply.Err = l.checkConnection(ldapRequest.GUID, index); ldapReply.Err != nil {
select {
case ldapRequest.LDAPReplyChan <- ldapReply:
default:
}
ldapRequest.LDAPReplyChan <- ldapReply

return
}
Expand Down Expand Up @@ -1362,10 +1356,7 @@ func LDAPMainWorker(ctx context.Context) {
case ldapRequest := <-LDAPRequestChan:
// Check that we have enough idle connections.
if err := ldapPool.setIdleConnections(true); err != nil {
select {
case ldapRequest.LDAPReplyChan <- &LDAPReply{Err: err}:
default:
}
ldapRequest.LDAPReplyChan <- &LDAPReply{Err: err}
}

ldapPool.handleLookupRequest(ldapRequest, &ldapWaitGroup)
Expand Down Expand Up @@ -1414,10 +1405,7 @@ func (l *LDAPPool) processAuthRequest(index int, ldapAuthRequest *LDAPAuthReques
ldapReply := &LDAPReply{}

if ldapReply.Err = l.checkConnection(ldapAuthRequest.GUID, index); ldapReply.Err != nil {
select {
case ldapAuthRequest.LDAPReplyChan <- ldapReply:
default:
}
ldapAuthRequest.LDAPReplyChan <- ldapReply

return
}
Expand Down Expand Up @@ -1472,10 +1460,7 @@ func LDAPAuthWorker(ctx context.Context) {
case ldapAuthRequest := <-LDAPAuthRequestChan:
// Check that we have enough idle connections.
if err := ldapPool.setIdleConnections(false); err != nil {
select {
case ldapAuthRequest.LDAPReplyChan <- &LDAPReply{Err: err}:
default:
}
ldapAuthRequest.LDAPReplyChan <- &LDAPReply{Err: err}
}

ldapPool.handleAuthRequest(ldapAuthRequest, &ldapWaitGroup)
Expand Down Expand Up @@ -1527,13 +1512,7 @@ func LuaLDAPSearch(ctx context.Context) lua.LGFunction {

ldapRequest := createLDAPRequest(fieldValues, scope, ctx)

select {
case LDAPRequestChan <- ldapRequest:
default:
L.RaiseError(errors.ErrClosedChannel.Error())

return 1
}
LDAPRequestChan <- ldapRequest

return processReply(L, ldapRequest.GetLDAPReplyChan())
}
Expand Down
5 changes: 1 addition & 4 deletions server/backend/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,8 @@ func processError(err error, luaRequest *LuaRequest, logs *lualib.CustomLogKeyVa
global.LogKeyError, err,
)

select {
case luaRequest.LuaReplyChan <- &lualib.LuaBackendResult{
luaRequest.LuaReplyChan <- &lualib.LuaBackendResult{
Err: err,
Logs: logs,
}:
default:
}
}
24 changes: 4 additions & 20 deletions server/core/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,7 @@ func ldapPassDB(auth *AuthState) (passDBResult *PassDBResult, err error) {
}

// Find user with account status enabled
select {
case backend.LDAPRequestChan <- ldapRequest:
default:
return passDBResult, errors.ErrClosedChannel
}
backend.LDAPRequestChan <- ldapRequest

ldapReply = <-ldapReplyChan

Expand Down Expand Up @@ -238,11 +234,7 @@ func ldapPassDB(auth *AuthState) (passDBResult *PassDBResult, err error) {
HTTPClientContext: auth.HTTPClientContext,
}

select {
case backend.LDAPAuthRequestChan <- ldapUserBindRequest:
default:
return passDBResult, errors.ErrClosedChannel
}
backend.LDAPAuthRequestChan <- ldapUserBindRequest

ldapReply = <-ldapReplyChan

Expand Down Expand Up @@ -341,11 +333,7 @@ func ldapAccountDB(auth *AuthState) (accounts AccountList, err error) {
}

// Find user with account status enabled
select {
case backend.LDAPRequestChan <- ldapRequest:
default:
return accounts, errors.ErrClosedChannel
}
backend.LDAPRequestChan <- ldapRequest

ldapReply = <-ldapReplyChan

Expand Down Expand Up @@ -436,11 +424,7 @@ func ldapAddTOTPSecret(auth *AuthState, totp *TOTPSecret) (err error) {
ldapRequest.ModifyAttributes = make(backend.LDAPModifyAttributes, 2)
ldapRequest.ModifyAttributes[configField] = []string{totp.getValue()}

select {
case backend.LDAPRequestChan <- ldapRequest:
default:
return errors.ErrClosedChannel
}
backend.LDAPRequestChan <- ldapRequest

ldapReply = <-ldapReplyChan

Expand Down
19 changes: 3 additions & 16 deletions server/core/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package core
import (
"github.com/croessner/nauthilus/server/backend"
"github.com/croessner/nauthilus/server/config"
"github.com/croessner/nauthilus/server/errors"
"github.com/croessner/nauthilus/server/global"
"github.com/croessner/nauthilus/server/lualib"
"github.com/croessner/nauthilus/server/stats"
Expand Down Expand Up @@ -91,11 +90,7 @@ func luaPassDB(auth *AuthState) (passDBResult *PassDBResult, err error) {
},
}

select {
case backend.LuaRequestChan <- luaRequest:
default:
return passDBResult, errors.ErrClosedChannel
}
backend.LuaRequestChan <- luaRequest

luaBackendResult = <-luaReplyChan

Expand Down Expand Up @@ -183,11 +178,7 @@ func luaAccountDB(auth *AuthState) (accounts AccountList, err error) {
},
}

select {
case backend.LuaRequestChan <- luaRequest:
default:
return accounts, errors.ErrClosedChannel
}
backend.LuaRequestChan <- luaRequest

luaBackendResult = <-luaReplyChan

Expand Down Expand Up @@ -241,11 +232,7 @@ func luaAddTOTPSecret(auth *AuthState, totp *TOTPSecret) (err error) {
},
}

select {
case backend.LuaRequestChan <- luaRequest:
default:
return errors.ErrClosedChannel
}
backend.LuaRequestChan <- luaRequest

luaBackendResult = <-luaReplyChan

Expand Down
1 change: 0 additions & 1 deletion server/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ var (
// common.

var (
ErrClosedChannel = errors.New("channel closed")
ErrNoPassDBResult = errors.New("no pass Database result")
ErrUnknownCause = errors.New("something went wrong")
)
Expand Down

0 comments on commit f490fda

Please sign in to comment.