Skip to content

add lfs.httpDownloadEncoding with zstd and gzip support - #6196

Merged
chrisd8088 merged 14 commits into
git-lfs:mainfrom
mzr:httpdownloadencoding
Apr 22, 2026
Merged

chrisd8088 merged 14 commits into
git-lfs:mainfrom
mzr:httpdownloadencoding

Conversation

@mzr

@mzr mzr commented Jan 21, 2026

Copy link
Copy Markdown
Contributor

This is to address the issue #6193.

Currently the default encoding is gzip, which golang's library adds automatically and transparently from the point of view of the git-lfs.

This commit adds configs lfs.httpDownloadEncoding / lfs.<url>.httpDownloadEncoding, which allow the git-lfs client to specify which encoding should the backend send the objects encoded with. The HTTP's Accept-Encoding header is used.

The config can be scoped to a url, and only affects the http urls that are returned as a download action from the batch endpoint response. It's because not every object provider can support zstd.

@mzr
mzr requested a review from a team as a code owner January 21, 2026 21:52
@chrisd8088

Copy link
Copy Markdown
Member

Hey, thanks very much for drafting this PR! I'll take a detailed look as soon as I'm able.

@chrisd8088

Copy link
Copy Markdown
Member

Hey, thanks so much for this PR!

I've been reviewing it over the last few days and am still writing up my notes, but in the process I've turned up a number of problems with our existing test suite which I'd like to fix first, so that I don't have to complicate this PR's review with pre-existing bug fixes.

Thanks again and I'll update this PR as soon as I can!

@chrisd8088 chrisd8088 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for this PR!

Reviewing it has certainly been an educational experience and one which led to multiple avenues of investigation!

My PRs #6195 and #6207 were prompted by this PR, and in addition, your PR led to an illuminating discussion in klauspost/compress#1129 regarding zstd frames and unexpected EOFs, and also highlighted a pre-existing (but minor) bug with our progress logs.

I see there is a proposal in golang/go#62492 to add support in the net/http package for automatic zstd decompression, which looks to depend on the introduction of a public compress/zstd module, as tracked by golang/go#62513. However, neither appears likely to be addressed in the short term, so I think this PR is valuable.

I developed PR #6207 in the process of writing up my review comments, and at a minimum I'd like to see that merged first so that this PR's tests can mirror the gzip-encoding test I refactored out of an existing test. Note that I renamed the test and moved it into a new script named t/t-batch-storage-encoding.sh.

I also experimented with a number of potential enhancements to this PR to try to address the issues which turned up during my review, and ultimately drafted a series of commits in my zstd-download-encoding branch that I hope could be pulled into this PR.

Note that my branch starts from this PR's current state, merges PR #6207's branch, and then adds another group of commits on top.

I didn't try to address everything in that branch, just some of the key issues regarding transfer queue workers, multi-threading, and HTTP retry handling, plus a couple of other details.

I also tried to make sure the server we use in our shell test suite will send zstd-encoded responses when requested, so our tests can actually verify that we decode those responses correctly.

However, I didn't make any changes to the documentation or the placement of the github.com/klauspost/compress module in our go.mod file; those points still need to addressed.

At any rate, let me know what you think of all of that, and if you're willing to pull my changes into this PR, then we can ask for a review from the other member of our core team to get a third pair of eyes on the code.

Thank you again for proposing this new feature and writing up this PR! We really appreciate initiatives like this from our community!

Comment thread tq/basic_download.go Resolved
Comment thread tq/basic_download.go Resolved
Comment thread docs/man/git-lfs-config.adoc Resolved
Comment thread docs/man/git-lfs-config.adoc Resolved
Comment thread t/t-httpdownloadencoding.sh Resolved
Comment thread t/t-httpdownloadencoding.sh Resolved
Comment thread t/t-httpdownloadencoding.sh Resolved
Comment thread tq/basic_download.go Resolved
Comment thread tq/basic_download.go Resolved
Comment thread go.mod Resolved
@mzr

mzr commented Mar 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @chrisd8088 for a very thorough review! I'm impressed by your attention to detail in this PR and in the PRs you handled beforehand to make this one fit better into git-lfs.

I looked into some easy-to-address comments, but as I went through them I realised I need a proper sitting with this.

