| // Copyright 2026 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package rsproxy |
| |
| import ( |
| "context" |
| "fmt" |
| "os" |
| |
| "github.com/bazelbuild/rsclient/internal/pkg/protoutil" |
| "github.com/golang/glog" |
| |
| rspb "google.golang.org/genproto/googleapis/devtools/resultstore/v2" |
| ) |
| |
| // PrototextDelimiter is the marker used to separate individual Prototext messages in a stream. |
| const PrototextDelimiter = "# ---- ResultStore v2 UploadRequest ----" |
| |
| var uploadRequestIO = protoutil.NewDelimitedPrototextIO((*rspb.UploadRequest)(nil), protoutil.DelimitedPrototextConfig{ |
| Delimiter: PrototextDelimiter, |
| }) |
| |
| // PrototextReaderOptions contains configuration for a Prototext-based ingress. |
| type PrototextReaderOptions struct { |
| // Path is the filesystem path to the Prototext file. |
| Path string |
| } |
| |
| // prototextReader reads human-readable prototext messages from a file. |
| // It expects each request to be preceded by PrototextDelimiter. |
| type prototextReader struct { |
| path string |
| } |
| |
| func NewPrototextReader(path string) IngressWorker { |
| return &prototextReader{path: path} |
| } |
| |
| func (w *prototextReader) Name() string { |
| return "PrototextReader" |
| } |
| |
| func (w *prototextReader) Run(ctx context.Context, outbound chan<- *rspb.UploadRequest, ea ErrorAggregator) error { |
| f, err := os.Open(w.path) |
| if err != nil { |
| return fmt.Errorf("failed to open prototext reader file %q: %w", w.path, err) |
| } |
| defer f.Close() |
| |
| glog.Infof("PrototextReader: reading from %q", w.path) |
| |
| handle := func(req *rspb.UploadRequest) error { |
| select { |
| case <-ctx.Done(): |
| return ctx.Err() |
| case outbound <- req: |
| return nil |
| } |
| } |
| |
| if err := uploadRequestIO.ReadStream(ctx, f, handle); err != nil { |
| return fmt.Errorf("prototext stream failed: %w", err) |
| } |
| |
| glog.Infof("PrototextReader: finished reading %q", w.path) |
| return nil |
| } |