Skip to content

Commit

Permalink
WIP: Add deflate compression
Browse files Browse the repository at this point in the history
  • Loading branch information
FinleyMcIlwaine committed Jan 23, 2024
1 parent 891eca8 commit 7b11f68
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
4 changes: 4 additions & 0 deletions demo-client/Demo/Client/Cmdline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ parseCompression = asum [
Opt.long "gzip"
, Opt.help "Use GZip compression for all messages"
]
, Opt.flag' Compr.deflate $ mconcat [
Opt.long "deflate"
, Opt.help "Use deflate compression for all messages"
]
]

parseAPI :: Opt.Parser API
Expand Down
10 changes: 5 additions & 5 deletions docs/demo-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ actually compress anything:
https://github.com/grpc/grpc/blob/master/examples/python/compression/README.md
* You need to send a "name" that is long enough that the server actually
bothers with compression at all.
* You can also use the `--gzip` command line flag to tell the server to /only/
use GZip compression.
* You can also use the `--gzip` or `--deflate` command line flag to tell the
server to *only* use GZip or Deflate compression, respectively.

For example:

```
cabal run demo-client -- --gzip sayHello \
--name 'John' \
--name '0xxxxxxxxxx1xxxxxxxxxx2xxxxxxxxxx3xxxxxxxxxx4xxxxxxxxxx5xxxxxxxxxx'
cabal run demo-client -- --gzip \
sayHello --name 'John' \
sayHello --name '0xxxxxxxxxx1xxxxxxxxxx2xxxxxxxxxx3xxxxxxxxxx4xxxxxxxxxx5xxxxxxxxxx'
```

Of course, the compression is transparent to the user, but you can observe it
Expand Down
1 change: 1 addition & 0 deletions src/Network/GRPC/Common/Compression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Network.GRPC.Common.Compression (
-- * Standard compression schemes
, noCompression
, gzip
, deflate
, allSupportedCompression
-- * Negotation
, Negotation(..)
Expand Down
1 change: 1 addition & 0 deletions src/Network/GRPC/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module Network.GRPC.Spec (
-- ** Compression algorithms
, noCompression
, gzip
, deflate
, allSupportedCompression
-- * Requests
, RequestHeaders(..)
Expand Down
12 changes: 11 additions & 1 deletion src/Network/GRPC/Spec/Compression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ module Network.GRPC.Spec.Compression (
-- * Specific coders
, noCompression
, gzip
, deflate
) where

import Codec.Compression.GZip qualified as GZip
import Codec.Compression.Zlib.Raw qualified as Deflate
import Data.ByteString qualified as Strict (ByteString)
import Data.ByteString.Lazy qualified as Lazy (ByteString)
import Data.ByteString.UTF8 qualified as BS.Strict.UTF8
Expand Down Expand Up @@ -50,7 +52,7 @@ instance Show Compression where
-- The order of this list is important: algorithms listed earlier are preferred
-- over algorithms listed later.
allSupportedCompression :: NonEmpty Compression
allSupportedCompression = gzip :| [noCompression]
allSupportedCompression = gzip :| [deflate, noCompression]

{-------------------------------------------------------------------------------
Compression ID
Expand Down Expand Up @@ -110,4 +112,12 @@ gzip = Compression {
compressionId = GZip
, compress = GZip.compress
, decompress = GZip.decompress
}

-- TODO: We should deal with exceptions during decompression
deflate :: Compression
deflate = Compression {
compressionId = Deflate
, compress = Deflate.compress
, decompress = Deflate.decompress
}

0 comments on commit 7b11f68

Please sign in to comment.