Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
go-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ai3
go-common
Commits
51087510
Commit
51087510
authored
7 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Add the Add() and Modify() methods
parent
30e27c15
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
ldap/pool.go
+76
-0
76 additions, 0 deletions
ldap/pool.go
ldap/search.go
+0
-63
0 additions, 63 deletions
ldap/search.go
with
76 additions
and
63 deletions
ldap/pool.go
+
76
−
0
View file @
51087510
...
...
@@ -7,6 +7,8 @@ import (
"net/url"
"time"
"git.autistici.org/ai3/go-common/clientutil"
"github.com/cenkalti/backoff"
"gopkg.in/ldap.v2"
)
...
...
@@ -125,3 +127,77 @@ func NewConnectionPool(uri, bindDN, bindPw string, cacheSize int) (*ConnectionPo
bindPw
:
bindPw
,
},
nil
}
func
(
p
*
ConnectionPool
)
doRequest
(
ctx
context
.
Context
,
fn
func
(
*
ldap
.
Conn
)
error
)
error
{
return
clientutil
.
Retry
(
func
()
error
{
conn
,
err
:=
p
.
Get
(
ctx
)
if
err
!=
nil
{
// Here conn is nil, so we don't need to Release it.
if
isTemporaryLDAPError
(
err
)
{
return
clientutil
.
TempError
(
err
)
}
return
err
}
if
deadline
,
ok
:=
ctx
.
Deadline
();
ok
{
conn
.
SetTimeout
(
time
.
Until
(
deadline
))
}
err
=
fn
(
conn
)
if
err
!=
nil
&&
isTemporaryLDAPError
(
err
)
{
p
.
Release
(
conn
,
err
)
return
clientutil
.
TempError
(
err
)
}
p
.
Release
(
conn
,
err
)
return
err
},
backoff
.
WithContext
(
clientutil
.
NewExponentialBackOff
(),
ctx
))
}
// Search performs the given search request. It will retry the request
// on temporary errors.
func
(
p
*
ConnectionPool
)
Search
(
ctx
context
.
Context
,
searchRequest
*
ldap
.
SearchRequest
)
(
*
ldap
.
SearchResult
,
error
)
{
var
result
*
ldap
.
SearchResult
err
:=
p
.
doRequest
(
ctx
,
func
(
conn
*
ldap
.
Conn
)
error
{
var
err
error
result
,
err
=
conn
.
Search
(
searchRequest
)
return
err
})
return
result
,
err
}
// Modify issues a ModifyRequest to the LDAP server.
func
(
p
*
ConnectionPool
)
Modify
(
ctx
context
.
Context
,
modifyRequest
*
ldap
.
ModifyRequest
)
error
{
return
p
.
doRequest
(
ctx
,
func
(
conn
*
ldap
.
Conn
)
error
{
return
conn
.
Modify
(
modifyRequest
)
})
}
// Add issues an AddRequest to the LDAP server.
func
(
p
*
ConnectionPool
)
Add
(
ctx
context
.
Context
,
addRequest
*
ldap
.
AddRequest
)
error
{
return
p
.
doRequest
(
ctx
,
func
(
conn
*
ldap
.
Conn
)
error
{
return
conn
.
Add
(
addRequest
)
})
}
// Interface matched by net.Error.
type
hasTemporary
interface
{
Temporary
()
bool
}
// Treat network errors as temporary. Other errors are permanent by
// default.
func
isTemporaryLDAPError
(
err
error
)
bool
{
switch
v
:=
err
.
(
type
)
{
case
*
ldap
.
Error
:
switch
v
.
ResultCode
{
case
ldap
.
ErrorNetwork
:
return
true
default
:
return
false
}
case
hasTemporary
:
return
v
.
Temporary
()
default
:
return
false
}
}
This diff is collapsed.
Click to expand it.
ldap/search.go
deleted
100644 → 0
+
0
−
63
View file @
30e27c15
package
ldaputil
import
(
"context"
"time"
"github.com/cenkalti/backoff"
"gopkg.in/ldap.v2"
"git.autistici.org/ai3/go-common/clientutil"
)
// Interface matched by net.Error.
type
hasTemporary
interface
{
Temporary
()
bool
}
// Treat network errors as temporary. Other errors are permanent by
// default.
func
isTemporaryLDAPError
(
err
error
)
bool
{
switch
v
:=
err
.
(
type
)
{
case
*
ldap
.
Error
:
switch
v
.
ResultCode
{
case
ldap
.
ErrorNetwork
:
return
true
default
:
return
false
}
case
hasTemporary
:
return
v
.
Temporary
()
default
:
return
false
}
}
// Search performs the given search request. It will retry the request
// on temporary errors.
func
(
p
*
ConnectionPool
)
Search
(
ctx
context
.
Context
,
searchRequest
*
ldap
.
SearchRequest
)
(
*
ldap
.
SearchResult
,
error
)
{
var
result
*
ldap
.
SearchResult
err
:=
clientutil
.
Retry
(
func
()
error
{
conn
,
err
:=
p
.
Get
(
ctx
)
if
err
!=
nil
{
// Here conn is nil, so we don't need to Release it.
if
isTemporaryLDAPError
(
err
)
{
return
clientutil
.
TempError
(
err
)
}
return
err
}
if
deadline
,
ok
:=
ctx
.
Deadline
();
ok
{
conn
.
SetTimeout
(
time
.
Until
(
deadline
))
}
result
,
err
=
conn
.
Search
(
searchRequest
)
if
err
!=
nil
&&
isTemporaryLDAPError
(
err
)
{
p
.
Release
(
conn
,
err
)
return
clientutil
.
TempError
(
err
)
}
p
.
Release
(
conn
,
err
)
return
err
},
backoff
.
WithContext
(
clientutil
.
NewExponentialBackOff
(),
ctx
))
return
result
,
err
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment