Skip to content

Commit

Permalink
Properly keep track of active/inactive connections
Browse files Browse the repository at this point in the history
* subtracting from 0 on an unsinged int makes it huge

[#91218274]
  • Loading branch information
jfmyers9 committed Apr 9, 2015
1 parent 8ddfe9b commit bc55b86
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
16 changes: 12 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,21 @@ var _ = Describe("Router Integration", func() {

go func() {
defer GinkgoRecover()

//Open a connection that never goes active
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", localIP, proxyPort))
Expect(err).NotTo(HaveOccurred())
err = conn.Close()
Expect(err).NotTo(HaveOccurred())

//Open a connection that goes active
resp, err := http.Get(longApp.Endpoint())
Ω(err).ShouldNot(HaveOccurred())
Ω(resp.StatusCode).Should(Equal(http.StatusOK))
Expect(err).ShouldNot(HaveOccurred())
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
bytes, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
Ω(err).ShouldNot(HaveOccurred())
Ω(bytes).Should(Equal([]byte{'b'}))
Expect(err).ShouldNot(HaveOccurred())
Expect(bytes).Should(Equal([]byte{'b'}))
responseRead <- true
}()

Expand Down
13 changes: 7 additions & 6 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ type Router struct {
listener net.Listener
tlsListener net.Listener
closeConnections bool
activeConns uint32
connLock sync.Mutex
idleConns map[net.Conn]struct{}
activeConns map[net.Conn]struct{}
drainDone chan struct{}
serveDone chan struct{}
tlsServeDone chan struct{}
Expand Down Expand Up @@ -89,6 +89,7 @@ func NewRouter(cfg *config.Config, p proxy.Proxy, mbusClient yagnats.NATSConn, r
serveDone: make(chan struct{}),
tlsServeDone: make(chan struct{}),
idleConns: make(map[net.Conn]struct{}),
activeConns: make(map[net.Conn]struct{}),
logger: steno.NewLogger("router"),
}

Expand Down Expand Up @@ -198,7 +199,7 @@ func (r *Router) Drain(drainTimeout time.Duration) error {
r.connLock.Lock()
r.closeIdleConns()

if r.activeConns == 0 {
if len(r.activeConns) == 0 {
close(drained)
} else {
r.drainDone = drained
Expand Down Expand Up @@ -318,12 +319,12 @@ func (r *Router) HandleConnState(conn net.Conn, state http.ConnState) {

switch state {
case http.StateActive:
r.activeConns++
r.activeConns[conn] = struct{}{}
delete(r.idleConns, conn)

conn.SetDeadline(time.Time{})
case http.StateIdle:
r.activeConns--
delete(r.activeConns, conn)
r.idleConns[conn] = struct{}{}

if r.closeConnections {
Expand All @@ -340,11 +341,11 @@ func (r *Router) HandleConnState(conn net.Conn, state http.ConnState) {
i := len(r.idleConns)
delete(r.idleConns, conn)
if i == len(r.idleConns) {
r.activeConns--
delete(r.activeConns, conn)
}
}

if r.drainDone != nil && r.activeConns == 0 {
if r.drainDone != nil && len(r.activeConns) == 0 {
close(r.drainDone)
r.drainDone = nil
}
Expand Down

0 comments on commit bc55b86

Please sign in to comment.