[proxy] return errors when attaching new containers
I had implemented this with a promise, but was informed that as waitForStart is only called once, we are safe to just put the error into the channel.
This commit is contained in:
1 parent
720862dafb
commit
63339c026d
3 files changed
+27
-14
No files matched your search
+14
-12
@@ -59,7 +59,8 @@ type Config struct {
|
|||||||
|
|
||||||
type wait struct {
|
type wait struct {
|
||||||
ident string
|
ident string
|
||||||
ch chan struct{}
|
ch chan error
|
||||||
|
done bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Proxy struct {
|
type Proxy struct {
|
||||||
@@ -304,11 +305,11 @@ func (proxy *Proxy) ContainerStarted(ident string) {
|
|||||||
}
|
}
|
||||||
// If this was a container we modified the entrypoint for, attach it to the network
|
// If this was a container we modified the entrypoint for, attach it to the network
|
||||||
if containerShouldAttach(container) {
|
if containerShouldAttach(container) {
|
||||||
proxy.attach(container, true)
|
err = proxy.attach(container, true)
|
||||||
} else if containerIsWeaveRouter(container) {
|
} else if containerIsWeaveRouter(container) {
|
||||||
proxy.attachRouter(container)
|
err = proxy.attachRouter(container)
|
||||||
}
|
}
|
||||||
proxy.notifyWaiters(container.ID)
|
proxy.notifyWaiters(container.ID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func containerShouldAttach(container *docker.Container) bool {
|
func containerShouldAttach(container *docker.Container) bool {
|
||||||
@@ -321,8 +322,7 @@ func containerIsWeaveRouter(container *docker.Container) bool {
|
|||||||
|
|
||||||
func (proxy *Proxy) createWait(r *http.Request, ident string) {
|
func (proxy *Proxy) createWait(r *http.Request, ident string) {
|
||||||
proxy.Lock()
|
proxy.Lock()
|
||||||
ch := make(chan struct{})
|
proxy.waiters[r] = &wait{ident: ident, ch: make(chan error, 1)}
|
||||||
proxy.waiters[r] = &wait{ident: ident, ch: ch}
|
|
||||||
proxy.Unlock()
|
proxy.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -332,19 +332,20 @@ func (proxy *Proxy) removeWait(r *http.Request) {
|
|||||||
proxy.Unlock()
|
proxy.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (proxy *Proxy) notifyWaiters(ident string) {
|
func (proxy *Proxy) notifyWaiters(ident string, err error) {
|
||||||
proxy.Lock()
|
proxy.Lock()
|
||||||
for _, wait := range proxy.waiters {
|
for _, wait := range proxy.waiters {
|
||||||
if ident == wait.ident && wait.ch != nil {
|
if ident == wait.ident && !wait.done {
|
||||||
|
wait.ch <- err
|
||||||
close(wait.ch)
|
close(wait.ch)
|
||||||
wait.ch = nil
|
wait.done = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
proxy.Unlock()
|
proxy.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (proxy *Proxy) waitForStart(r *http.Request) {
|
func (proxy *Proxy) waitForStart(r *http.Request) error {
|
||||||
var ch chan struct{}
|
var ch chan error
|
||||||
proxy.Lock()
|
proxy.Lock()
|
||||||
wait, found := proxy.waiters[r]
|
wait, found := proxy.waiters[r]
|
||||||
if found {
|
if found {
|
||||||
@@ -353,8 +354,9 @@ func (proxy *Proxy) waitForStart(r *http.Request) {
|
|||||||
proxy.Unlock()
|
proxy.Unlock()
|
||||||
if ch != nil {
|
if ch != nil {
|
||||||
Log.Debugf("Wait for start of container %s", wait.ident)
|
Log.Debugf("Wait for start of container %s", wait.ident)
|
||||||
<-ch
|
return <-ch
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (proxy *Proxy) ContainerDied(ident string) {
|
func (proxy *Proxy) ContainerDied(ident string) {
|
||||||
|
|||||||
@@ -53,6 +53,5 @@ func (i *startContainerInterceptor) InterceptResponse(r *http.Response) error {
|
|||||||
if r.StatusCode < 200 || r.StatusCode >= 300 { // Docker didn't do the start
|
if r.StatusCode < 200 || r.StatusCode >= 300 { // Docker didn't do the start
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
i.proxy.waitForStart(r.Request)
|
return i.proxy.waitForStart(r.Request)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
Executable
+12
@@ -0,0 +1,12 @@
|
|||||||
|
#! /bin/bash
|
||||||
|
|
||||||
|
. ./config.sh
|
||||||
|
|
||||||
|
start_suite "Proxy failure modes"
|
||||||
|
|
||||||
|
# docker run should fail when weave router is not running
|
||||||
|
weave_on $HOST1 launch-proxy
|
||||||
|
assert_raises "proxy docker_on $HOST1 run --rm $SMALL_IMAGE true" 1
|
||||||
|
assert_raises "proxy docker_on $HOST1 run --rm $SMALL_IMAGE true 2>&1 1>/dev/null | grep 'Error response from daemon: weave container is not present. Have you launched it?'"
|
||||||
|
|
||||||
|
end_suite
|
||||||
Reference in new issue
Block a user