Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix request timeout handling #388

Merged
merged 2 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const (
type Conn struct {
// requestTimeout is loaded atomically
// so we need to ensure 64-bit alignment on 32-bit platforms.
requestTimeout int64
requestTimeout time.Duration
cpuschma marked this conversation as resolved.
Show resolved Hide resolved
conn net.Conn
isTLS bool
closing uint32
Expand Down Expand Up @@ -281,9 +281,7 @@ func (l *Conn) Close() {

// SetTimeout sets the time after a request is sent that a MessageTimeout triggers
func (l *Conn) SetTimeout(timeout time.Duration) {
if timeout > 0 {
atomic.StoreInt64(&l.requestTimeout, int64(timeout))
}
l.requestTimeout = timeout
}

// Returns the next available messageID
Expand Down Expand Up @@ -486,20 +484,26 @@ func (l *Conn) processMessages() {
l.messageContexts[message.MessageID] = message.Context

// Add timeout if defined
requestTimeout := time.Duration(atomic.LoadInt64(&l.requestTimeout))
if requestTimeout > 0 {
if l.requestTimeout > 0 {
go func() {
timer := time.NewTimer(l.requestTimeout)
defer func() {
if err := recover(); err != nil {
logger.Printf("ldap: recovered panic in RequestTimeout: %v", err)
}

timer.Stop()
}()
time.Sleep(requestTimeout)
timeoutMessage := &messagePacket{
Op: MessageTimeout,
MessageID: message.MessageID,

select {
case <-timer.C:
timeoutMessage := &messagePacket{
Op: MessageTimeout,
MessageID: message.MessageID,
}
l.sendProcessMessage(timeoutMessage)
case <-message.Context.done:
}
l.sendProcessMessage(timeoutMessage)
}()
}
case MessageResponse:
Expand Down
26 changes: 15 additions & 11 deletions v3/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const (
type Conn struct {
// requestTimeout is loaded atomically
// so we need to ensure 64-bit alignment on 32-bit platforms.
requestTimeout int64
requestTimeout time.Duration
conn net.Conn
isTLS bool
closing uint32
Expand Down Expand Up @@ -281,9 +281,7 @@ func (l *Conn) Close() {

// SetTimeout sets the time after a request is sent that a MessageTimeout triggers
func (l *Conn) SetTimeout(timeout time.Duration) {
if timeout > 0 {
atomic.StoreInt64(&l.requestTimeout, int64(timeout))
}
l.requestTimeout = timeout
}

// Returns the next available messageID
Expand Down Expand Up @@ -486,20 +484,26 @@ func (l *Conn) processMessages() {
l.messageContexts[message.MessageID] = message.Context

// Add timeout if defined
requestTimeout := time.Duration(atomic.LoadInt64(&l.requestTimeout))
if requestTimeout > 0 {
if l.requestTimeout > 0 {
go func() {
timer := time.NewTimer(l.requestTimeout)
defer func() {
if err := recover(); err != nil {
logger.Printf("ldap: recovered panic in RequestTimeout: %v", err)
}

timer.Stop()
}()
time.Sleep(requestTimeout)
timeoutMessage := &messagePacket{
Op: MessageTimeout,
MessageID: message.MessageID,

select {
case <-timer.C:
timeoutMessage := &messagePacket{
Op: MessageTimeout,
MessageID: message.MessageID,
}
l.sendProcessMessage(timeoutMessage)
case <-message.Context.done:
}
l.sendProcessMessage(timeoutMessage)
}()
}
case MessageResponse:
Expand Down