Use gzip reader pool (#3903)

Borrowing from @paulwe's change for internal proto
This commit is contained in:
Raja Subramanian
2025-09-07 13:50:22 +05:30
committed by GitHub
parent 17c349210d
commit e16b3ba95a
+13 -12
View File
@@ -116,6 +116,10 @@ func decodeAttributes(str string) (map[string]string, error) {
return attrs, nil return attrs, nil
} }
var gzipReaderPool = sync.Pool{
New: func() any { return &gzip.Reader{} },
}
func (s *RTCService) validateInternal(lgr logger.Logger, r *http.Request, strict bool) (livekit.RoomName, routing.ParticipantInit, int, error) { func (s *RTCService) validateInternal(lgr logger.Logger, r *http.Request, strict bool) (livekit.RoomName, routing.ParticipantInit, int, error) {
var params ValidateConnectRequestParams var params ValidateConnectRequestParams
useSinglePeerConnection := false useSinglePeerConnection := false
@@ -154,19 +158,16 @@ func (s *RTCService) validateInternal(lgr logger.Logger, r *http.Request, strict
} }
case livekit.WrappedJoinRequest_GZIP: case livekit.WrappedJoinRequest_GZIP:
b := bytes.NewReader(wrappedJoinRequest.JoinRequest) reader := gzipReaderPool.Get().(*gzip.Reader)
if reader, err := gzip.NewReader(b); err != nil { defer gzipReaderPool.Put(reader)
return "", routing.ParticipantInit{}, http.StatusBadRequest, errors.New("cannot decompress join request") reader.Reset(bytes.NewReader(wrappedJoinRequest.JoinRequest))
} else { protoBytes, err := io.ReadAll(reader)
protoBytes, err := io.ReadAll(reader) if err != nil {
reader.Close() return "", routing.ParticipantInit{}, http.StatusBadRequest, errors.New("cannot read decompressed join request")
if err != nil { }
return "", routing.ParticipantInit{}, http.StatusBadRequest, errors.New("cannot read decompressed join request")
}
if err := proto.Unmarshal(protoBytes, joinRequest); err != nil { if err := proto.Unmarshal(protoBytes, joinRequest); err != nil {
return "", routing.ParticipantInit{}, http.StatusBadRequest, errors.New("cannot unmarshal join request") return "", routing.ParticipantInit{}, http.StatusBadRequest, errors.New("cannot unmarshal join request")
}
} }
} }