I agree it would be ideal to just reuse zstd from golang, but if that's doesn't get traction, it shouldn't stop us from doing it ourselves here.

First thing I'll do, I'll rebase on top your branch, then I'll address your comments.

@chrisd8088

Copy link
Copy Markdown
Member

Just FYI, we've merged PR #6207 now, so if you rebase on main, I think that should simplify this PR a bit, since the t/t-batch-storage-encoding.sh script is now part of our main development branch.

@mzr
mzr force-pushed the httpdownloadencoding branch from 76309bc to f5cda14 Compare April 1, 2026 09:29
@mzr

mzr commented Apr 1, 2026

Copy link
Copy Markdown
Contributor Author

Squashed & rebased

@chrisd8088

Copy link
Copy Markdown
Member

Thank you for rebasing this PR, it's looking really good to me!

Because I contributed a fair amount to this PR, I'm going to ask for a review from another maintainer of the project before we merge this PR.

Before I do that, though, I'd like to "un-squash" the changes from my zstd-download-encoding branch, specifically the commits from 5289261 to ec9ba2e, inclusive (i.e., the range 16fc463b8a01..ec9ba2e9bcca). I'm happy to take care of this, though, if you don't mind my force-pushing to your branch.

I divided up my work into a series of commits so that each one has a discrete, logically-related set of changes and a detailed description of the purpose and context for those changes. That should make it easier for my colleague to review the PR as a whole and understand how the pieces all fit together.

