1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
10 "git.arvados.org/arvados.git/sdk/go/arvados"
13 // A ClientPool is a pool of ArvadosClients. This is useful for
14 // applications that make API calls using a dynamic set of tokens,
15 // like web services that pass through their own clients'
16 // credentials. See arvados-git-httpd for an example, and sync.Pool
17 // for more information about garbage collection.
18 type ClientPool struct {
19 // Initialize new clients by copying this one.
20 Prototype *ArvadosClient
27 // MakeClientPool returns a new empty ClientPool, using environment
28 // variables to initialize the prototype.
29 func MakeClientPool() *ClientPool {
30 return MakeClientPoolWith(nil)
33 // MakeClientPoolWith returns a new empty ClientPool with a previously
34 // initialized arvados.Client.
35 func MakeClientPoolWith(client *arvados.Client) *ClientPool {
37 var proto *ArvadosClient
40 proto, err = MakeArvadosClient()
42 proto, err = New(client)
50 func (p *ClientPool) setup() {
51 p.pool = &sync.Pool{New: func() interface{} {
60 // Err returns the error that was encountered last time Get returned
62 func (p *ClientPool) Err() error {
66 // Get returns an ArvadosClient taken from the pool, or a new one if
67 // the pool is empty. If an existing client is returned, its state
68 // (including its ApiToken) will be just as it was when it was Put
70 func (p *ClientPool) Get() *ArvadosClient {
71 p.setupOnce.Do(p.setup)
72 c, ok := p.pool.Get().(*ArvadosClient)
79 // Put puts an ArvadosClient back in the pool.
80 func (p *ClientPool) Put(c *ArvadosClient) {
81 p.setupOnce.Do(p.setup)