]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/google.golang.org/api/internal/pool.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / google.golang.org / api / internal / pool.go
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package internal
16
17 import (
18 "errors"
19
20 "google.golang.org/grpc/naming"
21 )
22
23 // PoolResolver provides a fixed list of addresses to load balance between
24 // and does not provide further updates.
25 type PoolResolver struct {
26 poolSize int
27 dialOpt *DialSettings
28 ch chan []*naming.Update
29 }
30
31 // NewPoolResolver returns a PoolResolver
32 // This is an EXPERIMENTAL API and may be changed or removed in the future.
33 func NewPoolResolver(size int, o *DialSettings) *PoolResolver {
34 return &PoolResolver{poolSize: size, dialOpt: o}
35 }
36
37 // Resolve returns a Watcher for the endpoint defined by the DialSettings
38 // provided to NewPoolResolver.
39 func (r *PoolResolver) Resolve(target string) (naming.Watcher, error) {
40 if r.dialOpt.Endpoint == "" {
41 return nil, errors.New("No endpoint configured")
42 }
43 addrs := make([]*naming.Update, 0, r.poolSize)
44 for i := 0; i < r.poolSize; i++ {
45 addrs = append(addrs, &naming.Update{Op: naming.Add, Addr: r.dialOpt.Endpoint, Metadata: i})
46 }
47 r.ch = make(chan []*naming.Update, 1)
48 r.ch <- addrs
49 return r, nil
50 }
51
52 // Next returns a static list of updates on the first call,
53 // and blocks indefinitely until Close is called on subsequent calls.
54 func (r *PoolResolver) Next() ([]*naming.Update, error) {
55 return <-r.ch, nil
56 }
57
58 // Close releases resources associated with the pool and causes Next to unblock.
59 func (r *PoolResolver) Close() {
60 close(r.ch)
61 }