proxy: fixing linting errors.

pull/6/head
Burcu Dogan 2014-07-10 22:36:06 -07:00
parent f4d989ae92
commit e4d966744d
5 changed files with 22 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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()

View File

@ -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 {