1// Copyright 2016 Google Inc. All Rights Reserved. 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 15package internal 16 17import ( 18 "errors" 19 "google.golang.org/grpc/naming" 20) 21 22// PoolResolver provides a fixed list of addresses to load balance between 23// and does not provide further updates. 24type PoolResolver struct { 25 poolSize int 26 dialOpt *DialSettings 27 ch chan []*naming.Update 28} 29 30// NewPoolResolver returns a PoolResolver 31// This is an EXPERIMENTAL API and may be changed or removed in the future. 32func NewPoolResolver(size int, o *DialSettings) *PoolResolver { 33 return &PoolResolver{poolSize: size, dialOpt: o} 34} 35 36// Resolve returns a Watcher for the endpoint defined by the DialSettings 37// provided to NewPoolResolver. 38func (r *PoolResolver) Resolve(target string) (naming.Watcher, error) { 39 if r.dialOpt.Endpoint == "" { 40 return nil, errors.New("No endpoint configured") 41 } 42 addrs := make([]*naming.Update, 0, r.poolSize) 43 for i := 0; i < r.poolSize; i++ { 44 addrs = append(addrs, &naming.Update{Op: naming.Add, Addr: r.dialOpt.Endpoint, Metadata: i}) 45 } 46 r.ch = make(chan []*naming.Update, 1) 47 r.ch <- addrs 48 return r, nil 49} 50 51// Next returns a static list of updates on the first call, 52// and blocks indefinitely until Close is called on subsequent calls. 53func (r *PoolResolver) Next() ([]*naming.Update, error) { 54 return <-r.ch, nil 55} 56 57func (r *PoolResolver) Close() { 58 close(r.ch) 59} 60