-
Notifications
You must be signed in to change notification settings - Fork 355
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
Connection pooling in Dial to server #383
Comments
As of now, there's no method implemented for connection pooling. You can setup a simple connection pool yourself with just an array or even a channel. The one thing that it hard to manage is to determine when a connection has been abandoned by the directory server. On Active Directory for example, when enabled the server can simply close the underyling TCP connection. This ends the To put it simply: Before using a pooled connection, run package main
import (
"fmt"
"github.com/go-ldap/ldap"
"log"
"sync"
)
// This channel will later hold all pooled connections
// 1024 is the max. number of objects the channel can hold
// until it blocks.
var connectionPool = make(chan *ldap.Conn, 1024)
func getConnection(dialURL string) (conn *ldap.Conn, err error) {
select {
case conn = <-connectionPool:
if conn.IsClosing() {
// The connection is already closing and unusable at this point
// Retry one more time
return getConnection(dialURL)
}
default:
// When there's no object to retrieve from the channel, create
// a new one
conn, err = ldap.DialURL(dialURL)
}
return
}
func putConnection(conn *ldap.Conn) {
select {
case connectionPool <- conn:
// Try to put the connection back into the pool
default:
// Connection pool is full, close and discard connection
fmt.Println("Connection closed since pool is full")
conn.Close()
}
}
func main() {
wg := &sync.WaitGroup{}
wg.Add(5)
for n := 0; n < 5; n++ {
go func(n int) {
for i := 0; i < 1024; i++ {
search()
}
wg.Done()
}(n)
}
wg.Wait()
log.Printf("%#v len: %d", connectionPool, len(connectionPool))
}
func search() {
conn, err := getConnection("ldap://192.168.1.1:389")
if err != nil {
log.Fatalln(err)
}
defer putConnection(conn)
conn.Bind("[email protected]", "")
type User struct {
DN string `ldap:"dn"`
CN string `ldap:"cn"`
UserAccountControl int `ldap:"userAccountControl"`
}
result, err := conn.Search(&ldap.SearchRequest{
BaseDN: "dc=pusch,dc=local",
Scope: ldap.ScopeWholeSubtree,
DerefAliases: ldap.NeverDerefAliases,
Filter: "([email protected])",
Attributes: []string{"cn", "mail", "userAccountControl"},
})
if err != nil {
log.Fatalln(err)
}
targetUser := &User{}
if err = result.Entries[0].Unmarshal(targetUser); err != nil {
log.Fatalln(err)
}
} |
Looking forward to progress in this regard |
Thanks @eryajf . You can close this request |
Is there any elegant plan? |
@mirza-ali-ctct Now, you can try this. https://github.com/eryajf/ldapool |
Is it possible to allow connection in the
Dial
call or is there an equivalent function for connection pooling when making ldap calls?The text was updated successfully, but these errors were encountered: