Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit a55eca6

Browse files
author
David Chung
authored
Fix connection leak (#680)
Signed-off-by: David Chung <[email protected]>
1 parent 1ddac60 commit a55eca6

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

pkg/controller/ingress/swarm/init.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,17 @@ type Spec struct {
3737
Docker `json:",inline" yaml:",inline"`
3838
}
3939

40-
// RoutesFromSwarmServices determines the routes based on the services running in the Docker swarm
41-
func RoutesFromSwarmServices(properties *types.Any,
40+
type handler struct {
41+
dockerClient docker.APIClientCloser
42+
}
43+
44+
// Close implements io.Closer
45+
func (h *handler) Close() error {
46+
return h.dockerClient.Close()
47+
}
48+
49+
// Routes implements ingress/types/RouteHandler
50+
func (h *handler) Routes(properties *types.Any,
4251
options ingress.Options) (map[ingress.Vhost][]loadbalancer.Route, error) {
4352

4453
spec := Spec{}
@@ -63,10 +72,17 @@ func RoutesFromSwarmServices(properties *types.Any,
6372
}
6473

6574
log.Info("Connected to Docker", "client", dockerClient)
75+
h.dockerClient = dockerClient
76+
6677
routes, err := NewServiceRoutes(dockerClient).SetOptions(options).Build()
6778
if err != nil {
6879
return nil, err
6980
}
7081

7182
return routes.List()
7283
}
84+
85+
// RoutesFromSwarmServices determines the routes based on the services running in the Docker swarm
86+
func RoutesFromSwarmServices() (ingress.RouteHandler, error) {
87+
return &handler{}, nil
88+
}

pkg/controller/ingress/types/funcs.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package types
22

33
import (
4+
"io"
45
"sync"
56

67
logutil "github.com/docker/infrakit/pkg/log"
@@ -12,12 +13,19 @@ import (
1213
var (
1314
log = logutil.New("module", "controller/ingress/types")
1415
debugV = logutil.V(300)
15-
routeHandlers = map[string]func(*types.Any, Options) (map[Vhost][]loadbalancer.Route, error){}
16+
routeHandlers = map[string]func() (RouteHandler, error){}
1617
routeHandlersLock = sync.Mutex{}
1718
)
1819

20+
// RouteHandler is the interface that different modules must support
21+
type RouteHandler interface {
22+
io.Closer
23+
// Routes returns a map of vhost and loadbalancer routes given the input blob
24+
Routes(*types.Any, Options) (map[Vhost][]loadbalancer.Route, error)
25+
}
26+
1927
// RegisterRouteHandler registers a package specific handler for determining the L4 routes (e.g. static or swarm)
20-
func RegisterRouteHandler(key string, f func(*types.Any, Options) (map[Vhost][]loadbalancer.Route, error)) {
28+
func RegisterRouteHandler(key string, f func() (RouteHandler, error)) {
2129

2230
routeHandlersLock.Lock()
2331
defer routeHandlersLock.Unlock()
@@ -89,15 +97,22 @@ func (p Properties) Routes(options Options) (result map[Vhost][]loadbalancer.Rou
8997
result[spec.Vhost] = spec.Routes
9098

9199
for key, config := range spec.RouteSources {
92-
handler, has := routeHandlers[key]
100+
handlerFunc, has := routeHandlers[key]
93101

94102
log.Debug("route handler", "key", key, "exists", has, "V", debugV)
95103
if !has {
96104
continue
97105
}
98106

99107
log.Debug("calling route handler", "config", config, "options", options, "V", debugV)
100-
vhostRoutes, err := handler(config, options)
108+
109+
handler, err := handlerFunc()
110+
if err != nil {
111+
return nil, err
112+
}
113+
defer handler.Close()
114+
115+
vhostRoutes, err := handler.Routes(config, options)
101116

102117
log.Debug("found routes", "routesByVhost", vhostRoutes, "err", err)
103118
if err != nil {

pkg/controller/ingress/types/funcs_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ func TestGroupsInstanceIDs(t *testing.T) {
181181
}, mm)
182182
}
183183

184+
type routeHandlerFunc func(customConfig *types.Any, options Options) (map[Vhost][]loadbalancer.Route, error)
185+
186+
func (r routeHandlerFunc) Close() error {
187+
return nil
188+
}
189+
func (r routeHandlerFunc) Routes(customConfig *types.Any, options Options) (map[Vhost][]loadbalancer.Route, error) {
190+
return r(customConfig, options)
191+
}
192+
184193
func TestRegisterRouteHandler(t *testing.T) {
185194

186195
RegisterRouteHandler("nil", nil)
@@ -192,7 +201,9 @@ func TestRegisterRouteHandler(t *testing.T) {
192201
calledChan <- customConfig
193202
return <-routesChan, nil
194203
}
195-
RegisterRouteHandler("test", f)
204+
RegisterRouteHandler("test", func() (RouteHandler, error) {
205+
return routeHandlerFunc(f), nil
206+
})
196207
require.Equal(t, 1, len(routeHandlers))
197208

198209
vhost := Vhost("test")

0 commit comments

Comments
 (0)