Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
properly add entries to the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
chkeita authored and Porges committed Sep 29, 2022
1 parent 648186c commit 7c64453
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/ApiService/ApiService/onefuzzlib/VmssOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,21 @@ public async Async.Task<IDictionary<Guid, string>> ListInstanceIds(Guid name) {
private record InstanceIdKey(Guid Scaleset, Guid VmId);
private Task<string> GetInstanceIdForVmId(Guid scaleset, Guid vmId)
=> _cache.GetOrCreateAsync(new InstanceIdKey(scaleset, vmId), async entry => {
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10);
var scalesetResource = GetVmssResource(scaleset);
var vmIdString = vmId.ToString();
await foreach (var vm in scalesetResource.GetVirtualMachineScaleSetVms().AsAsyncEnumerable()) {
var response = await vm.GetAsync();
var instanceId = response.Value.Data.InstanceId;
if (response.Value.Data.VmId == vmIdString) {
// we found the VM we are looking for
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10);
return instanceId;
} else {
// if we find any other VMs, put them in the cache
if (Guid.TryParse(response.Value.Data.VmId, out var vmId)) {
_ = _cache.CreateEntry(new InstanceIdKey(scaleset, vmId)).SetValue(instanceId);
using var e = _cache.CreateEntry(new InstanceIdKey(scaleset, vmId));
_ = e.SetValue(instanceId);
e.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10);
}
}
}
Expand Down

0 comments on commit 7c64453

Please sign in to comment.