@@ -129,7 +129,7 @@ func (d *DiscoveryClient) discoverHostsViaTailscale(ctx context.Context) ([]*Blo
129129}
130130
131131func (d * DiscoveryClient ) discoverHostsViaMetadata (ctx context.Context ) ([]* BlobCacheHost , error ) {
132- hosts , err := d .metadata .GetAvailableHosts (ctx )
132+ hosts , err := d .metadata .GetAvailableHosts (ctx , d . hostMap . Remove )
133133 if err != nil {
134134 return nil , err
135135 }
@@ -140,18 +140,16 @@ func (d *DiscoveryClient) discoverHostsViaMetadata(ctx context.Context) ([]*Blob
140140
141141 for _ , host := range hosts {
142142 if host .PrivateAddr != "" {
143- addr := fmt .Sprintf ("%s:%d" , host .PrivateAddr , d .cfg .Port )
144-
145143 // Don't try to get the state on peers we're already aware of
146- if d .hostMap .Get (addr ) != nil {
144+ if d .hostMap .Get (host . Addr ) != nil {
147145 continue
148146 }
149147
150148 wg .Add (1 )
151149 go func (addr string ) {
152150 defer wg .Done ()
153151
154- hostState , err := d .GetHostState (ctx , addr )
152+ hostState , err := d .GetHostStateViaMetadata (ctx , addr , host . PrivateAddr )
155153 if err != nil {
156154 return
157155 }
@@ -161,7 +159,7 @@ func (d *DiscoveryClient) discoverHostsViaMetadata(ctx context.Context) ([]*Blob
161159 mu .Unlock ()
162160
163161 Logger .Debugf ("Added host with private address to map: %s" , hostState .PrivateAddr )
164- }(addr )
162+ }(host . Addr )
165163 }
166164 }
167165
@@ -243,3 +241,37 @@ func (d *DiscoveryClient) GetHostState(ctx context.Context, addr string) (*BlobC
243241
244242 return & host , nil
245243}
244+
245+ func (d * DiscoveryClient ) GetHostStateViaMetadata (ctx context.Context , addr , privateAddr string ) (* BlobCacheHost , error ) {
246+ host := BlobCacheHost {
247+ Addr : addr ,
248+ RTT : 0 ,
249+ PrivateAddr : privateAddr ,
250+ CapacityUsagePct : 0.0 ,
251+ }
252+
253+ var dialOpts = []grpc.DialOption {
254+ grpc .WithTransportCredentials (insecure .NewCredentials ()),
255+ }
256+
257+ conn , err := grpc .Dial (privateAddr , dialOpts ... )
258+ if err != nil {
259+ return nil , err
260+ }
261+ defer conn .Close ()
262+
263+ c := proto .NewBlobCacheClient (conn )
264+ resp , err := c .GetState (ctx , & proto.GetStateRequest {})
265+ if err != nil {
266+ return nil , err
267+ }
268+
269+ host .RTT = 0
270+ host .CapacityUsagePct = float64 (resp .GetCapacityUsagePct ())
271+
272+ if resp .GetVersion () != BlobCacheVersion {
273+ return nil , fmt .Errorf ("version mismatch: %s != %s" , resp .GetVersion (), BlobCacheVersion )
274+ }
275+
276+ return & host , nil
277+ }
0 commit comments