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
+7 -6
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,12 +158,10 @@ 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)
reader.Close()
if err != nil { if err != nil {
return "", routing.ParticipantInit{}, http.StatusBadRequest, errors.New("cannot read decompressed join request") return "", routing.ParticipantInit{}, http.StatusBadRequest, errors.New("cannot read decompressed join request")
} }
@@ -168,7 +170,6 @@ func (s *RTCService) validateInternal(lgr logger.Logger, r *http.Request, strict
return "", routing.ParticipantInit{}, http.StatusBadRequest, errors.New("cannot unmarshal join request") return "", routing.ParticipantInit{}, http.StatusBadRequest, errors.New("cannot unmarshal join request")
} }
} }
}
params.metadata = joinRequest.Metadata params.metadata = joinRequest.Metadata
params.attributes = joinRequest.ParticipantAttributes params.attributes = joinRequest.ParticipantAttributes