1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
11 // A ClientPool is a pool of ArvadosClients. This is useful for
12 // applications that make API calls using a dynamic set of tokens,
13 // like web services that pass through their own clients'
14 // credentials. See arvados-git-httpd for an example, and sync.Pool
15 // for more information about garbage collection.
16 type ClientPool struct {
17 // Initialize new clients by coping this one.
18 Prototype *ArvadosClient
25 // MakeClientPool returns a new empty ClientPool, using environment
26 // variables to initialize the prototype.
27 func MakeClientPool() *ClientPool {
28 proto, err := MakeArvadosClient()
35 func (p *ClientPool) setup() {
36 p.pool = &sync.Pool{New: func() interface{} {
45 // Err returns the error that was encountered last time Get returned
47 func (p *ClientPool) Err() error {
51 // Get returns an ArvadosClient taken from the pool, or a new one if
52 // the pool is empty. If an existing client is returned, its state
53 // (including its ApiToken) will be just as it was when it was Put
55 func (p *ClientPool) Get() *ArvadosClient {
56 p.setupOnce.Do(p.setup)
57 c, ok := p.pool.Get().(*ArvadosClient)
64 // Put puts an ArvadosClient back in the pool.
65 func (p *ClientPool) Put(c *ArvadosClient) {
66 p.setupOnce.Do(p.setup)