backend/remote-state/consul: Make gzip compression configurable (#8491)

This commit is contained in:
Christoffer Kylvåg 2017-03-13 08:17:33 +01:00
parent e098c7c24a
commit abfa35db7c
4 changed files with 14 additions and 3 deletions

View File

@ -53,6 +53,13 @@ func New() backend.Backend {
Description: "HTTP Auth in the format of 'username:password'", Description: "HTTP Auth in the format of 'username:password'",
Default: "", // To prevent input Default: "", // To prevent input
}, },
"gzip": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Description: "Compress the state data using gzip",
Default: false,
},
}, },
} }

View File

@ -85,11 +85,15 @@ func (b *Backend) State(name string) (state.State, error) {
// Determine the path of the data // Determine the path of the data
path := b.path(name) path := b.path(name)
// Determine whether to gzip or not
gzip := b.configData.Get("gzip").(bool)
// Build the state client // Build the state client
stateMgr := &remote.State{ stateMgr := &remote.State{
Client: &RemoteClient{ Client: &RemoteClient{
Client: client, Client: client,
Path: path, Path: path,
GZip: gzip,
}, },
} }

View File

@ -18,13 +18,13 @@ import (
const ( const (
lockSuffix = "/.lock" lockSuffix = "/.lock"
lockInfoSuffix = "/.lockinfo" lockInfoSuffix = "/.lockinfo"
maxKVSize = 512 * 1024
) )
// RemoteClient is a remote client that stores data in Consul. // RemoteClient is a remote client that stores data in Consul.
type RemoteClient struct { type RemoteClient struct {
Client *consulapi.Client Client *consulapi.Client
Path string Path string
GZip bool
consulLock *consulapi.Lock consulLock *consulapi.Lock
lockCh <-chan struct{} lockCh <-chan struct{}
@ -58,8 +58,7 @@ func (c *RemoteClient) Get() (*remote.Payload, error) {
func (c *RemoteClient) Put(data []byte) error { func (c *RemoteClient) Put(data []byte) error {
payload := data payload := data
// If the payload to be written exceeds the Consul KV byte limit, compress if c.GZip {
if len(data) > maxKVSize {
if compressedState, err := compressState(data); err == nil { if compressedState, err := compressState(data); err == nil {
payload = compressedState payload = compressedState
} else { } else {

View File

@ -53,3 +53,4 @@ The following configuration options / environment variables are supported:
* `datacenter` - (Optional) The datacenter to use. Defaults to that of the agent. * `datacenter` - (Optional) The datacenter to use. Defaults to that of the agent.
* `http_auth` / `CONSUL_HTTP_AUTH` - (Optional) HTTP Basic Authentication credentials to be used when * `http_auth` / `CONSUL_HTTP_AUTH` - (Optional) HTTP Basic Authentication credentials to be used when
communicating with Consul, in the format of either `user` or `user:pass`. communicating with Consul, in the format of either `user` or `user:pass`.
* `gzip` - (Optional) `true` to compress the state data using gzip, or `false` (the default) to leave it uncompressed.