Skip to content

Commit

Permalink
refactor: simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
aofei committed Dec 10, 2020
1 parent aa43f1c commit b1b30c0
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 56 deletions.
37 changes: 14 additions & 23 deletions air.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,12 +569,12 @@ type Air struct {
contextCancel context.CancelFunc
addressMap map[string]int
shutdownJobs []func()
shutdownJobMutex *sync.Mutex
shutdownJobMutex sync.Mutex
shutdownJobDone chan struct{}
requestPool *sync.Pool
responsePool *sync.Pool
contentTypeSnifferBufferPool *sync.Pool
gzipWriterPool *sync.Pool
requestPool sync.Pool
responsePool sync.Pool
contentTypeSnifferBufferPool sync.Pool
gzipWriterPool sync.Pool
reverseProxyTransport *reverseProxyTransport
reverseProxyBufferPool *reverseProxyBufferPool
}
Expand Down Expand Up @@ -657,31 +657,22 @@ func New() *Air {

a.context, a.contextCancel = context.WithCancel(context.Background())
a.addressMap = map[string]int{}
a.shutdownJobMutex = &sync.Mutex{}
a.shutdownJobDone = make(chan struct{})
a.requestPool = &sync.Pool{
New: func() interface{} {
return &Request{}
},
a.requestPool.New = func() interface{} {
return &Request{}
}

a.responsePool = &sync.Pool{
New: func() interface{} {
return &Response{}
},
a.responsePool.New = func() interface{} {
return &Response{}
}

a.contentTypeSnifferBufferPool = &sync.Pool{
New: func() interface{} {
return make([]byte, 512)
},
a.contentTypeSnifferBufferPool.New = func() interface{} {
return make([]byte, 512)
}

a.gzipWriterPool = &sync.Pool{
New: func() interface{} {
w, _ := gzip.NewWriterLevel(nil, a.GzipCompressionLevel)
return w
},
a.gzipWriterPool.New = func() interface{} {
w, _ := gzip.NewWriterLevel(nil, a.GzipCompressionLevel)
return w
}

a.reverseProxyTransport = newReverseProxyTransport()
Expand Down
5 changes: 0 additions & 5 deletions air_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,13 @@ func TestNew(t *testing.T) {
assert.NotNil(t, a.contextCancel)
assert.NotNil(t, a.addressMap)
assert.Nil(t, a.shutdownJobs)
assert.NotNil(t, a.shutdownJobMutex)
assert.Zero(t, cap(a.shutdownJobDone))
assert.NotNil(t, a.requestPool)
assert.NotNil(t, a.responsePool)
assert.IsType(t, &Request{}, a.requestPool.Get())
assert.IsType(t, &Response{}, a.responsePool.Get())

assert.NotNil(t, a.contentTypeSnifferBufferPool)
assert.IsType(t, []byte{}, a.contentTypeSnifferBufferPool.Get())
assert.Len(t, a.contentTypeSnifferBufferPool.Get(), 512)

assert.NotNil(t, a.gzipWriterPool)
assert.IsType(t, &gzip.Writer{}, a.gzipWriterPool.Get())

assert.NotNil(t, a.reverseProxyTransport)
Expand Down
3 changes: 1 addition & 2 deletions coffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type coffer struct {
loadOnce *sync.Once
loadError error
watcher *fsnotify.Watcher
assets *sync.Map
assets sync.Map
cache *fastcache.Cache
}

Expand Down Expand Up @@ -78,7 +78,6 @@ func (c *coffer) load() {
}()
}

c.assets = &sync.Map{}
c.cache = fastcache.New(c.a.CofferMaxMemoryBytes)
}

Expand Down
2 changes: 0 additions & 2 deletions coffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func TestNewCoffer(t *testing.T) {
assert.NotNil(t, c.a)
assert.NotNil(t, c.loadOnce)
assert.Nil(t, c.watcher)
assert.Nil(t, c.assets)
assert.Nil(t, c.cache)
}

Expand All @@ -29,7 +28,6 @@ func TestCofferLoad(t *testing.T) {
c.load()
assert.Nil(t, c.loadError)
assert.NotNil(t, c.watcher)
assert.NotNil(t, c.assets)
assert.NotNil(t, c.cache)
}

Expand Down
3 changes: 1 addition & 2 deletions listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func (l *listener) Accept() (net.Conn, error) {
return &proxyConn{
Conn: tc,
bufReader: bufio.NewReader(tc),
readHeaderOnce: &sync.Once{},
readHeaderTimeout: l.a.PROXYReadHeaderTimeout,
}, nil
}
Expand All @@ -114,7 +113,7 @@ type proxyConn struct {
bufReader *bufio.Reader
srcAddr *net.TCPAddr
dstAddr *net.TCPAddr
readHeaderOnce *sync.Once
readHeaderOnce sync.Once
readHeaderError error
readHeaderTimeout time.Duration
}
Expand Down
1 change: 0 additions & 1 deletion listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func TestListenerAccept(t *testing.T) {
assert.NotNil(t, pc.bufReader)
assert.Nil(t, pc.srcAddr)
assert.Nil(t, pc.dstAddr)
assert.NotNil(t, pc.readHeaderOnce)
assert.Nil(t, pc.readHeaderError)
assert.Zero(t, pc.readHeaderTimeout)

Expand Down
5 changes: 2 additions & 3 deletions minifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import (
// minifier is a minifier that minifies contents based on the MIME types.
type minifier struct {
a *Air
loadOnce *sync.Once
loadOnce sync.Once
minifier *minify.M
}

// newMinifier returns a new instance of the `minifier` with the a.
func newMinifier(a *Air) *minifier {
return &minifier{
a: a,
loadOnce: &sync.Once{},
a: a,
}
}

Expand Down
1 change: 0 additions & 1 deletion minifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func TestNewMinifier(t *testing.T) {

assert.NotNil(t, m)
assert.NotNil(t, m.a)
assert.NotNil(t, m.loadOnce)
assert.Nil(t, m.minifier)
}

Expand Down
8 changes: 4 additions & 4 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ type Request struct {
params []*RequestParam
routeParamNames []string
routeParamValues []string
parseRouteParamsOnce *sync.Once
parseOtherParamsOnce *sync.Once
parseRouteParamsOnce sync.Once
parseOtherParamsOnce sync.Once
values map[string]interface{}
localizedString func(string) string
}
Expand All @@ -115,8 +115,8 @@ func (r *Request) reset(a *Air, hr *http.Request, res *Response) {
r.params = r.params[:0]
r.routeParamNames = nil
r.routeParamValues = nil
r.parseRouteParamsOnce = &sync.Once{}
r.parseOtherParamsOnce = &sync.Once{}
r.parseRouteParamsOnce = sync.Once{}
r.parseOtherParamsOnce = sync.Once{}
for key := range r.values {
delete(r.values, key)
}
Expand Down
2 changes: 0 additions & 2 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ func TestRequestHTTPRequest(t *testing.T) {
assert.Len(t, req.params, 0)
assert.Len(t, req.routeParamNames, 0)
assert.Len(t, req.routeParamValues, 0)
assert.NotNil(t, req.parseRouteParamsOnce)
assert.NotNil(t, req.parseOtherParamsOnce)
assert.Nil(t, req.values)
assert.Nil(t, req.localizedString)

Expand Down
4 changes: 2 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -1217,14 +1217,14 @@ func (rpt *reverseProxyTransport) RoundTrip(

// reverseProxyBufferPool is a buffer pool for the reverse proxy.
type reverseProxyBufferPool struct {
pool *sync.Pool
pool sync.Pool
}

// newReverseProxyBufferPool returns a new instance of the
// `reverseProxyBufferPool`.
func newReverseProxyBufferPool() *reverseProxyBufferPool {
return &reverseProxyBufferPool{
pool: &sync.Pool{
pool: sync.Pool{
New: func() interface{} {
return make([]byte, 32<<20)
},
Expand Down
4 changes: 1 addition & 3 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,7 @@ func TestResponseGzippable(t *testing.T) {
}

func TestNewReverseProxyBufferPool(t *testing.T) {
rpbp := newReverseProxyBufferPool()

assert.NotNil(t, rpbp.pool)
assert.NotNil(t, newReverseProxyBufferPool())
}

func TestReverseProxyBufferPoolGet(t *testing.T) {
Expand Down
9 changes: 4 additions & 5 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type router struct {
routeTree *routeNode
registeredRoutes map[string]bool
maxRouteParams int
routeParamValuesPool *sync.Pool
routeParamValuesPool sync.Pool
}

// newRouter returns a new instance of the `router` with the a.
Expand All @@ -27,10 +27,9 @@ func newRouter(a *Air) *router {
},
registeredRoutes: map[string]bool{},
}
r.routeParamValuesPool = &sync.Pool{
New: func() interface{} {
return make([]string, r.maxRouteParams)
},

r.routeParamValuesPool.New = func() interface{} {
return make([]string, r.maxRouteParams)
}

return r
Expand Down
1 change: 0 additions & 1 deletion router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func TestNewRouter(t *testing.T) {
assert.NotNil(t, r.routeTree)
assert.NotNil(t, r.routeTree.handlers)
assert.NotNil(t, r.registeredRoutes)
assert.NotNil(t, r.routeParamValuesPool)
}

func TestRouterRegister(t *testing.T) {
Expand Down

0 comments on commit b1b30c0

Please sign in to comment.