Skip to content

Commit

Permalink
Add experimental s2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
mostynb committed Jul 24, 2022
1 parent 364200e commit 20f220e
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ algorithms that are not available in google.golang.org/grpc.
* https://github.com/golang/snappy
* https://github.com/klauspost/compress/tree/master/zstd
* https://github.com/pierrec/lz4

The following algorithms also have experimental implementations, which have
not been tested as much as those above. These may be changed significantly, or
even removed from this library at a future point.

* https://github.com/klauspost/compress/tree/master/s2
86 changes: 86 additions & 0 deletions experimental/s2/s2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2022 Mostyn Bramley-Moore.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package s2 is an experimental wrapper for using
// github.com/klauspost/compress/s2 stream compression with gRPC.
package s2

import (
"io"
"io/ioutil"
"sync"

"github.com/klauspost/compress/s2"
"google.golang.org/grpc/encoding"
)

const Name = "s2"

type compressor struct {
poolCompressor sync.Pool
poolDecompressor sync.Pool
}

type writer struct {
*s2.Writer
pool *sync.Pool
}

type reader struct {
*s2.Reader
pool *sync.Pool
}

func init() {
c := &compressor{}
c.poolCompressor.New = func() interface{} {
w := s2.NewWriter(ioutil.Discard, s2.WriterConcurrency(1))
return &writer{Writer: w, pool: &c.poolCompressor}
}
encoding.RegisterCompressor(c)
}

func (c *compressor) Compress(w io.Writer) (io.WriteCloser, error) {
s := c.poolCompressor.Get().(*writer)
s.Writer.Reset(w)
return s, nil
}

func (c *compressor) Decompress(r io.Reader) (io.Reader, error) {
s, inPool := c.poolDecompressor.Get().(*reader)
if !inPool {
newR := s2.NewReader(r)
return &reader{Reader: newR, pool: &c.poolDecompressor}, nil
}
s.Reset(r)
return s, nil
}

func (c *compressor) Name() string {
return Name
}

func (s *writer) Close() error {
err := s.Writer.Close()
s.pool.Put(s)
return err
}

func (s *reader) Read(p []byte) (n int, err error) {
n, err = s.Reader.Read(p)
if err == io.EOF {
s.pool.Put(s)
}
return n, err
}
96 changes: 96 additions & 0 deletions experimental/s2/s2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2022 Mostyn Bramley-Moore.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package s2

import (
"bytes"
"context"
"io/ioutil"
"net"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding"
"google.golang.org/grpc/test/bufconn"

"github.com/mostynb/go-grpc-compression/internal/testserver"
)

const (
bufSize = 1024
message = "Message Request s2"
)

func TestRegisteredCompression(t *testing.T) {
comp := encoding.GetCompressor(Name)
require.NotNil(t, comp)
assert.Equal(t, Name, comp.Name())

buf := bytes.NewBuffer(make([]byte, 0, bufSize))
wc, err := comp.Compress(buf)
require.NoError(t, err)

_, err = wc.Write([]byte(message))
require.NoError(t, err)
assert.NoError(t, wc.Close())

r, err := comp.Decompress(buf)
require.NoError(t, err)
expected, err := ioutil.ReadAll(r)
require.NoError(t, err)

assert.Equal(t, message, string(expected))
}

func TestRoundTrip(t *testing.T) {
lis := bufconn.Listen(bufSize)
t.Cleanup(func() {
assert.NoError(t, lis.Close())
})

done := make(chan struct{}, 1)

s := grpc.NewServer()
defer func() {
s.GracefulStop()
<-done
}()
testserver.RegisterTestServerServer(s, &testserver.EchoTestServer{})
go func() {
if err := s.Serve(lis); err != nil && err != grpc.ErrServerStopped {
t.Errorf("Server exited with error: %v", err)
}
done <- struct{}{}
}()

ctx := context.Background()
conn, err := grpc.DialContext(ctx, "bufnet",
grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}),
grpc.WithDefaultCallOptions(grpc.UseCompressor(Name)),
grpc.WithInsecure())
require.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, conn.Close())
})

client := testserver.NewTestServerClient(conn)
resp, err := client.SendMessage(ctx, &testserver.MessageRequest{Request: message})
require.NoError(t, err)
assert.Equal(t, message, resp.Response)
}

0 comments on commit 20f220e

Please sign in to comment.