(The other reason I try to include notes with each of my commits is so that in the future they'll help me—or anyone else looking at the history of the project—figure out why we did something a particular way!)

Other than "un-squashing", though, I think we're ready for a second review.

FWIW, I ran a few tests just to make sure that the addition of the per-URL configuration option search to the download() method of the tq package's basicDownloadAdapter structure didn't have a material impact on the performance of the download transfer queue, and it seems fine to me. On my older macOS system with a modestly-sized Git configuration file, the search for a matching lfs.transfer.<url>.httpDownloadEncoding option adds about 20 μs to each Git LFS object download, which is dwarfed by the HTTP transfer time and the filesystem I/O.

(It's maybe worth noting here that the current Git configuration is only read once. Each search is then just performed using a copy of the cached configuration data.)

Thanks again so much for contributing this PR and initiating this new feature for Git LFS!

@mzr

mzr commented Apr 6, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for this great summary and driving a ton of changes to deliver this feature! I don't mind you force pushing to my branch at all.

mzr and others added 13 commits April 8, 2026 22:45
In prior commits in this PR we introduced a new configuration option
named "lfs.httpDownloadEncoding", which may be set to either of the
values "gzip" or "zstd", with a default value of "gzip".

When our new option is not defined or is set to "gzip", the Git LFS
client simply follows its legacy behaviour when making object transfer
download requests over HTTP.  Specifically, the client should allow the
"net/http" package in the Go standard library to insert an
"Accept-Encoding: gzip" header into the requests, and if the server
chooses to respond with gzip-encoded data, the client should expect
the "net/http" package to automatically decode the body of the response.

A test of this legacy behaviour was first introduced in PR git-lfs#4345.
More recently, in PR git-lfs#6207, we created the "t/t-batch-storage-encoding.sh"
shell script and moved the test into that script, while also renaming
the test to "batch storage HTTP download with gzip encoding".

We now expand the test to verify that the client continues to behave
as it has previously when our new configuration option is explicitly
set to "gzip", either universally or for a specific URL.
In prior commits in this PR we introduced a new configuration option
named "lfs.httpDownloadEncoding", which may be set to either of the
values "gzip" or "zstd", with a default value of "gzip".  We also
added a set of tests to confirm that the Git LFS client sends an
Accept-Encoding header with the selected encoding type when making
object transfer download HTTP requests, and that the client is able
to decode the response data if the server chooses to encode it.

We now move those tests into the "t/t-batch-storage-encoding.sh"
script we created recently in PR git-lfs#6207, and rename and refactor them
to follow the design of the existing test in that script.  In doing
so, we combine the "lfs.httpDownloadEncoding zstd" test and the
"lfs.<url>.httpDownloadEncoding zstd" test into a single test
named "batch storage HTTP download with zstd encoding".

We also elide the "lfs.httpDownloadEncoding gzip does not set header"
test, as the existing "batch storage HTTP download with gzip encoding"
test should be sufficient to demonstrate that when our new configuration
option is explicitly set to "gzip", all the Accept-Encoding headers
we expect the client to send have the value "gzip".

In our "batch storage HTTP download with zstd encoding" test we check
that the client sends an "Accept-Encoding: zstd" header when downloading
individual objects, but still sends an "Accept-Encoding: gzip" header
in its Batch API requests, since we only support zstd encoding for
object transfer downloads.  We use the -c option to the grep(1) command
to verify that we see the expected number of Accept-Encoding headers
with each value.

As well as checking the Accept-Encoding headers, we also update the
test and our "lfstest-gitserver" utility program so that we can confirm
that the Git LFS client properly decodes the object data it receives
from a server, if a "Content-Encoding: zstd" response header indicates
that the data is zstd-encoded.

First, we enhance the "lfstest-gitserver" utility so that when handling
a GET request for an object, if the object's content is the special key
string "storage-download-encoding-zstd" and if an "Accept-Encoding: zstd"
header was sent, then the program returns a response with a
"Content-Encoding: zstd" header and a zstd-encoded body.

Next, in our "batch storage HTTP download with zstd encoding" test we
check that a single "Content-Encoding: zstd" header was received by each
"git lfs pull" command, and we use our assert_local_object() assertion
to verify that a local copy of the object has been created.

In a previous commit in this PR we updated the "batch storage HTTP
download with gzip encoding" test so that it repeats its checks
of the client's default behaviour with the "lfs.httpDownloadEncoding"
option explicitly set to the value "gzip", and then repeats the checks
again with a URL-specific version of the option set to the value "gzip".

We follow this pattern in our "batch storage HTTP download with zstd
encoding" test, except that the test does not exercise the client's
default behaviour, since the client sends an "Accept-Encoding: gzip"
header when performing object downloads in that case.

In both tests, when we set the URL-specific configuration option, we
use the value of the GITSERVER shell variable for the URL, since it is
conveniently available and generic across all our tests' repositories.

We also ascertain that each "git lfs pull" command succeeds by checking
the first element of the PIPESTATUS array variable.  This is necessary
because the shell's "errexit" attribute ignores the exit codes of all
commands in a pipeline other than the final one, and we pipe the output
of our "git lfs pull" commands into the tee(1) command to capture a
log file.  As in many of the other tests in our suite, we use the name
"pull.log" for this file.
In prior commits in this PR we introduced a new configuration option
named "lfs.httpDownloadEncoding", which may be set to either of the
values "gzip" or "zstd", with a default value of "gzip".

Since this option pertains strictly to object transfer downloads,
we now adjust the prefix of the option from the generic "lfs" to the
more-specific "lfs.transfer".

This change should further clarify the option's scope by grouping it
together with our existing options that have the same prefix and which
also pertain only to object transfers, such as "lfs.transfer.maxRetries"
and "lfs.transfer.maxRetryDelay".
In prior commits in this PR we introduced a new configuration option
named "lfs.transfer.httpDownloadEncoding", which may be set to either
of the values "gzip" or "zstd", with a default value of "gzip".

When the option is set to the value "zstd", the Git LFS client will
send an "Accept-Encoding: zstd" header when it makes an object transfer
download HTTP request.  If the server chooses, it may then reply with
a response that includes a "Content-Encoding: zstd" header and whose
body is zstd-encoded.  When this occurs, the client should recognize
the Content-Encoding header and attempt to decode the response body
using the zstd algorithm.

According to the HTTP specification, the "content coding" value of a
Content-Encoding header is case-insensitive.  For this reason, when
the "net/http" package of the Go standard library checks whether a
Content-Encoding header has the value "gzip", it performs a case-
insensitive match:

  https://www.rfc-editor.org/rfc/rfc9110.html#section-8.4.1
  https://github.com/golang/go/blob/go1.25.7/src/net/http/transport.go#L2381

We should therefore also perform a case-insensitive comparison when
checking whether a Content-Encoding header matches the value "zstd".
To do this, we update the download() method of the basicDownloadAdapter
structure in our "tq" package so that it converts the header's value
to lowercase before testing its value.

We also take the opportunity to simplify another portion of the
download() method that we added in a previous commit in this PR.
When we read the value of the "lfs.transfer.httpDownloadEncoding"
configuration option, we do not need to remove leading or trailing
whitespace from the value, as Git will remove any excess whitespace
when reading values from its configuration files.

(Note, though, that if the user has explicitly configured an option
with a quoted value that contains leading or trailing whitespace inside
the quotation marks, then Git will preserve that whitespace.  In this
case, however, we should not trim the excess whitespace but treat the
value as a literal.)
In prior commits in this PR we introduced a new configuration option
named "lfs.transfer.httpDownloadEncoding", which may be set to either
of the values "gzip" or "zstd", with a default value of "gzip".

When our new option is not defined or is set to "gzip", the Git LFS
client simply follows its legacy behaviour when making object transfer
download requests over HTTP.  Specifically, the client should allow the
"net/http" package in the Go standard library to insert an
"Accept-Encoding: gzip" header into the requests, and if the server
chooses to respond with gzip-encoded data, the client should expect
the "net/http" package to automatically decode the body of the response.

A test of this legacy behaviour was first introduced in PR git-lfs#4345.
More recently, in PR git-lfs#6207, we created the "t/t-batch-storage-encoding.sh"
shell script and moved the test into that script, while also renaming
the test to "batch storage HTTP download with gzip encoding".

In order for our test to detect that an object transfer download request
was gzip-encoded, in PR git-lfs#4345 we also updated the DoWithRedirect()
method of the Client structure in our "lfshttp" package.  The method
checks whether the "net/http" package has set the Uncompressed field
in the Response structure it returns, which would indicate that the
response body was gzip-encoded and that it has been decoded for us.

If the Uncompressed field is set, our DoWithRedirect() method outputs
a trace log message.  The test is then able to use the presence of
this message in the logs it captures from each "git lfs pull" command
to determine that the client received a gzip-encoded response to an
object transfer download request and handled it appropriately.

We would now like to include a similar check in the "batch storage
HTTP download with zstd encoding" test that we added to our
"t/t-batch-storage-encoding.sh" shell script in previous commits
in this PR.

This test verifies that the changes we have made in this PR work as
expected to implement support for zstd-encoded object HTTP downloads.
Specifically, when our new configuration option is set to the value
"zstd", the Git LFS client should send an "Accept-Encoding: zstd"
header with each object download request, and should decode the
response body appropriately if the server chooses to encode it using
the zstd algorithm.

We therefore update the download() method of the basicDownloadAdapter
structure in our "tq" package to output a trace log message when
processing a zstd-encoded response to an object transfer download
request.  We then update our "batch storage HTTP download with zstd
encoding" test to check for this message in the logs it captures from
each "git lfs pull" command.
In prior commits in this PR we introduced a new configuration option
named "lfs.transfer.httpDownloadEncoding", which may be set to either
of the values "gzip" or "zstd", with a default value of "gzip".

When our new option is not defined or is set to "gzip", the Git LFS
client simply follows its legacy behaviour when making object transfer
download requests over HTTP.  Specifically, the client should allow the
"net/http" package in the Go standard library to insert an
"Accept-Encoding: gzip" header into the requests, and if the server
chooses to respond with gzip-encoded data, the client should expect
the "net/http" package to automatically decode the body of the response.

By contrast, when our new option is set to the value "zstd", the
Git LFS client will explicitly add an "Accept-Encoding: zstd" header
when it makes an object transfer download HTTP request.  If the server
chooses to encode the body of its response, it should include a
"Content-Encoding: zstd" header which the client will recognize and
then attempt to decode the response body using the zstd algorithm.

In either case, the server may also send a Content-Length header, in
which case the value of that header will be the length of the encoded
content and not the length of the original, uncompressed data.

For this reason, when the "net/http" package handles gzip-encoded
response data, it returns a Response structure whose ContentLength
field is set to -1.  This informs any reader of the data stream in
the structure's Body field that the length of the stream is unknown:

  https://github.com/golang/go/blob/go1.25.7/src/net/http/response.go#L90-L92
  https://github.com/golang/go/blob/go1.25.7/src/net/http/transport.go#L2385

The same issue pertains when we handle a zstd-encoded response to an
object download request, except that the "net/http" package is not
responsible for decoding the data in the response body.  Instead, we
decode this data in the download() method of the basicDownloadAdapter
structure in our "tq" package.

Later in its operation, the download() method passes the Response
structure's ContentLength field to the CopyWithCallback() function
from our "tools" package.  Thus when handling a response with a
Content-Length header and a zstd-encoded body, we currently pass an
inaccurate value to the CopyWithCallback() function, one which
represents the length of the encoded object data and not its
uncompressed actual size.

We therefore update the download() to set the ContentLength field
to -1 after decoding a zstd-encoded response body, to match the
behaviour of the "net/http" package when it decodes a gzip-encoded
response body.

One consequence of this change will be visible if a user has set the
GIT_LFS_PROGRESS environment variable to an absolute file path.
In this case, the progress log messages we write to that path will
contain a misleading "-1" value as the denominator of the fraction
which indicates how much object data has been received so far.

However, this is a known issue which already occurs when a server
returns a gzip-encoded response body, and one which we expect to
address comprehensively in a future PR.  See, for reference:

  git-lfs#6011 (comment)
In prior commits in this PR we introduced a new configuration option
named "lfs.transfer.httpDownloadEncoding", which may be set to either
of the values "gzip" or "zstd", with a default value of "gzip".

When the option is set to the value "zstd", the Git LFS client will
send an "Accept-Encoding: zstd" header when it makes an object transfer
download HTTP request.  If the server chooses, it may then reply with
a response that includes a "Content-Encoding: zstd" header and whose
body is zstd-encoded.  When this occurs, the client should recognize
the Content-Encoding header and attempt to decode the response body
using the zstd algorithm.

To decode a zstd-encoded response body we use the "zstd" package from
the "github.com/klauspost/compress" module.  By default, this package
decodes a stream of data in four asynchronous stages, using four
separate goroutines.

Our object transfer queue normally runs using eight separate worker
goroutines, although this number can be raised or lowered using the
"lfs.concurrentTransfers" configuration option.

Many users do not adjust the number of transfer queue workers, so if
we continue to use the "zstd" package's default settings then we might
often start up to 32 goroutines when downloading zstd-compressed
objects, which may exceed the available number of CPU cores.

To avoid this situation and to align our support of zstd-encoded
object transfer downloads with our support of gzip-encoded downloads,
we limit the "zstd" package to single-threaded decoding only.
Specifically, when initializing a new stream decoder, we use the
WithDecoderConcurrency() function to set the number of concurrent
decoding goroutines to just one.

In the future, we may want to introduce a configuration option to
specify the maximum number of decoding routines, or we might try to
set this limit automatically based on the number of available CPU
cores.  We defer this decision for now, however, until we have
more experience with our initial implementation from this PR.
In prior commits in this PR we introduced a new configuration option
named "lfs.transfer.httpDownloadEncoding", which may be set to either
of the values "gzip" or "zstd", with a default value of "gzip".

When the option is set to the value "zstd", the Git LFS client will
send an "Accept-Encoding: zstd" header when it makes an object transfer
download HTTP request.  If the server chooses, it may then reply with
a response that includes a "Content-Encoding: zstd" header and whose
body is zstd-encoded.  When this occurs, the client should recognize
the Content-Encoding header and attempt to decode the response body
using the zstd algorithm.

To decode a zstd-encoded response body we use the "zstd" package from
the "github.com/klauspost/compress" module.  The documentation for
this package notes that its Decoder structures are designed to be
reused to decode multiple data streams in succession, as this avoids
the allocation costs incurred when a new Decoder structure is
initialized:

  https://github.com/klauspost/compress/blob/v1.18.4/zstd/decoder.go#L17-L20

Our object transfer queue normally runs using eight separate worker
goroutines, each of which sequentially downloads multiple Git LFS
objects.  A single Git LFS command may download large numbers of
objects, so we should try to reuse the Decoder structures instead of
initializing a new structure for each object download request.

To make this possible, we define a new basicDownloadAdapterWorkerContext
structure in our "tq" package, which we can pass in place of the "ctx"
parameter to the DoTransfer() and WorkerEnding() methods of our
basicDownloadAdapter structure.  The basicDownloadAdapterWorkerContext
structure contains a "zstdDecoder" field which we initialize with a
pointer to a Decoder structure the first time a worker goroutine needs
to decode a zstd-encoded response body.

The DoTransfer() method uses a type assertion to confirm that its
"ctx" parameter is a pointer to a basicDownloadAdapterWorkerContext
structure, and then passes this pointer to the download() method
of the basicDownloadAdapter structure, which in turn may pass it
to itself recursively if an HTTP request needs to be repeated.

If the download() method receives an HTTP response with a
"Content-Encoding: zstd" header, it now checks whether the "zstdDecoder"
field in the basicDownloadAdapterWorkerContext structure is nil.  If it
is, the method initializes a new Decoder structure from the "zstd"
package, passing the data stream with the zstd-encoded response body.
The method also outputs a trace log message stating that a new
decoder has been initialized.

If the "zstdDecoder" field is not nil, however, the download() method
simply passes the response body's data stream to the existing Decoder
structure's Reset() method, thus obviating the need to initialize a
new Decoder structure.

When each worker goroutine finishes downloading all the objects it is
assigned, the WorkerEnding() method will be called for that goroutine.
We enhance this method so that it checks whether its "ctx" parameter
is a pointer to a basicDownloadAdapterWorkerContext structure, and if
so and if the structure's "zstdDecoder" field is not nil, then the
WorkerEnding() method calls the Decoder structure's Close() method,
which should release the resources that were allocated when Decoder
structure was first initialized.  The WorkerEnding() method also
outputs a trace log message to report that the decoder has been closed.

To verify that our changes are effective, we introduce a new test
named "batch storage HTTP download with zstd encoding and multiple
objects" to our "t/t-batch-storage-encoding.sh" script.

This new test is similar to our existing "batch storage HTTP download
with zstd encoding" test, but it creates three Git LFS objects rather
than just one.  As well, before downloading these objects, the new test
sets the "lfs.concurrentTransfers" configuration option to the value
of two, which will force the Git LFS client's transfer queue to start
no more than two worker goroutines, instead of the usual eight.

After performing a "git lfs pull" command, which should retrieve all
three objects, the test then checks that the trace log it has captured
from the command includes exactly two messages stating that new
decoders were initialized, and another two messages stating that
the decoders were closed.  As well, the test validates that the
responses to the object transfer download requests for all three
objects had zstd-encoded data.

Lastly, to enable our new test to function, we update our
"lfstest-gitserver" test utility so that it recognizes the special
key strings in all three objects' content when responding to a GET
request for object data, and returns a "Content-Encoding: zstd"
header along with a zstd-encoded response body.
In prior commits in this PR we introduced a new configuration option
named "lfs.transfer.httpDownloadEncoding", which may be set to either
of the values "gzip" or "zstd", with a default value of "gzip".

When the option is set to the value "zstd", the Git LFS client will
send an "Accept-Encoding: zstd" header when it makes an object transfer
download HTTP request.  If the server chooses, it may then reply with
a response that includes a "Content-Encoding: zstd" header and whose
body is zstd-encoded.  When this occurs, the client should recognize
the Content-Encoding header and attempt to decode the response body
using the zstd algorithm.

If the response to an object transfer download request is interrupted,
the client may only receive a portion of the object data, and when the
data is zstd-encoded, the client may only be able to decode an even
smaller amount of the data than was received, or none at all.

During an object transfer download, the Git LFS client writes the data
it receives into a temporary file in the ".git/lfs/incomplete"
directory.  If the download is interrupted but some data has been
received and written to the temporary file, the client renames the
file with the extension ".part" and leaves the file in place so it is
available if the client tries to download the object again.

While executing a single command, the client will retry a failed
object transfer download request up to the limit defined by the
"lfs.transfer.maxRetries" configuration option.  Each time the client
attempts to download the object again, if partial data is available in
a file with the ".part" extension in the ".git/lfs/incomplete" directory,
the client will make an HTTP request with a Range header that specifies
only the outstanding portion of the object data.

Further, if a command fails to completely download an object, but
another Git LFS command is run later to download the same object, it
will also make a request with a Range header if an incomplete portion
of the object's data exists in a file with the ".part" extension.

When a Range header is set for an HTTP request, the "net/http" package
in the Go standard library will not automatically insert an
"Accept-Encoding: gzip" header:

  https://github.com/golang/go/blob/go1.25.7/src/net/http/transport.go#L2768
  https://github.com/golang/go/blob/go1.25.7/src/net/http/transport.go#L2779-L2781

While the HTTP specification allows both Range and Accept-Encoding
headers to appear in a request, if the content is encoded by the server
then the start and end position indexes in the Range header should be
interpreted as offsets into the encoded content:

  https://www.rfc-editor.org/rfc/rfc9110.html#section-14.1.2-3

Since the "net/http" package will decode a gzip-encoded response body
if the package added an "Accept-Encoding: gzip" header to the request,
the caller may never be aware that the server supports encoding.  Hence
any start and end positions provided in a Range header are likely to be
offsets into the full, uncompressed source data, and so the "net/http"
package does not insert an Accept-Encoding header when a Range header
is defined.

In the case where our Git LFS client sends a request with an
"Accept-Encoding: zstd" header, we are responsible for decoding the
response body, if the server chooses to compress the source data with
the zstd algorithm.  Should a zstd-encoded response be interrupted,
we may have written some of the decoded data to a temporary file,
so when we retry the request, the byte indexes we use in the Range
header will be offsets into the original object data and not offsets
into its encoded version.

Therefore, we avoid adding an "Accept-Encoding: zstd" header when we
retry a request, unless no temporary file exists for the given object,
or a temporary file exists but is empty.

To implement this change we simply modify the download() method of the
basicDownloadAdapter structure in our "tq" package so that it only
sends an "Accept-Encoding: zstd" header when the "fromByte" variable
has a zero value.  This variable will be set to the size of any
temporary file with previously-downloaded data for the current object,
and so a Range header will be included in the next request with the
"fromByte" variable's value used as the start position byte index.

(In theory, we could try to determine where the encoded data stream
from the response was interrupted, if we read from the stream before
we passed it the Decoder structure from the "zstd" package of the
"github.com/klauspost/compress" module.  Our intermediate reader
would then be able to detect when an unexpected EOF appeared in the
encoded data stream.  However, this would also entail significant
other changes, as we would need to retain the offset into the
encoded data stream between retry attempts, and potentially even
between attempts made by separate Git LFS commands.)

To verify that our changes are effective, we introduce a new "batch
storage HTTP download with zstd encoding retries with Range header
and without encoding" test to our "t/t-batch-storage-encoding.sh" shell
script.  This test creates an object whose content is the special key
string "storage-download-encoding-zstd-retry-range", which our
"lfstest-gitserver" test utility will now recognize when responding
to a GET request for an object.

We update our "lfstest-gitserver" utility so that when this special
content key is detected, the program calls the handleRangeRequest()
function we recently developed in PR git-lfs#6207.  This function handles
requests that are expected to have a Range header, and will return a
400 Bad Request status code to the client if it also sent an
Accept-Encoding header.

However, when the "git lfs pull" command run by our new test makes
its first object transfer download request, it will not send a Range
header but will send an "Accept-Encoding: zstd" header.  To simulate
an interrupted response to this first request, we further update
the "lfstest-gitserver" utility so that it returns only one frame
of zstd-encoded data, representing half of the complete object, plus
some but not all of the remaining encoded data.

By encoding the object's data in two separate frames, and sending at
least the initial frame, we guarantee that the client will be able to
fully decode the first frame, and so some data will be written to a
temporary file when the request is interrupted.  Otherwise the client
will leave an empty temporary file and so will keep retrying the request
with an "Accept-Encoding: zstd" header and without a Range header.

After the "git lfs pull" command completes, our new test then checks
the messages in the trace log it captured of the command's output.
The test confirms that only one request with an "Accept-Encoding: zstd"
header was made, and that the client resumed by making another request
with a Range header, to which the server responded with a 206 Partial
Content status code and a Content-Range header with the expected
byte range indexes for the missing portion of the object's data.
In a prior commit in this PR we added the "zstd" package from the
"github.com/klauspost/compress" Go module as a dependency, so we
now run the "go mod tidy" command to update our "go.mod" file
appropriately.

Co-authored-by: Jan Mazur <[email protected]>
In prior commits in this PR we introduced a new configuration option
named "lfs.transfer.httpDownloadEncoding", which may be set to either
of the values "gzip" or "zstd", with a default value of "gzip".  We
also support this new option in a URL-specific version, akin to the
"http.<url>.*" options provided by Git.

When either form of the option is set to the value "zstd", the Git LFS
client will send an "Accept-Encoding: zstd" header when it makes an
object transfer download HTTP request.  If the server chooses, it may
then reply with a response that includes a "Content-Encoding: zstd"
header and whose body is zstd-encoded.

In one of our prior commits we adjusted the name of this configuration
option from its original version, "lfs.httpDownloadEncoding", but did
not update the new documentation of the option in our git-lfs-config(5)
manual page, so we do that now.

As well, we take the opportunity to move the new option's entry into
the group of entries with the same "lfs.transfer" prefix, and we revise
our description of the new option slightly to try to make it as concise
as possible while also clarifying several details.

Co-authored-by: Jan Mazur <[email protected]>
@chrisd8088
chrisd8088 force-pushed the httpdownloadencoding branch from f5cda14 to b97c13e Compare April 9, 2026 23:19
@chrisd8088

Copy link
Copy Markdown
Member

I don't mind you force pushing to my branch at all.

Wonderful, thank you very much. I've done so, and now we'll just look for a second reviewer on this before we merge.

Thanks again!

@larsxschneider larsxschneider left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work. Thank you 🙇

Comment thread tq/basic_download.go
context, ok := ctx.(*basicDownloadAdapterWorkerContext)
if !ok {
tracerx.Printf("context object for basic download transfer adapter was of the wrong type")
return

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this would happen, then this would be a bug in the code, no? Shouldn't we rather panic to make any test fail instead of a silent log?

Granted, the error in DoTransfer would trigger first anyways and return surface the error.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it would be a bug!

This is the idiom we already use in tq/custom.go in the equivalent WorkerEnding() method for the customAdapter structure. (It's been in place since PR #1367 in 2016, apparently.)

Meanwhile, in tq/ssh.go, we don't even bother to check the type assertion.

I'm comfortable with the logic here whereby the DoTransfer() method will raise an error if there's a problem, and so WorkerEnding() just needs to log the same, since at this point the process must be shutting down anyway.

If we run a test with the existing custom transfer adapter after injecting an invalid type in the return value from the WorkerStarting() method, we get errors like the following, which seem sufficient to me:

context object for custom transfer "testcustom" was of the wrong type
context object for custom transfer "testcustom" was of the wrong type
...
error: failed to push some refs to 'http://127.0.0.1:56559/test-custom-transfer-1'

If you feel we ought to do more than this, though, I think that should be something we change in all the transfer adapters at the same time in a separate PR, so I'm still inclined to leave this as written in order to match the existing idiom for now.

@chrisd8088
chrisd8088 merged commit c93d0cd into git-lfs:main Apr 22, 2026
28 of 30 checks passed
@chrisd8088 chrisd8088 mentioned this pull request Apr 23, 2026
@slonopotamus

Copy link
Copy Markdown
Contributor

Is there a reason why zstd is not accepted by default?

Also: is it possible to enable zstd via repo .lfsconfig?

@chrisd8088

Copy link
Copy Markdown
Member

Is there a reason why zstd is not accepted by default?

@slonopotamus — There's no reason which prevents this being made the default in a future release. I held back from doing so in this PR both because we try to be a bit cautious about changing long-standing defaults, and because it will complicate the implementation for the reason I described in #6196 (comment) above.

Go's net/http package automatically inserts an Accept-Encoding: gzip request header if no Accept-Encoding header is specified (and if the DisableCompression field is not set to true in the http.Transport structure), and then if the response includes a Content-Encoding: gzip header, Go automatically decompresses the response body

However, the net/http package only performs that automatic gzip-decompression if the package itself added the Accept-Encoding: gzip request header.

So, if we explicitly set an Accept-Encoding: zstd, gzip request header, then we have handle gzip-decompression ourselves. While that's perfectly possible, it was more than I thought we should try to achieve in a first implementation.

If there's demand for zstd support by default, that's obviously something which can be addressed in a future PR. Of course, there's the possibility that some Git LFS services may not correctly handle an Accept-Encoding: zstd, gzip request header, but since that's a well-formed HTTP header, it would be a problem those services would need to address on their own.

Also: is it possible to enable zstd via repo .lfsconfig?

Not at the moment, but if someone wants to propose adding that feature, or simply making zstd support the default, we'd certainly be glad to take a look at any PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants