Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
e4d96674
Commit
e4d96674
authored
Jul 10, 2014
by
Burcu Dogan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
proxy: fixing linting errors.
parent
f4d989ae
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
22 additions
and
5 deletions
+22
-5
doc.go
pkg/proxy/doc.go
+1
-1
loadbalancer.go
pkg/proxy/loadbalancer.go
+2
-0
proxier.go
pkg/proxy/proxier.go
+5
-1
roundrobbin.go
pkg/proxy/roundrobbin.go
+13
-2
roundrobbin_test.go
pkg/proxy/roundrobbin_test.go
+1
-1
No files found.
pkg/proxy/doc.go
View file @
e4d96674
...
@@ -14,5 +14,5 @@ See the License for the specific language governing permissions and
...
@@ -14,5 +14,5 @@ See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/
*/
// Package proxy implements the layer-3 network proxy
// Package proxy implements the layer-3 network proxy
.
package
proxy
package
proxy
pkg/proxy/loadbalancer.go
View file @
e4d96674
...
@@ -22,6 +22,8 @@ import (
...
@@ -22,6 +22,8 @@ import (
"net"
"net"
)
)
// LoadBalancer represents a load balancer that decides where to route
// the incoming services for a particular service to.
type
LoadBalancer
interface
{
type
LoadBalancer
interface
{
// LoadBalance takes an incoming request and figures out where to route it to.
// LoadBalance takes an incoming request and figures out where to route it to.
// Determination is based on destination service (for example, 'mysql') as
// Determination is based on destination service (for example, 'mysql') as
...
...
pkg/proxy/proxier.go
View file @
e4d96674
...
@@ -33,10 +33,12 @@ type Proxier struct {
...
@@ -33,10 +33,12 @@ type Proxier struct {
serviceMap
map
[
string
]
int
serviceMap
map
[
string
]
int
}
}
// NewProxier returns a new Proxier.
func
NewProxier
(
loadBalancer
LoadBalancer
)
*
Proxier
{
func
NewProxier
(
loadBalancer
LoadBalancer
)
*
Proxier
{
return
&
Proxier
{
loadBalancer
:
loadBalancer
,
serviceMap
:
make
(
map
[
string
]
int
)}
return
&
Proxier
{
loadBalancer
:
loadBalancer
,
serviceMap
:
make
(
map
[
string
]
int
)}
}
}
// CopyBytes copies bytes from in to out until EOF.
func
CopyBytes
(
in
,
out
*
net
.
TCPConn
)
{
func
CopyBytes
(
in
,
out
*
net
.
TCPConn
)
{
glog
.
Infof
(
"Copying from %v <-> %v <-> %v <-> %v"
,
glog
.
Infof
(
"Copying from %v <-> %v <-> %v <-> %v"
,
in
.
RemoteAddr
(),
in
.
LocalAddr
(),
out
.
LocalAddr
(),
out
.
RemoteAddr
())
in
.
RemoteAddr
(),
in
.
LocalAddr
(),
out
.
LocalAddr
(),
out
.
RemoteAddr
())
...
@@ -49,7 +51,8 @@ func CopyBytes(in, out *net.TCPConn) {
...
@@ -49,7 +51,8 @@ func CopyBytes(in, out *net.TCPConn) {
out
.
CloseWrite
()
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
)
{
func
ProxyConnection
(
in
,
out
*
net
.
TCPConn
)
{
glog
.
Infof
(
"Creating proxy between %v <-> %v <-> %v <-> %v"
,
glog
.
Infof
(
"Creating proxy between %v <-> %v <-> %v <-> %v"
,
in
.
RemoteAddr
(),
in
.
LocalAddr
(),
out
.
LocalAddr
(),
out
.
RemoteAddr
())
in
.
RemoteAddr
(),
in
.
LocalAddr
(),
out
.
LocalAddr
(),
out
.
RemoteAddr
())
...
@@ -117,6 +120,7 @@ func (proxier Proxier) addServiceCommon(service string, l net.Listener) {
...
@@ -117,6 +120,7 @@ func (proxier Proxier) addServiceCommon(service string, l net.Listener) {
go
proxier
.
AcceptHandler
(
service
,
l
)
go
proxier
.
AcceptHandler
(
service
,
l
)
}
}
// OnUpdate handles update notices for the updated services.
func
(
proxier
Proxier
)
OnUpdate
(
services
[]
api
.
Service
)
{
func
(
proxier
Proxier
)
OnUpdate
(
services
[]
api
.
Service
)
{
glog
.
Infof
(
"Received update notice: %+v"
,
services
)
glog
.
Infof
(
"Received update notice: %+v"
,
services
)
for
_
,
service
:=
range
services
{
for
_
,
service
:=
range
services
{
...
...
pkg/proxy/roundrobbin.go
View file @
e4d96674
...
@@ -30,22 +30,28 @@ import (
...
@@ -30,22 +30,28 @@ import (
"github.com/golang/glog"
"github.com/golang/glog"
)
)
// LoadBalancerRR is a round-robin load balancer.
type
LoadBalancerRR
struct
{
type
LoadBalancerRR
struct
{
lock
sync
.
RWMutex
endpointsMap
map
[
string
][]
string
endpointsMap
map
[
string
][]
string
rrIndex
map
[
string
]
int
rrIndex
map
[
string
]
int
lock
sync
.
RWMutex
}
}
// NewLoadBalancerRR returns a new LoadBalancerRR.
func
NewLoadBalancerRR
()
*
LoadBalancerRR
{
func
NewLoadBalancerRR
()
*
LoadBalancerRR
{
return
&
LoadBalancerRR
{
endpointsMap
:
make
(
map
[
string
][]
string
),
rrIndex
:
make
(
map
[
string
]
int
)}
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
)
{
func
(
impl
LoadBalancerRR
)
LoadBalance
(
service
string
,
srcAddr
net
.
Addr
)
(
string
,
error
)
{
impl
.
lock
.
RLock
()
impl
.
lock
.
RLock
()
endpoints
,
exists
:=
impl
.
endpointsMap
[
service
]
endpoints
,
exists
:=
impl
.
endpointsMap
[
service
]
index
:=
impl
.
rrIndex
[
service
]
index
:=
impl
.
rrIndex
[
service
]
impl
.
lock
.
RUnlock
()
impl
.
lock
.
RUnlock
()
if
exists
==
false
{
if
!
exists
{
return
""
,
errors
.
New
(
"no service entry for:"
+
service
)
return
""
,
errors
.
New
(
"no service entry for:"
+
service
)
}
}
if
len
(
endpoints
)
==
0
{
if
len
(
endpoints
)
==
0
{
...
@@ -56,6 +62,7 @@ func (impl LoadBalancerRR) LoadBalance(service string, srcAddr net.Addr) (string
...
@@ -56,6 +62,7 @@ func (impl LoadBalancerRR) LoadBalance(service string, srcAddr net.Addr) (string
return
endpoint
,
nil
return
endpoint
,
nil
}
}
// IsValid returns true if spec is valid.
func
(
impl
LoadBalancerRR
)
IsValid
(
spec
string
)
bool
{
func
(
impl
LoadBalancerRR
)
IsValid
(
spec
string
)
bool
{
index
:=
strings
.
Index
(
spec
,
":"
)
index
:=
strings
.
Index
(
spec
,
":"
)
if
index
==
-
1
{
if
index
==
-
1
{
...
@@ -68,6 +75,7 @@ func (impl LoadBalancerRR) IsValid(spec string) bool {
...
@@ -68,6 +75,7 @@ func (impl LoadBalancerRR) IsValid(spec string) bool {
return
value
>
0
return
value
>
0
}
}
// FilterValidEndpoints filters out invalid endpoints.
func
(
impl
LoadBalancerRR
)
FilterValidEndpoints
(
endpoints
[]
string
)
[]
string
{
func
(
impl
LoadBalancerRR
)
FilterValidEndpoints
(
endpoints
[]
string
)
[]
string
{
var
result
[]
string
var
result
[]
string
for
_
,
spec
:=
range
endpoints
{
for
_
,
spec
:=
range
endpoints
{
...
@@ -78,6 +86,9 @@ func (impl LoadBalancerRR) FilterValidEndpoints(endpoints []string) []string {
...
@@ -78,6 +86,9 @@ func (impl LoadBalancerRR) FilterValidEndpoints(endpoints []string) []string {
return
result
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
)
{
func
(
impl
LoadBalancerRR
)
OnUpdate
(
endpoints
[]
api
.
Endpoints
)
{
tmp
:=
make
(
map
[
string
]
bool
)
tmp
:=
make
(
map
[
string
]
bool
)
impl
.
lock
.
Lock
()
impl
.
lock
.
Lock
()
...
...
pkg/proxy/roundrobbin_test.go
View file @
e4d96674
...
@@ -59,7 +59,7 @@ func TestLoadBalanceFilterWorks(t *testing.T) {
...
@@ -59,7 +59,7 @@ func TestLoadBalanceFilterWorks(t *testing.T) {
func
TestLoadBalanceFailsWithNoEndpoints
(
t
*
testing
.
T
)
{
func
TestLoadBalanceFailsWithNoEndpoints
(
t
*
testing
.
T
)
{
loadBalancer
:=
NewLoadBalancerRR
()
loadBalancer
:=
NewLoadBalancerRR
()
endpoints
:=
make
([]
api
.
Endpoints
,
0
)
var
endpoints
[]
api
.
Endpoints
loadBalancer
.
OnUpdate
(
endpoints
)
loadBalancer
.
OnUpdate
(
endpoints
)
endpoint
,
err
:=
loadBalancer
.
LoadBalance
(
"foo"
,
nil
)
endpoint
,
err
:=
loadBalancer
.
LoadBalance
(
"foo"
,
nil
)
if
err
==
nil
{
if
err
==
nil
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment