golang.org/x/build@v0.0.0-20240506185731-218518f32b70/internal/coordinator/remote/legacy.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build linux || darwin
     6  
     7  package remote
     8  
     9  import (
    10  	"context"
    11  	"sync"
    12  	"time"
    13  
    14  	"golang.org/x/build/buildlet"
    15  )
    16  
    17  // Buildlets is a store for the legacy remote buildlets.
    18  type Buildlets struct {
    19  	sync.Mutex
    20  	M map[string]*Buildlet // keyed by buildletName
    21  }
    22  
    23  // Buildlet is the representation of the legacy remote buildlet.
    24  type Buildlet struct {
    25  	User        string // "user-foo" build key
    26  	Name        string // dup of key
    27  	HostType    string
    28  	BuilderType string // default builder config to use if not overwritten
    29  	Created     time.Time
    30  	Expires     time.Time
    31  
    32  	buildlet buildlet.Client
    33  }
    34  
    35  // Renew renews rb's idle timeout if ctx hasn't expired.
    36  // Renew should run in its own goroutine.
    37  func (rb *Buildlet) Renew(ctx context.Context, rbs *Buildlets) {
    38  	rbs.Lock()
    39  	defer rbs.Unlock()
    40  	select {
    41  	case <-ctx.Done():
    42  		return
    43  	default:
    44  	}
    45  	if got := rbs.M[rb.Name]; got == rb {
    46  		rb.Expires = time.Now().Add(remoteBuildletIdleTimeout)
    47  		time.AfterFunc(time.Minute, func() { rb.Renew(ctx, rbs) })
    48  	}
    49  }
    50  
    51  // Buildlet returns the buildlet client for the associated legacy buildlet.
    52  func (rb *Buildlet) Buildlet() buildlet.Client {
    53  	return rb.buildlet
    54  }
    55  
    56  // SetBuildlet sets the buildlet client for a legacy buildlet.
    57  func (rb *Buildlet) SetBuildlet(b buildlet.Client) {
    58  	rb.buildlet = b
    59  }