Skip to content

Commit

Permalink
Extract proxy.go chagnes to #322
Browse files Browse the repository at this point in the history
  • Loading branch information
miry committed Sep 17, 2021
1 parent a9a0a04 commit 8357384
Showing 1 changed file with 34 additions and 51 deletions.
85 changes: 34 additions & 51 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ type Proxy struct {
Upstream string `json:"upstream"`
Enabled bool `json:"enabled"`

listener net.Listener
started chan error
started chan error

tomb tomb.Tomb
connections ConnectionList
Expand Down Expand Up @@ -89,64 +88,48 @@ func (proxy *Proxy) Stop() {
stop(proxy)
}

func (p *Proxy) listen() (net.Listener, error) {
listener, err := net.Listen("tcp", p.Listen)
if err != nil {
p.started <- err
return nil, err
}
p.listener = listener
p.Listen = listener.Addr().String()
p.started <- nil

logrus.WithFields(logrus.Fields{
"name": p.Name,
"proxy": p.Listen,
"upstream": p.Upstream,
}).Info("Started proxy")

return p.listener, nil
}

func (p *Proxy) close() {
// Unblock ln.Accept()
err := p.listener.Close()
if err != nil {
logrus.WithFields(logrus.Fields{
"proxy": p.Name,
"listen": p.Listen,
"err": err,
}).Warn("Attempted to close an already closed proxy server")
}
}

// This channel is to kill the blocking Accept() call below by closing the
// net.Listener.
func (p *Proxy) freeBlocker(acceptTomb *tomb.Tomb) {
<-p.tomb.Dying()

// Notify ln.Accept() that the shutdown was safe
acceptTomb.Killf("Shutting down from stop()")

p.close()

// Wait for the accept loop to finish processing
acceptTomb.Wait()
p.tomb.Done()
}

// server runs the Proxy server, accepting new clients and creating Links to
// connect them to upstreams.
func (proxy *Proxy) server() {
ln, err := proxy.listen()
ln, err := net.Listen("tcp", proxy.Listen)
if err != nil {
proxy.started <- err
return
}

acceptTomb := &tomb.Tomb{}
proxy.Listen = ln.Addr().String()
proxy.started <- nil

logrus.WithFields(logrus.Fields{
"name": proxy.Name,
"proxy": proxy.Listen,
"upstream": proxy.Upstream,
}).Info("Started proxy")

acceptTomb := tomb.Tomb{}
defer acceptTomb.Done()

go proxy.freeBlocker(acceptTomb)
// This channel is to kill the blocking Accept() call below by closing the
// net.Listener.
go func() {
<-proxy.tomb.Dying()

// Notify ln.Accept() that the shutdown was safe
acceptTomb.Killf("Shutting down from stop()")
// Unblock ln.Accept()
err := ln.Close()
if err != nil {
logrus.WithFields(logrus.Fields{
"proxy": proxy.Name,
"listen": proxy.Listen,
"err": err,
}).Warn("Attempted to close an already closed proxy server")
}

// Wait for the accept loop to finish processing
acceptTomb.Wait()
proxy.tomb.Done()
}()

for {
client, err := ln.Accept()
Expand Down

0 comments on commit 8357384

Please sign in to comment.