Skip to content

Commit cf75199

Browse files
authored
optimize: delete pool and optimize ut (#12)
* optimize: delete pool and optimize ut * update * format * format * format * format * optimize ut
1 parent 0bb3498 commit cf75199

File tree

5 files changed

+55
-37
lines changed

5 files changed

+55
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func main() {
157157
for i := 0; i < 10; i++ {
158158
c.Write([]byte(fmt.Sprintf("chunk %d: %s\n", i, strings.Repeat("hi~", i)))) // nolint: errcheck
159159
c.Flush() // nolint: errcheck
160-
time.Sleep(200 * time.Millisecond)
160+
time.Sleep(time.Second)
161161
}
162162
})
163163
h.Spin()

README_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func main() {
151151
for i := 0; i < 10; i++ {
152152
c.Write([]byte(fmt.Sprintf("chunk %d: %s\n", i, strings.Repeat("hi~", i)))) // nolint: errcheck
153153
c.Flush() // nolint: errcheck
154-
time.Sleep(200 * time.Millisecond)
154+
time.Sleep(time.Second)
155155
}
156156
})
157157
h.Spin()

example/stream/main.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func main() {
6565
for i := 0; i < 10; i++ {
6666
c.Write([]byte(fmt.Sprintf("chunk %d: %s\n", i, strings.Repeat("hi~", i)))) // nolint: errcheck
6767
c.Flush() // nolint: errcheck
68-
time.Sleep(200 * time.Millisecond)
68+
time.Sleep(time.Second)
6969
}
7070
})
7171
go h.Spin()
@@ -87,11 +87,30 @@ func main() {
8787
}
8888

8989
bodyStream := res.BodyStream()
90-
compressedData, _ := ioutil.ReadAll(bodyStream)
91-
gunzipBytes, err := compress.AppendGunzipBytes(nil, compressedData)
90+
91+
r, err := compress.AcquireGzipReader(bodyStream)
92+
if err != nil {
93+
panic(err)
94+
}
95+
96+
firstChunk := make([]byte, 10)
97+
_, err = r.Read(firstChunk)
9298
if err != nil {
9399
panic(err)
94100
}
101+
fmt.Println(fmt.Printf("%s", firstChunk))
95102

96-
fmt.Println(fmt.Printf("%s", gunzipBytes))
103+
secondChunk := make([]byte, 13)
104+
_, err = r.Read(secondChunk)
105+
if err != nil {
106+
panic(err)
107+
}
108+
fmt.Println(fmt.Printf("%s", secondChunk))
109+
110+
otherChunks, _ := ioutil.ReadAll(r)
111+
fmt.Println(fmt.Printf("%s", otherChunks))
112+
113+
if r != nil {
114+
compress.ReleaseGzipReader(r)
115+
}
97116
}

gzip_test.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,11 @@ func TestDecompressGzipForClient(t *testing.T) {
439439
}
440440

441441
func TestStreamGzip(t *testing.T) {
442-
data := `chunk 0:
443-
chunk 1: hi~
444-
chunk 2: hi~hi~
442+
firstData := `chunk 0:
443+
`
444+
secondData := `chunk 1: hi~
445+
`
446+
otherData := `chunk 2: hi~hi~
445447
chunk 3: hi~hi~hi~
446448
chunk 4: hi~hi~hi~hi~
447449
chunk 5: hi~hi~hi~hi~hi~
@@ -457,7 +459,7 @@ chunk 9: hi~hi~hi~hi~hi~hi~hi~hi~hi~
457459
for i := 0; i < 10; i++ {
458460
c.Write([]byte(fmt.Sprintf("chunk %d: %s\n", i, strings.Repeat("hi~", i)))) // nolint: errcheck
459461
c.Flush() // nolint: errcheck
460-
time.Sleep(200 * time.Millisecond)
462+
time.Sleep(time.Second)
461463
}
462464
})
463465

@@ -484,14 +486,34 @@ chunk 9: hi~hi~hi~hi~hi~hi~hi~hi~hi~
484486
}
485487

486488
bodyStream := resp.BodyStream()
487-
compressedData, _ := ioutil.ReadAll(bodyStream)
488-
gunzipBytes, err := compress.AppendGunzipBytes(nil, compressedData)
489+
490+
r, err := compress.AcquireGzipReader(bodyStream)
491+
if err != nil {
492+
t.Fatalf("Get: %v", err)
493+
}
494+
495+
firstChunk := make([]byte, 10)
496+
_, err = r.Read(firstChunk)
489497
if err != nil {
490498
t.Fatalf("Get: %v", err)
491499
}
492500

501+
secondChunk := make([]byte, 13)
502+
_, err = r.Read(secondChunk)
503+
if err != nil {
504+
t.Fatalf("Get: %v", err)
505+
}
506+
507+
otherChunks, _ := ioutil.ReadAll(r)
508+
509+
if r != nil {
510+
compress.ReleaseGzipReader(r)
511+
}
512+
493513
assert.Equal(t, "gzip", resp.Header.Get("Content-Encoding"))
494514
assert.Equal(t, "chunked", resp.Header.Get("Transfer-Encoding"))
495515
assert.Equal(t, "Accept-Encoding", resp.Header.Get("Vary"))
496-
assert.Equal(t, data, string(gunzipBytes))
516+
assert.Equal(t, firstData, string(firstChunk))
517+
assert.Equal(t, secondData, string(secondChunk))
518+
assert.Equal(t, otherData, string(otherChunks))
497519
}

srv_stream_middleware.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ package gzip
4242

4343
import (
4444
"context"
45-
"runtime"
4645
"strings"
4746
"sync"
4847

@@ -54,16 +53,6 @@ import (
5453
"github.com/cloudwego/hertz/pkg/protocol/http1/resp"
5554
)
5655

57-
var chunkReaderPool sync.Pool
58-
59-
func init() {
60-
chunkReaderPool = sync.Pool{
61-
New: func() interface{} {
62-
return &gzipChunkedWriter{}
63-
},
64-
}
65-
}
66-
6756
type gzipChunkedWriter struct {
6857
sync.Once
6958
level int
@@ -123,24 +112,12 @@ func (g *gzipChunkedWriter) Finalize() error {
123112
return g.finalizeErr
124113
}
125114

126-
func (g *gzipChunkedWriter) release() {
127-
g.level = 0
128-
g.originalSize = 0
129-
g.compressedSize = 0
130-
g.r = nil
131-
g.w = nil
132-
g.finalizeErr = nil
133-
g.wroteHeader = false
134-
chunkReaderPool.Put(g)
135-
}
136-
137115
func newGzipChunkedWriter(r *protocol.Response, w network.Writer, level int) network.ExtWriter {
138-
extWriter := chunkReaderPool.Get().(*gzipChunkedWriter)
116+
extWriter := new(gzipChunkedWriter)
139117
extWriter.r = r
140118
extWriter.w = w
141119
extWriter.Once = sync.Once{}
142120
extWriter.level = level
143-
runtime.SetFinalizer(extWriter, (*gzipChunkedWriter).release)
144121
return extWriter
145122
}
146123

0 commit comments

Comments
 (0)