-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add search asynchronously with context (#440)
* feat: add search with channels inspired by #319 * refactor: fix to check proper test results #319 * refactor: fix to use unpackAttributes() for Attributes #319 * refactor: returns receive-only channel to prevent closing it from the caller #319 * refactor: pass channelSize to be able to controll buffered channel by the caller #319 * fix: recover an asynchronouse closing timing issue #319 * fix: consume all entries from the channel to prevent blocking by the connection #319 * feat: add initial search async function with channel #341 * feat: provide search async function and drop search with channels #319 #341 * refactor: lock when to call GetLastError since it might be in communication
- Loading branch information
Showing
12 changed files
with
632 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
package ldap | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
ber "github.com/go-asn1-ber/asn1-ber" | ||
) | ||
|
||
// Response defines an interface to get data from an LDAP server | ||
type Response interface { | ||
Entry() *Entry | ||
Referral() string | ||
Controls() []Control | ||
Err() error | ||
Next() bool | ||
} | ||
|
||
type searchResponse struct { | ||
conn *Conn | ||
ch chan *SearchSingleResult | ||
|
||
entry *Entry | ||
referral string | ||
controls []Control | ||
err error | ||
} | ||
|
||
// Entry returns an entry from the given search request | ||
func (r *searchResponse) Entry() *Entry { | ||
return r.entry | ||
} | ||
|
||
// Referral returns a referral from the given search request | ||
func (r *searchResponse) Referral() string { | ||
return r.referral | ||
} | ||
|
||
// Controls returns controls from the given search request | ||
func (r *searchResponse) Controls() []Control { | ||
return r.controls | ||
} | ||
|
||
// Err returns an error when the given search request was failed | ||
func (r *searchResponse) Err() error { | ||
return r.err | ||
} | ||
|
||
// Next returns whether next data exist or not | ||
func (r *searchResponse) Next() bool { | ||
res, ok := <-r.ch | ||
if !ok { | ||
return false | ||
} | ||
if res == nil { | ||
return false | ||
} | ||
r.err = res.Error | ||
if r.err != nil { | ||
return false | ||
} | ||
r.err = r.conn.GetLastError() | ||
if r.err != nil { | ||
return false | ||
} | ||
r.entry = res.Entry | ||
r.referral = res.Referral | ||
r.controls = res.Controls | ||
return true | ||
} | ||
|
||
func (r *searchResponse) start(ctx context.Context, searchRequest *SearchRequest) { | ||
go func() { | ||
defer func() { | ||
close(r.ch) | ||
if err := recover(); err != nil { | ||
r.conn.err = fmt.Errorf("ldap: recovered panic in searchResponse: %v", err) | ||
} | ||
}() | ||
|
||
if r.conn.IsClosing() { | ||
return | ||
} | ||
|
||
packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request") | ||
packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, r.conn.nextMessageID(), "MessageID")) | ||
// encode search request | ||
err := searchRequest.appendTo(packet) | ||
if err != nil { | ||
r.ch <- &SearchSingleResult{Error: err} | ||
return | ||
} | ||
r.conn.Debug.PrintPacket(packet) | ||
|
||
msgCtx, err := r.conn.sendMessage(packet) | ||
if err != nil { | ||
r.ch <- &SearchSingleResult{Error: err} | ||
return | ||
} | ||
defer r.conn.finishMessage(msgCtx) | ||
|
||
foundSearchSingleResultDone := false | ||
for !foundSearchSingleResultDone { | ||
select { | ||
case <-ctx.Done(): | ||
r.conn.Debug.Printf("%d: %s", msgCtx.id, ctx.Err().Error()) | ||
return | ||
default: | ||
r.conn.Debug.Printf("%d: waiting for response", msgCtx.id) | ||
packetResponse, ok := <-msgCtx.responses | ||
if !ok { | ||
err := NewError(ErrorNetwork, errors.New("ldap: response channel closed")) | ||
r.ch <- &SearchSingleResult{Error: err} | ||
return | ||
} | ||
packet, err = packetResponse.ReadPacket() | ||
r.conn.Debug.Printf("%d: got response %p", msgCtx.id, packet) | ||
if err != nil { | ||
r.ch <- &SearchSingleResult{Error: err} | ||
return | ||
} | ||
|
||
if r.conn.Debug { | ||
if err := addLDAPDescriptions(packet); err != nil { | ||
r.ch <- &SearchSingleResult{Error: err} | ||
return | ||
} | ||
ber.PrintPacket(packet) | ||
} | ||
|
||
switch packet.Children[1].Tag { | ||
case ApplicationSearchResultEntry: | ||
r.ch <- &SearchSingleResult{ | ||
Entry: &Entry{ | ||
DN: packet.Children[1].Children[0].Value.(string), | ||
Attributes: unpackAttributes(packet.Children[1].Children[1].Children), | ||
}, | ||
} | ||
|
||
case ApplicationSearchResultDone: | ||
if err := GetLDAPError(packet); err != nil { | ||
r.ch <- &SearchSingleResult{Error: err} | ||
return | ||
} | ||
if len(packet.Children) == 3 { | ||
result := &SearchSingleResult{} | ||
for _, child := range packet.Children[2].Children { | ||
decodedChild, err := DecodeControl(child) | ||
if err != nil { | ||
werr := fmt.Errorf("failed to decode child control: %w", err) | ||
r.ch <- &SearchSingleResult{Error: werr} | ||
return | ||
} | ||
result.Controls = append(result.Controls, decodedChild) | ||
} | ||
r.ch <- result | ||
} | ||
foundSearchSingleResultDone = true | ||
|
||
case ApplicationSearchResultReference: | ||
ref := packet.Children[1].Children[0].Value.(string) | ||
r.ch <- &SearchSingleResult{Referral: ref} | ||
} | ||
} | ||
} | ||
r.conn.Debug.Printf("%d: returning", msgCtx.id) | ||
}() | ||
} | ||
|
||
func newSearchResponse(conn *Conn, bufferSize int) *searchResponse { | ||
var ch chan *SearchSingleResult | ||
if bufferSize > 0 { | ||
ch = make(chan *SearchSingleResult, bufferSize) | ||
} else { | ||
ch = make(chan *SearchSingleResult) | ||
} | ||
return &searchResponse{ | ||
conn: conn, | ||
ch: ch, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.