Skip to content

Commit

Permalink
frontend: make sure BuildOpts is cached
Browse files Browse the repository at this point in the history
Dockerfile frontend makes many calls to BuildOpts() function
after named build context support was added. The assumption
is that BuildOpts() is fast and does not need to be cached on
the caller side.

In current implementation BuildOpts() listed the workers
in a way that triggered the platform emulation check to be
triggered to with could take 50-100ms to complete what adds
up if there are many Dockerfile stages.

This commit adds caching to BuildOpts. If frontend was
loaded through external image then it was already cached
and this issue did not appear.

Signed-off-by: Tonis Tiigi <[email protected]>
(cherry picked from commit a7c248b)
  • Loading branch information
tonistiigi committed May 5, 2022
1 parent 9d2c9b2 commit a84c2b0
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions frontend/gateway/forwarder/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func llbBridgeToGatewayClient(ctx context.Context, llbBridge frontend.FrontendLLBBridge, opts map[string]string, inputs map[string]*opspb.Definition, w worker.Infos, sid string, sm *session.Manager) (*bridgeClient, error) {
return &bridgeClient{
bc := &bridgeClient{
opts: opts,
inputs: inputs,
FrontendLLBBridge: llbBridge,
Expand All @@ -35,7 +35,9 @@ func llbBridgeToGatewayClient(ctx context.Context, llbBridge frontend.FrontendLL
workers: w,
final: map[*ref]struct{}{},
workerRefByID: make(map[string]*worker.WorkerRef),
}, nil
}
bc.buildOpts = bc.loadBuildOpts()
return bc, nil
}

type bridgeClient struct {
Expand All @@ -49,6 +51,7 @@ type bridgeClient struct {
refs []*ref
workers worker.Infos
workerRefByID map[string]*worker.WorkerRef
buildOpts client.BuildOpts
}

func (c *bridgeClient) Solve(ctx context.Context, req client.SolveRequest) (*client.Result, error) {
Expand Down Expand Up @@ -87,14 +90,15 @@ func (c *bridgeClient) Solve(ctx context.Context, req client.SolveRequest) (*cli

return cRes, nil
}
func (c *bridgeClient) BuildOpts() client.BuildOpts {
workers := make([]client.WorkerInfo, 0, len(c.workers.WorkerInfos()))
for _, w := range c.workers.WorkerInfos() {
workers = append(workers, client.WorkerInfo{
func (c *bridgeClient) loadBuildOpts() client.BuildOpts {
wis := c.workers.WorkerInfos()
workers := make([]client.WorkerInfo, len(wis))
for i, w := range wis {
workers[i] = client.WorkerInfo{
ID: w.ID,
Labels: w.Labels,
Platforms: w.Platforms,
})
}
}

return client.BuildOpts{
Expand All @@ -107,6 +111,10 @@ func (c *bridgeClient) BuildOpts() client.BuildOpts {
}
}

func (c *bridgeClient) BuildOpts() client.BuildOpts {
return c.buildOpts
}

func (c *bridgeClient) Inputs(ctx context.Context) (map[string]llb.State, error) {
inputs := make(map[string]llb.State)
for key, def := range c.inputs {
Expand Down

0 comments on commit a84c2b0

Please sign in to comment.