<commit_before>package proxy

import (
	"fmt"
	"net/http"
	"net/url"
	"sync"
	"time"

	"github.com/mailgun/vulcand/engine"
	"github.com/mailgun/vulcand/stapler"
	"github.com/mailgun/vulcand/router"

	"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/mailgun/log"
	"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/mailgun/metrics"
	"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/mailgun/route"
	"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/mailgun/timetools"
)

// mux is capable of listening on multiple interfaces, graceful shutdowns and updating TLS certificates
type mux struct {
	// Debugging id
	id int

	// Each listener address has a server associated with it
	servers map[engine.ListenerKey]*srv

	backends map[engine.BackendKey]*backend

	frontends map[engine.FrontendKey]*frontend

	hosts map[engine.HostKey]engine.Host

	// Options hold parameters that are used to initialize http servers
	options Options

	// Wait group for graceful shutdown
	wg *sync.WaitGroup

	// Read write mutex for serlialized operations
	mtx *sync.RWMutex

	// Router will be shared between mulitple listeners
	router router.Router

	// Current server stats
	state muxState

	// Connection watcher
	connTracker *connTracker

	// stopC used for global broadcast to all proxy systems that it's closed
	stopC chan struct{}

	// OCSP staple cache and responder
	stapler stapler.Stapler

	// Unsubscribe from staple updates
	stapleUpdatesC chan *stapler.StapleUpdated
}

func (m *mux) String() string {
	return fmt.Sprintf("mux(%d, %v)", m.id, m.state)
}

func New(id int, st stapler.Stapler, o Options) (*mux, error) {
	o = setDefaults(o)
	m := &mux{
		id:  id,
		wg:  &sync.WaitGroup{},
		mtx: &sync.RWMutex{},

		options: o,

		router:      route.NewMux(),
		connTracker: newConnTracker(),

		servers:   make(map[engine.ListenerKey]*srv),
		backends:  make(map[engine.BackendKey]*backend),
		frontends: make(map[engine.FrontendKey]*frontend),
		hosts:     make(map[engine.HostKey]engine.Host),

		stapleUpdatesC: make(chan *stapler.StapleUpdated),
		stopC:          make(chan struct{}),
		stapler:        st,
	}

	m.router.SetNotFound(&DefaultNotFound{})
	if o.NotFoundMiddleware != nil {
		if handler, err := o.NotFoundMiddleware.NewHandler(m.router.GetNotFound()); err == nil {
			m.router.SetNotFound(handler)
		}
	}

	if m.options.DefaultListener != nil {
		if err := m.upsertListener(*m.options.DefaultListener); err != nil {
			return nil, err
		}
	}

	// Subscribe to staple responses and kick staple updates
	m.stapler.Subscribe(m.stapleUpdatesC, m.stopC)

	m.wg.Add(1)<commit_msg>Begin to use new router<commit_after>package proxy

import (
	"fmt"
	"net/http"
	"net/url"
	"sync"
	"time"

	"github.com/mailgun/vulcand/engine"
	"github.com/mailgun/vulcand/stapler"
	"github.com/mailgun/vulcand/router"

	"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/mailgun/log"
	"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/mailgun/metrics"
	"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/mailgun/route"
	"github.com/mailgun/vulcand/Godeps/_workspace/src/github.com/mailgun/timetools"
)

// mux is capable of listening on multiple interfaces, graceful shutdowns and updating TLS certificates
type mux struct {
	// Debugging id
	id int

	// Each listener address has a server associated with it
	servers map[engine.ListenerKey]*srv

	backends map[engine.BackendKey]*backend

	frontends map[engine.FrontendKey]*frontend

	hosts map[engine.HostKey]engine.Host

	// Options hold parameters that are used to initialize http servers
	options Options

	// Wait group for graceful shutdown
	wg *sync.WaitGroup

	// Read write mutex for serlialized operations
	mtx *sync.RWMutex

	// Router will be shared between mulitple listeners
	router router.Router

	// Current server stats
	state muxState

	// Connection watcher
	connTracker *connTracker

	// stopC used for global broadcast to all proxy systems that it's closed
	stopC chan struct{}

	// OCSP staple cache and responder
	stapler stapler.Stapler

	// Unsubscribe from staple updates
	stapleUpdatesC chan *stapler.StapleUpdated
}

func (m *mux) String() string {
	return fmt.Sprintf("mux(%d, %v)", m.id, m.state)
}

func New(id int, st stapler.Stapler, o Options) (*mux, error) {
	o = setDefaults(o)
	m := &mux{
		id:  id,
		wg:  &sync.WaitGroup{},
		mtx: &sync.RWMutex{},

		options: o,

		router:      o.Router,
		connTracker: newConnTracker(),

		servers:   make(map[engine.ListenerKey]*srv),
		backends:  make(map[engine.BackendKey]*backend),
		frontends: make(map[engine.FrontendKey]*frontend),
		hosts:     make(map[engine.HostKey]engine.Host),

		stapleUpdatesC: make(chan *stapler.StapleUpdated),
		stopC:          make(chan struct{}),
		stapler:        st,
	}

	m.router.SetNotFound(&DefaultNotFound{})
	if o.NotFoundMiddleware != nil {
		if handler, err := o.NotFoundMiddleware.NewHandler(m.router.GetNotFound()); err == nil {
			m.router.SetNotFound(handler)
		}
	}

	if m.options.DefaultListener != nil {
		if err := m.upsertListener(*m.options.DefaultListener); err != nil {
			return nil, err
		}
	}

	// Subscribe to staple responses and kick staple updates
	m.stapler.Subscribe(m.stapleUpdatesC, m.stopC)

	m.wg.Add(1)
{'fraction_non_alphanumeric': 0.11867630277672118, 'fraction_numerical': 0.0003803727653100038, 'mean_word_length': 4.071359691417551, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4387171', 'n_tokens_mistral': 1961, 'n_tokens_neox': 1803, 'n_words': 425}