Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
keystore
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
Container Registry
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
id
keystore
Commits
e7b6f531
Commit
e7b6f531
authored
7 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Add a ShardedClient class for partitioned setups
parent
b09f1210
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
client/client.go
+62
-2
62 additions, 2 deletions
client/client.go
with
62 additions
and
2 deletions
client/client.go
+
62
−
2
View file @
e7b6f531
...
...
@@ -3,8 +3,10 @@ package client
import
(
"context"
"crypto/tls"
"fmt"
"net/http"
"net/url"
"sync"
"time"
"git.autistici.org/ai3/go-common/clientutil"
...
...
@@ -12,7 +14,7 @@ import (
"git.autistici.org/id/keystore"
)
// Client for the keystore API.
// Client for the keystore API
(for a specific backend)
.
type
Client
struct
{
*
http
.
Client
backendURL
string
...
...
@@ -24,7 +26,8 @@ type Config struct {
TLSConfig
*
clientutil
.
TLSClientConfig
`yaml:"tls_config"`
}
// New returns a new Client with the given Config.
// New returns a new Client with the given Config. Use this when the
// keystore service runs on a single global instance.
func
New
(
config
*
Config
)
(
*
Client
,
error
)
{
u
,
err
:=
url
.
Parse
(
config
.
BackendURL
)
if
err
!=
nil
{
...
...
@@ -76,3 +79,60 @@ func (c *Client) Close(ctx context.Context, username string) error {
var
resp
keystore
.
CloseResponse
return
clientutil
.
DoJSONHTTPRequest
(
ctx
,
c
.
Client
,
c
.
backendURL
+
"/api/close"
,
&
req
,
&
resp
)
}
// ShardedClient for the keystore API (sharded service).
type
ShardedClient
struct
{
baseURL
*
url
.
URL
tlsConfig
*
tls
.
Config
mx
sync
.
Mutex
shards
map
[
string
]
*
Client
}
// NewSharded creates a ShardedClient for the keystore service. Use it
// when the service is partitioned (sharded) across multiple backends.
func
NewSharded
(
config
*
Config
)
(
*
ShardedClient
,
error
)
{
u
,
err
:=
url
.
Parse
(
config
.
BackendURL
)
if
err
!=
nil
{
return
nil
,
err
}
var
tlsConfig
*
tls
.
Config
if
config
.
TLSConfig
!=
nil
{
tlsConfig
,
err
=
config
.
TLSConfig
.
TLSConfig
()
if
err
!=
nil
{
return
nil
,
err
}
}
return
&
ShardedClient
{
baseURL
:
u
,
tlsConfig
:
tlsConfig
,
shards
:
make
(
map
[
string
]
*
Client
),
},
nil
}
func
(
c
*
ShardedClient
)
getShardURL
(
shard
string
)
*
url
.
URL
{
u
:=
*
c
.
baseURL
u
.
Host
=
fmt
.
Sprintf
(
"%s.%s"
,
shard
,
u
.
Host
)
return
&
u
}
// Shard returns the Client for a specific service shard.
func
(
c
*
ShardedClient
)
Shard
(
shard
string
)
*
Client
{
c
.
mx
.
Lock
()
defer
c
.
mx
.
Unlock
()
client
,
ok
:=
c
.
shards
[
shard
]
if
!
ok
{
u
:=
c
.
getShardURL
(
shard
)
client
=
&
Client
{
Client
:
&
http
.
Client
{
Transport
:
clientutil
.
NewTransport
([]
string
{
u
.
Host
},
c
.
tlsConfig
,
nil
),
Timeout
:
20
*
time
.
Second
,
},
backendURL
:
u
.
String
(),
}
c
.
shards
[
shard
]
=
client
}
return
client
}
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