| Scott Baker | 8461e15 | 2019-10-01 14:44:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2019 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 grpc |
| 20 | |
| 21 | import ( |
| 22 | "google.golang.org/grpc/codes" |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame] | 23 | "google.golang.org/grpc/mem" |
| Scott Baker | 8461e15 | 2019-10-01 14:44:30 -0700 | [diff] [blame] | 24 | "google.golang.org/grpc/status" |
| 25 | ) |
| 26 | |
| 27 | // PreparedMsg is responsible for creating a Marshalled and Compressed object. |
| 28 | // |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame] | 29 | // # Experimental |
| 30 | // |
| 31 | // Notice: This type is EXPERIMENTAL and may be changed or removed in a |
| 32 | // later release. |
| Scott Baker | 8461e15 | 2019-10-01 14:44:30 -0700 | [diff] [blame] | 33 | type PreparedMsg struct { |
| 34 | // Struct for preparing msg before sending them |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame] | 35 | encodedData mem.BufferSlice |
| Scott Baker | 8461e15 | 2019-10-01 14:44:30 -0700 | [diff] [blame] | 36 | hdr []byte |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame] | 37 | payload mem.BufferSlice |
| 38 | pf payloadFormat |
| Scott Baker | 8461e15 | 2019-10-01 14:44:30 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // Encode marshalls and compresses the message using the codec and compressor for the stream. |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame] | 42 | func (p *PreparedMsg) Encode(s Stream, msg any) error { |
| Scott Baker | 8461e15 | 2019-10-01 14:44:30 -0700 | [diff] [blame] | 43 | ctx := s.Context() |
| 44 | rpcInfo, ok := rpcInfoFromContext(ctx) |
| 45 | if !ok { |
| 46 | return status.Errorf(codes.Internal, "grpc: unable to get rpcInfo") |
| 47 | } |
| 48 | |
| 49 | // check if the context has the relevant information to prepareMsg |
| 50 | if rpcInfo.preloaderInfo == nil { |
| 51 | return status.Errorf(codes.Internal, "grpc: rpcInfo.preloaderInfo is nil") |
| 52 | } |
| 53 | if rpcInfo.preloaderInfo.codec == nil { |
| 54 | return status.Errorf(codes.Internal, "grpc: rpcInfo.preloaderInfo.codec is nil") |
| 55 | } |
| 56 | |
| 57 | // prepare the msg |
| 58 | data, err := encode(rpcInfo.preloaderInfo.codec, msg) |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame] | 62 | |
| 63 | materializedData := data.Materialize() |
| 64 | data.Free() |
| 65 | p.encodedData = mem.BufferSlice{mem.SliceBuffer(materializedData)} |
| 66 | |
| 67 | // TODO: it should be possible to grab the bufferPool from the underlying |
| 68 | // stream implementation with a type cast to its actual type (such as |
| 69 | // addrConnStream) and accessing the buffer pool directly. |
| 70 | var compData mem.BufferSlice |
| 71 | compData, p.pf, err = compress(p.encodedData, rpcInfo.preloaderInfo.cp, rpcInfo.preloaderInfo.comp, mem.DefaultBufferPool()) |
| Scott Baker | 8461e15 | 2019-10-01 14:44:30 -0700 | [diff] [blame] | 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame] | 75 | |
| 76 | if p.pf.isCompressed() { |
| 77 | materializedCompData := compData.Materialize() |
| 78 | compData.Free() |
| 79 | compData = mem.BufferSlice{mem.SliceBuffer(materializedCompData)} |
| 80 | } |
| 81 | |
| 82 | p.hdr, p.payload = msgHeader(p.encodedData, compData, p.pf) |
| 83 | |
| Scott Baker | 8461e15 | 2019-10-01 14:44:30 -0700 | [diff] [blame] | 84 | return nil |
| 85 | } |