Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
59fa2482
Commit
59fa2482
authored
Dec 31, 2015
by
JessonChan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use sync.Pool to decrease new compression writer
parent
9519fc6c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
6 deletions
+65
-6
acceptencoder.go
context/acceptencoder.go
+65
-6
No files found.
context/acceptencoder.go
View file @
59fa2482
...
...
@@ -24,21 +24,79 @@ import (
"os"
"strconv"
"strings"
"sync"
)
type
resetWriter
interface
{
io
.
Writer
Reset
(
w
io
.
Writer
)
}
type
nopResetWriter
struct
{
io
.
Writer
}
func
(
n
nopResetWriter
)
Reset
(
w
io
.
Writer
)
{
//do nothing
}
type
encodeFunc
func
(
io
.
Writer
,
int
)
(
resetWriter
,
error
)
type
acceptEncoder
struct
{
name
string
encode
func
(
io
.
Writer
,
int
)
(
io
.
Writer
,
error
)
name
string
bestSpeedPool
*
sync
.
Pool
bestCompressionPool
*
sync
.
Pool
}
func
(
ac
acceptEncoder
)
encode
(
wr
io
.
Writer
,
level
int
)
(
resetWriter
,
error
)
{
if
ac
.
bestSpeedPool
==
nil
||
ac
.
bestCompressionPool
==
nil
{
return
nopResetWriter
{
wr
},
nil
}
var
rwr
resetWriter
if
level
==
flate
.
BestSpeed
{
rwr
=
ac
.
bestSpeedPool
.
Get
()
.
(
resetWriter
)
}
else
{
rwr
=
ac
.
bestCompressionPool
.
Get
()
.
(
resetWriter
)
}
rwr
.
Reset
(
wr
)
return
rwr
,
nil
}
func
(
ac
acceptEncoder
)
put
(
wr
resetWriter
,
level
int
)
{
if
ac
.
bestSpeedPool
==
nil
||
ac
.
bestCompressionPool
==
nil
{
return
}
wr
.
Reset
(
nil
)
switch
level
{
case
flate
.
BestSpeed
:
ac
.
bestSpeedPool
.
Put
(
wr
)
case
flate
.
BestCompression
:
ac
.
bestCompressionPool
.
Put
(
wr
)
}
}
var
(
noneCompressEncoder
=
acceptEncoder
{
""
,
func
(
wr
io
.
Writer
,
level
int
)
(
io
.
Writer
,
error
)
{
return
wr
,
nil
}}
gzipCompressEncoder
=
acceptEncoder
{
"gzip"
,
func
(
wr
io
.
Writer
,
level
int
)
(
io
.
Writer
,
error
)
{
return
gzip
.
NewWriterLevel
(
wr
,
level
)
}}
noneCompressEncoder
=
acceptEncoder
{
""
,
nil
,
nil
}
gzipCompressEncoder
=
acceptEncoder
{
"gzip"
,
&
sync
.
Pool
{
New
:
func
()
interface
{}
{
wr
,
_
:=
gzip
.
NewWriterLevel
(
nil
,
flate
.
BestSpeed
);
return
wr
},
},
&
sync
.
Pool
{
New
:
func
()
interface
{}
{
wr
,
_
:=
gzip
.
NewWriterLevel
(
nil
,
flate
.
BestCompression
);
return
wr
},
},
}
//according to the sec :http://tools.ietf.org/html/rfc2616#section-3.5 ,the deflate compress in http is zlib indeed
//deflate
//The "zlib" format defined in RFC 1950 [31] in combination with
//the "deflate" compression mechanism described in RFC 1951 [29].
deflateCompressEncoder
=
acceptEncoder
{
"deflate"
,
func
(
wr
io
.
Writer
,
level
int
)
(
io
.
Writer
,
error
)
{
return
zlib
.
NewWriterLevel
(
wr
,
level
)
}}
deflateCompressEncoder
=
acceptEncoder
{
"deflate"
,
&
sync
.
Pool
{
New
:
func
()
interface
{}
{
wr
,
_
:=
zlib
.
NewWriterLevel
(
nil
,
flate
.
BestSpeed
);
return
wr
},
},
&
sync
.
Pool
{
New
:
func
()
interface
{}
{
wr
,
_
:=
zlib
.
NewWriterLevel
(
nil
,
flate
.
BestCompression
);
return
wr
},
},
}
)
var
(
...
...
@@ -63,7 +121,7 @@ func WriteBody(encoding string, writer io.Writer, content []byte) (bool, string,
// writeLevel reads from reader,writes to writer by specific encoding and compress level
// the compress level is defined by deflate package
func
writeLevel
(
encoding
string
,
writer
io
.
Writer
,
reader
io
.
Reader
,
level
int
)
(
bool
,
string
,
error
)
{
var
outputWriter
io
.
Writer
var
outputWriter
reset
Writer
var
err
error
var
ce
=
noneCompressEncoder
...
...
@@ -72,6 +130,7 @@ func writeLevel(encoding string, writer io.Writer, reader io.Reader, level int)
}
encoding
=
ce
.
name
outputWriter
,
err
=
ce
.
encode
(
writer
,
level
)
defer
ce
.
put
(
outputWriter
,
level
)
if
err
!=
nil
{
return
false
,
""
,
err
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment