From e4d966744d462d0b0d47dcf5867aa339fc247ae5 Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Thu, 10 Jul 2014 22:36:06 -0700 Subject: [PATCH] proxy: fixing linting errors. --- pkg/proxy/doc.go | 2 +- pkg/proxy/loadbalancer.go | 2 ++ pkg/proxy/proxier.go | 6 +++++- pkg/proxy/roundrobbin.go | 15 +++++++++++++-- pkg/proxy/roundrobbin_test.go | 2 +- 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/pkg/proxy/doc.go b/pkg/proxy/doc.go index e29caf01ef..7e209b6650 100644 --- a/pkg/proxy/doc.go +++ b/pkg/proxy/doc.go @@ -14,5 +14,5 @@ See the License for the specific language governing permissions and limitations under the License. */ -// Package proxy implements the layer-3 network proxy +// Package proxy implements the layer-3 network proxy. package proxy diff --git a/pkg/proxy/loadbalancer.go b/pkg/proxy/loadbalancer.go index 6481ee30bf..6771d8bfcc 100644 --- a/pkg/proxy/loadbalancer.go +++ b/pkg/proxy/loadbalancer.go @@ -22,6 +22,8 @@ import ( "net" ) +// LoadBalancer represents a load balancer that decides where to route +// the incoming services for a particular service to. type LoadBalancer interface { // LoadBalance takes an incoming request and figures out where to route it to. // Determination is based on destination service (for example, 'mysql') as diff --git a/pkg/proxy/proxier.go b/pkg/proxy/proxier.go index 6c1a045a76..5e781456d5 100644 --- a/pkg/proxy/proxier.go +++ b/pkg/proxy/proxier.go @@ -33,10 +33,12 @@ type Proxier struct { serviceMap map[string]int } +// NewProxier returns a new Proxier. func NewProxier(loadBalancer LoadBalancer) *Proxier { return &Proxier{loadBalancer: loadBalancer, serviceMap: make(map[string]int)} } +// CopyBytes copies bytes from in to out until EOF. func CopyBytes(in, out *net.TCPConn) { glog.Infof("Copying from %v <-> %v <-> %v <-> %v", in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr()) @@ -49,7 +51,8 @@ func CopyBytes(in, out *net.TCPConn) { out.CloseWrite() } -// Create a bidirectional byte shuffler. Copies bytes to/from each connection. +// ProxyConnection creates a bidirectional byte shuffler. +// Copies bytes to/from each connection. func ProxyConnection(in, out *net.TCPConn) { glog.Infof("Creating proxy between %v <-> %v <-> %v <-> %v", in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr()) @@ -117,6 +120,7 @@ func (proxier Proxier) addServiceCommon(service string, l net.Listener) { go proxier.AcceptHandler(service, l) } +// OnUpdate handles update notices for the updated services. func (proxier Proxier) OnUpdate(services []api.Service) { glog.Infof("Received update notice: %+v", services) for _, service := range services { diff --git a/pkg/proxy/roundrobbin.go b/pkg/proxy/roundrobbin.go index 9d9f241354..a55d812ae6 100644 --- a/pkg/proxy/roundrobbin.go +++ b/pkg/proxy/roundrobbin.go @@ -30,22 +30,28 @@ import ( "github.com/golang/glog" ) +// LoadBalancerRR is a round-robin load balancer. type LoadBalancerRR struct { - lock sync.RWMutex endpointsMap map[string][]string rrIndex map[string]int + + lock sync.RWMutex } +// NewLoadBalancerRR returns a new LoadBalancerRR. func NewLoadBalancerRR() *LoadBalancerRR { return &LoadBalancerRR{endpointsMap: make(map[string][]string), rrIndex: make(map[string]int)} } +// LoadBalance registers srcAddr for the provided service. +// It returns error if no entry is available for the service +// or no previous endpoints are registered. func (impl LoadBalancerRR) LoadBalance(service string, srcAddr net.Addr) (string, error) { impl.lock.RLock() endpoints, exists := impl.endpointsMap[service] index := impl.rrIndex[service] impl.lock.RUnlock() - if exists == false { + if !exists { return "", errors.New("no service entry for:" + service) } if len(endpoints) == 0 { @@ -56,6 +62,7 @@ func (impl LoadBalancerRR) LoadBalance(service string, srcAddr net.Addr) (string return endpoint, nil } +// IsValid returns true if spec is valid. func (impl LoadBalancerRR) IsValid(spec string) bool { index := strings.Index(spec, ":") if index == -1 { @@ -68,6 +75,7 @@ func (impl LoadBalancerRR) IsValid(spec string) bool { return value > 0 } +// FilterValidEndpoints filters out invalid endpoints. func (impl LoadBalancerRR) FilterValidEndpoints(endpoints []string) []string { var result []string for _, spec := range endpoints { @@ -78,6 +86,9 @@ func (impl LoadBalancerRR) FilterValidEndpoints(endpoints []string) []string { return result } +// OnUpdate updates the registered endpoints with the new +// endpoint information, removes the registered endpoints +// no longer present in the provided endpoints. func (impl LoadBalancerRR) OnUpdate(endpoints []api.Endpoints) { tmp := make(map[string]bool) impl.lock.Lock() diff --git a/pkg/proxy/roundrobbin_test.go b/pkg/proxy/roundrobbin_test.go index b4af90a981..216cdbd2de 100644 --- a/pkg/proxy/roundrobbin_test.go +++ b/pkg/proxy/roundrobbin_test.go @@ -59,7 +59,7 @@ func TestLoadBalanceFilterWorks(t *testing.T) { func TestLoadBalanceFailsWithNoEndpoints(t *testing.T) { loadBalancer := NewLoadBalancerRR() - endpoints := make([]api.Endpoints, 0) + var endpoints []api.Endpoints loadBalancer.OnUpdate(endpoints) endpoint, err := loadBalancer.LoadBalance("foo", nil) if err == nil {