-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Fjord: Add Brotli channel compression support (#10358)
* wip * wip * fix * fix * fix * fix * address some of the bots comments * use version bit of 1 * fix lint * adding compression type * update batch reader * abstract span channel compressor * test and singular batch compressor * fix * lint * move channel compressor as interface * add base class * fix go mod * test fixes * address comments * fix * fix * revert channel builder test * revert ratio compressor test * add checks to accept brotli only post fjord * revemo unnecessary in test * fix forge-std * gofmt * address comments * remove methods in compressor * fix error msg * add compression algo flag to optional flags * add Clone() function --------- Co-authored-by: Roberto Bayardo <roberto.bayardo@coinbase.com>
- v1.11.1
- v1.11.0
- v1.10.0
- v1.9.5
- v1.9.4
- v1.9.3
- v1.9.2
- v1.9.1
- v1.9.0
- v1.8.0
- v1.7.7
- v1.7.6
- proxyd/v4.6.0
- proofs-tools/v1.0.0-alpha.20
- proofs-tools/v1.0.0-alpha.19
- proofs-tools/v1.0.0-alpha.18
- proofs-tools/v1.0.0-alpha.17
- proofs-tools/v1.0.0-alpha.16
- proofs-tools/v1.0.0-alpha.15
- proofs-tools/v1.0.0-alpha.14
- proofs-tools/v1.0.0-alpha.13
- proofs-tools/v1.0.0-alpha.12
- proofs-tools/v1.0.0-alpha.11
- proofs-tools/v1.0.0-alpha.10
- proofs-tools/v1.0.0-alpha.9
- proofs-tools/v1.0.0-alpha.8
- proofs-tools/v1.0.0-alpha.7
- proofs-tools/v1.0.0-alpha.6
- proofs-tools/v1.0.0-alpha.5
- proofs-tools/v1.0.0-alpha.4
- proofs-tools/v1.0.0-alpha.3
- proofs-tools/v1.0.0-alpha.2
- proofs-tools/v1.0.0-alpha.1
- op-supervisor/0.1.0-rc.1
- op-proposer/v1.10.0-rc.2
- op-proposer/v1.10.0-rc.1
- op-proposer/v1.9.5
- op-proposer/v1.9.5-rc.4
- op-proposer/v1.9.5-rc.3
- op-proposer/v1.9.5-rc.2
- op-proposer/v1.9.5-rc.1
- op-proposer/v1.9.4
- op-proposer/v1.9.4-rc.3
- op-proposer/v1.9.4-rc.2
- op-proposer/v1.9.4-rc.1
- op-proposer/v1.9.3
- op-proposer/v1.9.3-rc.2
- op-proposer/v1.9.3-rc.1
- op-proposer/v1.9.2
- op-proposer/v1.9.2-rc.2
- op-proposer/v1.9.2-rc.1
- op-proposer/v1.9.1
- op-proposer/v1.9.1-rc.4
- op-proposer/v1.9.1-rc.3
- op-proposer/v1.9.1-rc.2
- op-proposer/v1.9.0
- op-proposer/v1.9.0-rc.4
- op-proposer/v1.9.0-rc.3
- op-proposer/v1.9.0-rc.2
- op-proposer/v1.9.0-rc.1
- op-proposer/v1.8.1-rc.1
- op-proposer/v1.8.0
- op-proposer/v1.8.0-rc.2
- op-proposer/v1.8.0-rc.1
- op-proposer/v1.7.8-rc.1
- op-proposer/v1.7.7
- op-proposer/v1.7.7-rc.3
- op-proposer/v1.7.7-rc.1
- op-proposer/v1.7.6
- op-proposer/v1.7.6-rc.2
- op-proposer/v1.7.6-rc.1
- op-program/v1.5.0-rc.2
- op-program/v1.5.0-rc.1
- op-program/v1.4.1-rc.3
- op-program/v1.4.1-rc.2
- op-program/v1.4.1-rc.1
- op-program/v1.4.0
- op-program/v1.4.0-rc.3
- op-program/v1.4.0-rc.2
- op-program/v1.4.0-rc.1
- op-program/v1.3.1
- op-program/v1.3.1-rc.2
- op-program/v1.3.1-rc.1
- op-program/v1.3.1-ink
- op-program/v1.3.0-rc.3
- op-program/v1.3.0-rc.2
- op-program/v1.3.0-rc.1
- op-program/v1.2.0
- op-program/v1.1.0
- op-program/v1.0.1
- op-node/v1.11.1
- op-node/v1.11.1-rc.1
- op-node/v1.11.0
- op-node/v1.11.0-rc.2
- op-node/v1.11.0-rc.1
- op-node/v1.10.4-rc.1
- op-node/v1.10.4-dev.5
- op-node/v1.10.4-dev.4
- op-node/v1.10.4-dev.3
- op-node/v1.10.4-dev.2
- op-node/v1.10.4-dev.1
- op-node/v1.10.4-dev.0
- op-node/v1.10.3
- op-node/v1.10.3-rc.1
- op-node/v1.10.3-dev.0
- op-node/v1.10.2
- op-node/v1.10.2-rc.2
- op-node/v1.10.2-rc.1
- op-node/v1.10.1
- op-node/v1.10.1-seqw-recovery-hotfix-rc.2
- op-node/v1.10.1-seqw-recovery-hotfix-rc.1
- op-node/v1.10.1-rc.1
- op-node/v1.10.0
- op-node/v1.10.0-rc.2
- op-node/v1.10.0-rc.1
- op-node/v1.9.5
- op-node/v1.9.5-rc.4
- op-node/v1.9.5-rc.3
- op-node/v1.9.5-rc.2
- op-node/v1.9.5-rc.1
- op-node/v1.9.5-interop.0
- op-node/v1.9.4
- op-node/v1.9.4-rc.3
- op-node/v1.9.4-rc.2
- op-node/v1.9.4-rc.1
- op-node/v1.9.4-dev.0
- op-node/v1.9.3
- op-node/v1.9.3-rc.2
- op-node/v1.9.3-rc.1
- op-node/v1.9.2
- op-node/v1.9.2-rc.2
- op-node/v1.9.2-rc.1
- op-node/v1.9.1
- op-node/v1.9.1-rc.4
- op-node/v1.9.1-rc.3
- op-node/v1.9.1-rc.2
- op-node/v1.9.1-rc.1
- op-node/v1.9.1-dev.0
- op-node/v1.9.0
- op-node/v1.9.0-rc.4
- op-node/v1.9.0-rc.3
- op-node/v1.9.0-rc.2
- op-node/v1.9.0-rc.1
- op-node/v1.8.1-rc.1
- op-node/v1.8.0
- op-node/v1.8.0-rc.2
- op-node/v1.8.0-rc.2-hotfix
- op-node/v1.8.0-rc.1
- op-node/v1.7.8-rc.1
- op-node/v1.7.7
- op-node/v1.7.7-rc.3
- op-node/v1.7.7-rc.2
- op-node/v1.7.7-rc.1
- op-node/v1.7.6
- op-node/v1.7.6-rc.2
- op-node/v1.7.6-rc.1
- op-node/v1.7.6-blob-sidecars-fix
- op-node/geth_1_14_17_synctest
- op-dripper/v0.0.1
- op-dispute-mon/v1.3.0
- op-dispute-mon/v1.3.0-rc.1
- op-dispute-mon/v1.2.0
- op-dispute-mon/v1.2.0-rc.1
- op-dispute-mon/v1.1.0
- op-dispute-mon/v1.1.0-rc.1
- op-dispute-mon/v1.0.1
- op-dispute-mon/v1.0.0
- op-dispute-mon/v1.0.0-rc.1
- op-dispute-mon/v0.2.8
- op-dispute-mon/v0.2.7
- op-dispute-mon/v0.2.6
- op-deployer/v0.1.0-rc.2
- op-deployer/v0.1.0-rc.1
- op-deployer/v0.0.12
- op-deployer/v0.0.11
- op-deployer/v0.0.10
- op-deployer/v0.0.9
- op-deployer/v0.0.8
- op-deployer/v0.0.7
- op-deployer/v0.0.6
- op-deployer/v0.0.5
- op-deployer/v0.0.4
- op-deployer/v0.0.3
- op-deployer/v0.0.2
- op-deployer/v0.0.1-rc.0
- op-contracts-v180-blueprints-script
- op-contracts-v180-artifacts-base
- op-contracts-v160-artifacts-read-implementations-backport
- op-contracts-v160-artifacts-opcm-redesign-backport
- op-contracts-v160-artifacts-base
- op-contracts/v2.0.0-beta.3
- op-contracts/v2.0.0-beta.2
- op-contracts/v2.0.0-beta.1
- op-contracts/v1.9.0-rc.3
- op-contracts/v1.9.0-rc.2
- op-contracts/v1.9.0-rc.1
- op-contracts/v1.8.0
- op-contracts/v1.8.0-rc.4
- op-contracts/v1.8.0-rc.3
- op-contracts/v1.8.0-rc.2
- op-contracts/v1.8.0-rc.1
- op-contracts/v.1.7.0-beta.1+l2-contracts
- op-contracts/v1.6.0
- op-contracts/v1.6.0-rc.3
- op-contracts/v1.6.0-rc.2
- op-contracts/v1.6.0-rc.1
- op-contracts/v1.5.0
- op-contracts/v1.5.0-rc.1
- op-conductor/v0.3.1
- op-conductor/v0.3.1-rc2
- op-conductor/v0.3.1-rc1
- op-conductor/v0.3.0
- op-conductor/v0.2.0
- op-conductor/v0.1.0-rc.1
- op-conductor/v0.0.1
- op-conductor/v0.0.1-rc.5
- op-conductor/v0.0.1-rc.4
- op-conductor/v0.0.1-rc.3
- op-conductor/v0.0.1-rc.2
- op-conductor/v0.0.1-rc.1
- op-conductor/v0.0.0-rc.4
- op-conductor/v0.0.0-rc.3
- op-challenger/v1.3.1
- op-challenger/v1.3.1-rc.7
- op-challenger/v1.3.1-rc.6
- op-challenger/v1.3.1-rc.5
- op-challenger/v1.3.1-rc.4
- op-challenger/v1.3.1-rc.3
- op-challenger/v1.3.1-rc.2
- op-challenger/v1.3.1-rc.1
- op-challenger/v1.3.0
- op-challenger/v1.3.0-rc.2
- op-challenger/v1.3.0-rc.1
- op-challenger/v1.2.0
- op-challenger/v1.2.0-rc.3
- op-challenger/v1.2.0-rc.2
- op-challenger/v1.2.0-rc.1
- op-challenger/v1.1.2
- op-challenger/v1.1.2-rc.1
- op-challenger/v1.1.1
- op-challenger/v1.1.0
- op-challenger/v1.1.0-rc.1
- op-challenger/v1.0.1
- op-challenger/v1.0.0
- op-challenger/v1.0.0-rc.1
- op-challenger/v0.2.11
- op-challenger/v0.2.10
- op-challenger/v0.2.9
- op-challenger/v0.2.8
- op-batcher/v1.11.1
- op-batcher/v1.11.1-rc.1
- op-batcher/v1.11.0
- op-batcher/v1.11.0-rc.1
- op-batcher/v1.10.0
- op-batcher/v1.10.0-rc.4
- op-batcher/v1.10.0-rc.3
- op-batcher/v1.10.0-rc.2
- op-batcher/v1.10.0-rc.1
- op-batcher/v1.9.5
- op-batcher/v1.9.5-rc.4
- op-batcher/v1.9.5-rc.3
- op-batcher/v1.9.5-rc.2
- op-batcher/v1.9.5-rc.1
- op-batcher/v1.9.4
- op-batcher/v1.9.4-rc.3
- op-batcher/v1.9.4-rc.2
- op-batcher/v1.9.4-rc.1
- op-batcher/v1.9.3
- op-batcher/v1.9.3-rc.2
- op-batcher/v1.9.3-rc.1
- op-batcher/v1.9.2
- op-batcher/v1.9.2-rc.2
- op-batcher/v1.9.2-rc.1
- op-batcher/v1.9.1
- op-batcher/v1.9.1-rc.4
- op-batcher/v1.9.1-rc.3
- op-batcher/v1.9.1-rc.2
- op-batcher/v1.9.1-rc.1
- op-batcher/v1.9.0
- op-batcher/v1.9.0-rc.4
- op-batcher/v1.9.0-rc.3
- op-batcher/v1.9.0-rc.2
- op-batcher/v1.9.0-rc.1
- op-batcher/v1.9.0-granite-test.0
- op-batcher/v1.8.1-rc.1
- op-batcher/v1.8.1-dyn-eth-da.2
- op-batcher/v1.8.1-dyn-eth-da.1
- op-batcher/v1.8.0
- op-batcher/v1.8.0-rc.2
- op-batcher/v1.8.0-rc.1
- op-batcher/v1.7.8-rc.1
- op-batcher/v1.7.7
- op-batcher/v1.7.7-rc.3
- op-batcher/v1.7.7-rc.1
- op-batcher/v1.7.6
- op-batcher/v1.7.6-rc.2
- op-batcher/v1.7.6-rc.1
- @eth-optimism/sdk@3.3.1
- @eth-optimism/devnet-tasks@3.3.1
- @eth-optimism/contracts-bedrock@0.17.3
- @eth-optimism/chain-mon@0.6.6
- @eth-optimism/chain-mon@0.6.5
- da-server/v0.1.0
- da-server/v0.1.0-rc.2
- da-server/v0.1.0-rc.1
- ci-builder/v0.59.0
- ci-builder/v0.58.0
- ci-builder/v0.57.0
- ci-builder/v0.56.0
- ci-builder/v0.55.0
- ci-builder/v0.54.0
- ci-builder/v0.53.0
- ci-builder/v0.52.0
- ci-builder/v0.51.0
- ci-builder/v0.50.0
- chain-mon/v1.2.1
- chain-mon/v1.2.0
- cannon/v1.4.0-alpha.1
- cannon/v1.3.0
- cannon/v1.3.0-rc.1
- cannon/v1.2.0
- cannon/v1.1.0
- cannon/v1.1.0-rc.1
- cannon/v1.1.0-alpha.4
- cannon/v1.1.0-alpha.3
- cannon/v1.1.0-alpha.2
- cannon/v1.1.0-alpha.1
- cannon/v1.0.0
- cannon/v1.0.0-alpha.3
- cannon/v1.0.0-alpha.2
- cannon/v1.0.0-alpha.1
1 parent
ea52388
commit 4b8f6f4
Showing
32 changed files
with
714 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package derive | ||
|
||
import ( | ||
"bytes" | ||
"compress/zlib" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/andybalholm/brotli" | ||
) | ||
|
||
const ( | ||
ChannelVersionBrotli byte = 0x01 | ||
) | ||
|
||
type ChannelCompressor interface { | ||
Write([]byte) (int, error) | ||
Flush() error | ||
Close() error | ||
Reset() | ||
Len() int | ||
Read([]byte) (int, error) | ||
GetCompressed() *bytes.Buffer | ||
} | ||
|
||
type CompressorWriter interface { | ||
Write([]byte) (int, error) | ||
Flush() error | ||
Close() error | ||
Reset(io.Writer) | ||
} | ||
|
||
type BaseChannelCompressor struct { | ||
compressed *bytes.Buffer | ||
CompressorWriter | ||
} | ||
|
||
func (bcc *BaseChannelCompressor) Len() int { | ||
return bcc.compressed.Len() | ||
} | ||
|
||
func (bcc *BaseChannelCompressor) Read(p []byte) (int, error) { | ||
return bcc.compressed.Read(p) | ||
} | ||
|
||
func (bcc *BaseChannelCompressor) GetCompressed() *bytes.Buffer { | ||
return bcc.compressed | ||
} | ||
|
||
type ZlibCompressor struct { | ||
BaseChannelCompressor | ||
} | ||
|
||
func (zc *ZlibCompressor) Reset() { | ||
zc.compressed.Reset() | ||
zc.CompressorWriter.Reset(zc.compressed) | ||
} | ||
|
||
type BrotliCompressor struct { | ||
BaseChannelCompressor | ||
} | ||
|
||
func (bc *BrotliCompressor) Reset() { | ||
bc.compressed.Reset() | ||
bc.compressed.WriteByte(ChannelVersionBrotli) | ||
bc.CompressorWriter.Reset(bc.compressed) | ||
} | ||
|
||
func NewChannelCompressor(algo CompressionAlgo) (ChannelCompressor, error) { | ||
compressed := &bytes.Buffer{} | ||
if algo == Zlib { | ||
writer, err := zlib.NewWriterLevel(compressed, zlib.BestCompression) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ZlibCompressor{ | ||
BaseChannelCompressor{ | ||
CompressorWriter: writer, | ||
compressed: compressed, | ||
}, | ||
}, nil | ||
} else if algo.IsBrotli() { | ||
compressed.WriteByte(ChannelVersionBrotli) | ||
writer := brotli.NewWriterLevel(compressed, GetBrotliLevel(algo)) | ||
return &BrotliCompressor{ | ||
BaseChannelCompressor{ | ||
CompressorWriter: writer, | ||
compressed: compressed, | ||
}, | ||
}, nil | ||
} else { | ||
return nil, fmt.Errorf("unsupported compression algorithm: %s", algo) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package derive | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var r = rand.New(rand.NewSource(99)) | ||
|
||
func randomBytes(length int) []byte { | ||
b := make([]byte, length) | ||
_, err := r.Read(b) | ||
// Rand.Read always returns nil error | ||
if err != nil { | ||
panic(err) | ||
} | ||
return b | ||
} | ||
|
||
func TestChannelCompressor_NewReset(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
algo CompressionAlgo | ||
expectedResetSize int | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "zlib", | ||
algo: Zlib, | ||
expectedResetSize: 0, | ||
}, | ||
{ | ||
name: "brotli10", | ||
algo: Brotli10, | ||
expectedResetSize: 1, | ||
}, | ||
{ | ||
name: "zstd", | ||
algo: CompressionAlgo("zstd"), | ||
expectedResetSize: 0, | ||
expectErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
scc, err := NewChannelCompressor(tc.algo) | ||
if tc.expectErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expectedResetSize, scc.Len()) | ||
|
||
_, err = scc.Write(randomBytes(10)) | ||
require.NoError(t, err) | ||
err = scc.Flush() | ||
require.NoError(t, err) | ||
require.Greater(t, scc.Len(), tc.expectedResetSize) | ||
|
||
scc.Reset() | ||
require.Equal(t, tc.expectedResetSize, scc.Len()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package derive | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
type CompressionAlgo string | ||
|
||
const ( | ||
// compression algo types | ||
Zlib CompressionAlgo = "zlib" | ||
Brotli9 CompressionAlgo = "brotli-9" | ||
Brotli10 CompressionAlgo = "brotli-10" | ||
Brotli11 CompressionAlgo = "brotli-11" | ||
) | ||
|
||
var CompressionAlgoTypes = []CompressionAlgo{ | ||
Zlib, | ||
Brotli9, | ||
Brotli10, | ||
Brotli11, | ||
} | ||
|
||
var brotliRegexp = regexp.MustCompile(`^brotli-(9|10|11)$`) | ||
|
||
func (algo CompressionAlgo) String() string { | ||
return string(algo) | ||
} | ||
|
||
func (algo *CompressionAlgo) Set(value string) error { | ||
if !ValidCompressionAlgoType(CompressionAlgo(value)) { | ||
return fmt.Errorf("unknown compression algo type: %q", value) | ||
} | ||
*algo = CompressionAlgo(value) | ||
return nil | ||
} | ||
|
||
func (algo *CompressionAlgo) Clone() any { | ||
cpy := *algo | ||
return &cpy | ||
} | ||
|
||
func (algo *CompressionAlgo) IsBrotli() bool { | ||
return brotliRegexp.MatchString(algo.String()) | ||
} | ||
|
||
func GetBrotliLevel(algo CompressionAlgo) int { | ||
switch algo { | ||
case Brotli9: | ||
return 9 | ||
case Brotli10: | ||
return 10 | ||
case Brotli11: | ||
return 11 | ||
default: | ||
panic("Unsupported brotli level") | ||
} | ||
} | ||
|
||
func ValidCompressionAlgoType(value CompressionAlgo) bool { | ||
for _, k := range CompressionAlgoTypes { | ||
if k == value { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package derive | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCompressionAlgo(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
algo CompressionAlgo | ||
isBrotli bool | ||
isValidCompressionAlgoType bool | ||
}{ | ||
{ | ||
name: "zlib", | ||
algo: Zlib, | ||
isBrotli: false, | ||
isValidCompressionAlgoType: true, | ||
}, | ||
{ | ||
name: "brotli-9", | ||
algo: Brotli9, | ||
isBrotli: true, | ||
isValidCompressionAlgoType: true, | ||
}, | ||
{ | ||
name: "brotli-10", | ||
algo: Brotli10, | ||
isBrotli: true, | ||
isValidCompressionAlgoType: true, | ||
}, | ||
{ | ||
name: "brotli-11", | ||
algo: Brotli11, | ||
isBrotli: true, | ||
isValidCompressionAlgoType: true, | ||
}, | ||
{ | ||
name: "invalid", | ||
algo: CompressionAlgo("invalid"), | ||
isBrotli: false, | ||
isValidCompressionAlgoType: false, | ||
}} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
require.Equal(t, tc.isBrotli, tc.algo.IsBrotli()) | ||
if tc.isBrotli { | ||
require.NotPanics(t, func() { GetBrotliLevel((tc.algo)) }) | ||
} else { | ||
require.Panics(t, func() { GetBrotliLevel(tc.algo) }) | ||
} | ||
require.Equal(t, tc.isValidCompressionAlgoType, ValidCompressionAlgoType(tc.algo)) | ||
}) | ||
} | ||
} |