@@ -55,24 +55,14 @@ const (
55
55
processStartTimeHeader = "Process-Start-Time-Unix"
56
56
)
57
57
58
- type Compression int
58
+ type Compression string
59
59
60
60
const (
61
- Identity Compression = iota
62
- Gzip
63
- Zstd
61
+ Identity Compression = "identity"
62
+ Gzip Compression = "gzip"
63
+ Zstd Compression = "zstd"
64
64
)
65
65
66
- var compressions = [... ]string {
67
- "identity" ,
68
- "gzip" ,
69
- "zstd" ,
70
- }
71
-
72
- func (c Compression ) String () string {
73
- return compressions [c ]
74
- }
75
-
76
66
var defaultCompressionFormats = []Compression {Identity , Gzip , Zstd }
77
67
78
68
var gzipPool = sync.Pool {
@@ -143,6 +133,18 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO
143
133
}
144
134
}
145
135
136
+ // Select all supported compression formats
137
+ var compressions []string
138
+ if ! opts .DisableCompression {
139
+ offers := defaultCompressionFormats
140
+ if len (opts .OfferedCompressions ) > 0 {
141
+ offers = opts .OfferedCompressions
142
+ }
143
+ for _ , comp := range offers {
144
+ compressions = append (compressions , string (comp ))
145
+ }
146
+ }
147
+
146
148
h := http .HandlerFunc (func (rsp http.ResponseWriter , req * http.Request ) {
147
149
if ! opts .ProcessStartTime .IsZero () {
148
150
rsp .Header ().Set (processStartTimeHeader , strconv .FormatInt (opts .ProcessStartTime .Unix (), 10 ))
@@ -188,7 +190,7 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO
188
190
}
189
191
rsp .Header ().Set (contentTypeHeader , string (contentType ))
190
192
191
- w , err := GetWriter (req , rsp , opts .DisableCompression , opts . OfferedCompressions )
193
+ w , err := NegotiateEncodingWriter (req , rsp , opts .DisableCompression , compressions )
192
194
if err != nil {
193
195
if opts .ErrorLog != nil {
194
196
opts .ErrorLog .Println ("error getting writer" , err )
@@ -419,48 +421,42 @@ func httpError(rsp http.ResponseWriter, err error) {
419
421
)
420
422
}
421
423
422
- func GetWriter (r * http.Request , rsp http.ResponseWriter , disableCompression bool , offeredCompressions []Compression ) (io.Writer , error ) {
424
+ func NegotiateEncodingWriter (r * http.Request , rsp http.ResponseWriter , disableCompression bool , compressions []string ) (io.Writer , error ) {
423
425
w := io .Writer (rsp )
424
- rsp .Header ().Set (contentEncodingHeader , "identity" )
425
- if ! disableCompression {
426
- offers := defaultCompressionFormats
427
- if len (offeredCompressions ) > 0 {
428
- offers = offeredCompressions
429
- }
430
- var compressions []string
431
- for _ , comp := range offers {
432
- compressions = append (compressions , comp .String ())
426
+
427
+ // TODO(mrueg): Replace internal/github.com/gddo once https://github.com/golang/go/issues/19307 is implemented.
428
+ compression := httputil .NegotiateContentEncoding (r , compressions )
429
+ rsp .Header ().Set (contentEncodingHeader , compression )
430
+
431
+ if disableCompression {
432
+ return w , nil
433
+ }
434
+
435
+ switch compression {
436
+ case "zstd" :
437
+ // TODO(mrueg): Replace klauspost/compress with stdlib implementation once https://github.com/golang/go/issues/62513 is implemented.
438
+ z , err := zstd .NewWriter (rsp , zstd .WithEncoderLevel (zstd .SpeedFastest ))
439
+ if err != nil {
440
+ return nil , err
433
441
}
434
- // TODO(mrueg): Replace internal/github.com/gddo once https://github.com/golang/go/issues/19307 is implemented.
435
- compression := httputil .NegotiateContentEncoding (r , compressions )
436
- switch compression {
437
- case "zstd" :
438
- rsp .Header ().Set (contentEncodingHeader , "zstd" )
439
- // TODO(mrueg): Replace klauspost/compress with stdlib implementation once https://github.com/golang/go/issues/62513 is implemented.
440
- z , err := zstd .NewWriter (rsp , zstd .WithEncoderLevel (zstd .SpeedFastest ))
441
- if err != nil {
442
- return nil , err
443
- }
444
442
445
- z .Reset (w )
446
- defer z .Close ()
443
+ z .Reset (w )
444
+ defer z .Close ()
447
445
448
- w = z
449
- case "gzip" :
450
- rsp .Header ().Set (contentEncodingHeader , "gzip" )
451
- gz := gzipPool .Get ().(* gzip.Writer )
452
- defer gzipPool .Put (gz )
446
+ w = z
447
+ case "gzip" :
448
+ gz := gzipPool .Get ().(* gzip.Writer )
449
+ defer gzipPool .Put (gz )
453
450
454
- gz .Reset (w )
455
- defer gz .Close ()
451
+ gz .Reset (w )
452
+ defer gz .Close ()
456
453
457
- w = gz
458
- case "identity" :
459
- // This means the content is not compressed.
460
- default :
461
- // The content encoding was not implemented yet.
462
- return w , fmt .Errorf ("content compression format not recognized: %s. Valid formats are: %s" , compression , defaultCompressionFormats )
463
- }
454
+ w = gz
455
+ case "identity" :
456
+ // This means the content is not compressed.
457
+ default :
458
+ // The content encoding was not implemented yet.
459
+ return w , fmt .Errorf ("content compression format not recognized: %s. Valid formats are: %s" , compression , defaultCompressionFormats )
464
460
}
465
461
return w , nil
466
462
}
0 commit comments