blob: dadd21e40f93261075b6f754ec83419cb543e53c [file] [log] [blame]
William Kurkianea869482019-04-09 15:16:11 -04001/*
2 *
3 * Copyright 2017 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19// Package encoding defines the interface for the compressor and codec, and
20// functions to register and retrieve compressors and codecs.
21//
Abhay Kumara61c5222025-11-10 07:32:50 +000022// # Experimental
23//
24// Notice: This package is EXPERIMENTAL and may be changed or removed in a
25// later release.
William Kurkianea869482019-04-09 15:16:11 -040026package encoding
27
28import (
29 "io"
bseeniva0b9cbcb2026-02-12 19:11:11 +053030 "slices"
William Kurkianea869482019-04-09 15:16:11 -040031 "strings"
Abhay Kumara61c5222025-11-10 07:32:50 +000032
bseeniva0b9cbcb2026-02-12 19:11:11 +053033 "google.golang.org/grpc/encoding/internal"
Abhay Kumara61c5222025-11-10 07:32:50 +000034 "google.golang.org/grpc/internal/grpcutil"
William Kurkianea869482019-04-09 15:16:11 -040035)
36
37// Identity specifies the optional encoding for uncompressed streams.
38// It is intended for grpc internal use only.
39const Identity = "identity"
40
bseeniva0b9cbcb2026-02-12 19:11:11 +053041func init() {
42 internal.RegisterCompressorForTesting = func(c Compressor) func() {
43 name := c.Name()
44 curCompressor, found := registeredCompressor[name]
45 RegisterCompressor(c)
46 return func() {
47 if found {
48 registeredCompressor[name] = curCompressor
49 return
50 }
51 delete(registeredCompressor, name)
52 grpcutil.RegisteredCompressorNames = slices.DeleteFunc(grpcutil.RegisteredCompressorNames, func(s string) bool {
53 return s == name
54 })
55 }
56 }
57}
58
William Kurkianea869482019-04-09 15:16:11 -040059// Compressor is used for compressing and decompressing when sending or
60// receiving messages.
Abhay Kumara61c5222025-11-10 07:32:50 +000061//
62// If a Compressor implements `DecompressedSize(compressedBytes []byte) int`,
63// gRPC will invoke it to determine the size of the buffer allocated for the
64// result of decompression. A return value of -1 indicates unknown size.
William Kurkianea869482019-04-09 15:16:11 -040065type Compressor interface {
66 // Compress writes the data written to wc to w after compressing it. If an
67 // error occurs while initializing the compressor, that error is returned
68 // instead.
69 Compress(w io.Writer) (io.WriteCloser, error)
70 // Decompress reads data from r, decompresses it, and provides the
71 // uncompressed data via the returned io.Reader. If an error occurs while
72 // initializing the decompressor, that error is returned instead.
73 Decompress(r io.Reader) (io.Reader, error)
74 // Name is the name of the compression codec and is used to set the content
75 // coding header. The result must be static; the result cannot change
76 // between calls.
77 Name() string
78}
79
80var registeredCompressor = make(map[string]Compressor)
81
82// RegisterCompressor registers the compressor with gRPC by its name. It can
83// be activated when sending an RPC via grpc.UseCompressor(). It will be
84// automatically accessed when receiving a message based on the content coding
85// header. Servers also use it to send a response with the same encoding as
86// the request.
87//
88// NOTE: this function must only be called during initialization time (i.e. in
89// an init() function), and is not thread-safe. If multiple Compressors are
90// registered with the same name, the one registered last will take effect.
91func RegisterCompressor(c Compressor) {
92 registeredCompressor[c.Name()] = c
Abhay Kumara61c5222025-11-10 07:32:50 +000093 if !grpcutil.IsCompressorNameRegistered(c.Name()) {
94 grpcutil.RegisteredCompressorNames = append(grpcutil.RegisteredCompressorNames, c.Name())
95 }
William Kurkianea869482019-04-09 15:16:11 -040096}
97
98// GetCompressor returns Compressor for the given compressor name.
99func GetCompressor(name string) Compressor {
100 return registeredCompressor[name]
101}
102
103// Codec defines the interface gRPC uses to encode and decode messages. Note
104// that implementations of this interface must be thread safe; a Codec's
105// methods can be called from concurrent goroutines.
106type Codec interface {
107 // Marshal returns the wire format of v.
Abhay Kumara61c5222025-11-10 07:32:50 +0000108 Marshal(v any) ([]byte, error)
William Kurkianea869482019-04-09 15:16:11 -0400109 // Unmarshal parses the wire format into v.
Abhay Kumara61c5222025-11-10 07:32:50 +0000110 Unmarshal(data []byte, v any) error
William Kurkianea869482019-04-09 15:16:11 -0400111 // Name returns the name of the Codec implementation. The returned string
112 // will be used as part of content type in transmission. The result must be
113 // static; the result cannot change between calls.
114 Name() string
115}
116
Abhay Kumara61c5222025-11-10 07:32:50 +0000117var registeredCodecs = make(map[string]any)
William Kurkianea869482019-04-09 15:16:11 -0400118
119// RegisterCodec registers the provided Codec for use with all gRPC clients and
120// servers.
121//
122// The Codec will be stored and looked up by result of its Name() method, which
123// should match the content-subtype of the encoding handled by the Codec. This
124// is case-insensitive, and is stored and looked up as lowercase. If the
125// result of calling Name() is an empty string, RegisterCodec will panic. See
126// Content-Type on
127// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests for
128// more details.
129//
130// NOTE: this function must only be called during initialization time (i.e. in
Abhay Kumara61c5222025-11-10 07:32:50 +0000131// an init() function), and is not thread-safe. If multiple Codecs are
William Kurkianea869482019-04-09 15:16:11 -0400132// registered with the same name, the one registered last will take effect.
133func RegisterCodec(codec Codec) {
134 if codec == nil {
135 panic("cannot register a nil Codec")
136 }
Abhilash S.L3b494632019-07-16 15:51:09 +0530137 if codec.Name() == "" {
138 panic("cannot register Codec with empty string result for Name()")
William Kurkianea869482019-04-09 15:16:11 -0400139 }
Abhilash S.L3b494632019-07-16 15:51:09 +0530140 contentSubtype := strings.ToLower(codec.Name())
William Kurkianea869482019-04-09 15:16:11 -0400141 registeredCodecs[contentSubtype] = codec
142}
143
144// GetCodec gets a registered Codec by content-subtype, or nil if no Codec is
145// registered for the content-subtype.
146//
147// The content-subtype is expected to be lowercase.
148func GetCodec(contentSubtype string) Codec {
Abhay Kumara61c5222025-11-10 07:32:50 +0000149 c, _ := registeredCodecs[contentSubtype].(Codec)
150 return c
William Kurkianea869482019-04-09 15:16:11 -0400151}