Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OutputID for compatibility with Go1.24 #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions cacheproc/cacheproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ type Process struct {
Get func(ctx context.Context, actionID string) (outputID, diskPath string, _ error)

// Put optionally specifies a func to add something to the cache.
// The actionID and objectID is a lowercase hex string of unspecified format or length.
// The actionID and outputID is a lowercase hex string of unspecified format or length.
// On success, diskPath must be the absolute path to a regular file.
// If nil, cmd/go may write to disk elsewhere as needed.
Put func(ctx context.Context, actionID, objectID string, size int64, r io.Reader) (diskPath string, _ error)
Put func(ctx context.Context, actionID, outputID string, size int64, r io.Reader) (diskPath string, _ error)

// Close optionally specifies a func to run when the cmd/go tool is
// shutting down.
Expand Down Expand Up @@ -94,6 +94,10 @@ func (p *Process) Run() error {
}
return err
}
// For Go1.23 backward compatibility, remove in Go1.25.
if len(req.OutputID) == 0 && len(req.ObjectID) != 0 {
req.OutputID = req.ObjectID
}
if req.Command == wire.CmdPut && req.BodySize > 0 {
// TODO(bradfitz): stream this and pass a checksum-validating
// io.Reader that validates on EOF.
Expand Down Expand Up @@ -184,12 +188,12 @@ func (p *Process) handleGet(ctx context.Context, req *wire.Request, res *wire.Re
}

func (p *Process) handlePut(ctx context.Context, req *wire.Request, res *wire.Response) (retErr error) {
actionID, objectID := fmt.Sprintf("%x", req.ActionID), fmt.Sprintf("%x", req.ObjectID)
actionID, outputID := fmt.Sprintf("%x", req.ActionID), fmt.Sprintf("%x", req.OutputID)
p.Puts.Add(1)
defer func() {
if retErr != nil {
p.PutErrors.Add(1)
log.Printf("put(action %s, obj %s, %v bytes): %v", actionID, objectID, req.BodySize, retErr)
log.Printf("put(action %s, obj %s, %v bytes): %v", actionID, outputID, req.BodySize, retErr)
}
}()
if p.Put == nil {
Expand All @@ -202,7 +206,7 @@ func (p *Process) handlePut(ctx context.Context, req *wire.Request, res *wire.Re
if body == nil {
body = bytes.NewReader(nil)
}
diskPath, err := p.Put(ctx, actionID, objectID, req.BodySize, body)
diskPath, err := p.Put(ctx, actionID, outputID, req.BodySize, body)
if err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions cachers/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ func (dc *DiskCache) Get(ctx context.Context, actionID string) (outputID, diskPa
return ie.OutputID, filepath.Join(dc.Dir, fmt.Sprintf("o-%v", ie.OutputID)), nil
}

func (dc *DiskCache) OutputFilename(objectID string) string {
if len(objectID) < 4 || len(objectID) > 1000 {
func (dc *DiskCache) OutputFilename(outputID string) string {
if len(outputID) < 4 || len(outputID) > 1000 {
return ""
}
for i := range objectID {
b := objectID[i]
for i := range outputID {
b := outputID[i]
if b >= '0' && b <= '9' || b >= 'a' && b <= 'f' {
continue
}
return ""
}
return filepath.Join(dc.Dir, fmt.Sprintf("o-%s", objectID))
return filepath.Join(dc.Dir, fmt.Sprintf("o-%s", outputID))
}

func (dc *DiskCache) Put(ctx context.Context, actionID, objectID string, size int64, body io.Reader) (diskPath string, _ error) {
file := filepath.Join(dc.Dir, fmt.Sprintf("o-%s", objectID))
func (dc *DiskCache) Put(ctx context.Context, actionID, outputID string, size int64, body io.Reader) (diskPath string, _ error) {
file := filepath.Join(dc.Dir, fmt.Sprintf("o-%s", outputID))

// Special case empty files; they're both common and easier to do race-free.
if size == 0 {
Expand All @@ -86,7 +86,7 @@ func (dc *DiskCache) Put(ctx context.Context, actionID, objectID string, size in

ij, err := json.Marshal(indexEntry{
Version: 1,
OutputID: objectID,
OutputID: outputID,
Size: size,
TimeNanos: time.Now().UnixNano(),
})
Expand Down
12 changes: 8 additions & 4 deletions wire/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ type Request struct {
// ActionID is non-nil for get and puts.
ActionID []byte `json:",omitempty"` // or nil if not used

// ObjectID is set for Type "put" and "output-file".
ObjectID []byte `json:",omitempty"` // or nil if not used
// OutputID is set for Type "put" and "output-file".
OutputID []byte `json:",omitempty"` // or nil if not used

// ObjectID is the name of `OutputID` before Go1.24, it will be removed in Go1.25.
// It's used for backward compatibility.
ObjectID []byte `json:",omitempty"`

// Body is the body for "put" requests. It's sent after the JSON object
// as a base64-encoded JSON string when BodySize is non-zero.
Expand Down Expand Up @@ -81,8 +85,8 @@ type Response struct {
Size int64 `json:",omitempty"`
TimeNanos int64 `json:",omitempty"` // TODO(bradfitz): document

// DiskPath is the absolute path on disk of the ObjectID corresponding
// DiskPath is the absolute path on disk of the OutputID corresponding
// a "get" request's ActionID (on cache hit) or a "put" request's
// provided ObjectID.
// provided OutputID.
DiskPath string `json:",omitempty"`
}