| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package main | 5 package main |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "log" | 8 "log" |
| 9 "time" | 9 "time" |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 type checkerItem struct { | 31 type checkerItem struct { |
| 32 item *Item | 32 item *Item |
| 33 isolated bool | 33 isolated bool |
| 34 callback CheckerCallback | 34 callback CheckerCallback |
| 35 } | 35 } |
| 36 | 36 |
| 37 // Checker uses the isolatedclient.Client to check whether items are available | 37 // Checker uses the isolatedclient.Client to check whether items are available |
| 38 // on the server. | 38 // on the server. |
| 39 // Checker methods are safe to call concurrently. | 39 // Checker methods are safe to call concurrently. |
| 40 type Checker struct { | 40 type Checker struct { |
| 41 ctx context.Context |
| 41 svc isolateService | 42 svc isolateService |
| 42 bundler *bundler.Bundler | 43 bundler *bundler.Bundler |
| 43 err error | 44 err error |
| 44 | 45 |
| 45 Hit, Miss CountBytes | 46 Hit, Miss CountBytes |
| 46 } | 47 } |
| 47 | 48 |
| 48 // CountBytes aggregates a count of files and the number of bytes in them. | 49 // CountBytes aggregates a count of files and the number of bytes in them. |
| 49 type CountBytes struct { | 50 type CountBytes struct { |
| 50 Count int | 51 Count int |
| 51 Bytes int64 | 52 Bytes int64 |
| 52 } | 53 } |
| 53 | 54 |
| 54 func (cb *CountBytes) addFile(size int64) { | 55 func (cb *CountBytes) addFile(size int64) { |
| 55 cb.Count++ | 56 cb.Count++ |
| 56 cb.Bytes += size | 57 cb.Bytes += size |
| 57 } | 58 } |
| 58 | 59 |
| 59 // NewChecker creates a NewChecker with the given isolated client. | 60 // NewChecker creates a NewChecker with the given isolated client. |
| 60 func NewChecker(client *isolatedclient.Client) *Checker { | 61 // The provided context is used to make all requests to the isolate server. |
| 61 » return newChecker(client) | 62 func NewChecker(ctx context.Context, client *isolatedclient.Client) *Checker { |
| 63 » return newChecker(ctx, client) |
| 62 } | 64 } |
| 63 | 65 |
| 64 func newChecker(svc isolateService) *Checker { | 66 func newChecker(ctx context.Context, svc isolateService) *Checker { |
| 65 c := &Checker{ | 67 c := &Checker{ |
| 66 svc: svc, | 68 svc: svc, |
| 67 } | 69 } |
| 68 c.bundler = bundler.NewBundler(checkerItem{}, func(bundle interface{}) { | 70 c.bundler = bundler.NewBundler(checkerItem{}, func(bundle interface{}) { |
| 69 items := bundle.([]checkerItem) | 71 items := bundle.([]checkerItem) |
| 70 if c.err != nil { | 72 if c.err != nil { |
| 71 for _, item := range items { | 73 for _, item := range items { |
| 72 // Drop any more incoming items. | 74 // Drop any more incoming items. |
| 73 log.Printf("WARNING dropped %q from Checker", it
em.item.Path) | 75 log.Printf("WARNING dropped %q from Checker", it
em.item.Path) |
| 74 } | 76 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 // one invocation at a time. | 111 // one invocation at a time. |
| 110 func (c *Checker) check(items []checkerItem) error { | 112 func (c *Checker) check(items []checkerItem) error { |
| 111 var digests []*service.HandlersEndpointsV1Digest | 113 var digests []*service.HandlersEndpointsV1Digest |
| 112 for _, item := range items { | 114 for _, item := range items { |
| 113 digests = append(digests, &service.HandlersEndpointsV1Digest{ | 115 digests = append(digests, &service.HandlersEndpointsV1Digest{ |
| 114 Digest: string(item.item.Digest), | 116 Digest: string(item.item.Digest), |
| 115 Size: item.item.Size, | 117 Size: item.item.Size, |
| 116 IsIsolated: item.isolated, | 118 IsIsolated: item.isolated, |
| 117 }) | 119 }) |
| 118 } | 120 } |
| 119 » out, err := c.svc.Contains(context.Background(), digests) | 121 » out, err := c.svc.Contains(c.ctx, digests) |
| 120 if err != nil { | 122 if err != nil { |
| 121 return err | 123 return err |
| 122 } | 124 } |
| 123 for i, item := range items { | 125 for i, item := range items { |
| 124 if size := item.item.Size; out[i] == nil { | 126 if size := item.item.Size; out[i] == nil { |
| 125 c.Hit.addFile(size) | 127 c.Hit.addFile(size) |
| 126 } else { | 128 } else { |
| 127 c.Miss.addFile(size) | 129 c.Miss.addFile(size) |
| 128 } | 130 } |
| 129 | 131 |
| 130 item.callback(item.item, out[i]) | 132 item.callback(item.item, out[i]) |
| 131 } | 133 } |
| 132 return nil | 134 return nil |
| 133 } | 135 } |
| OLD | NEW |