|
| 1 | +package StormTrackingCMC |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/aws/aws-cryptographic-material-providers-library/mpl/AwsCryptographyMaterialProvidersTypes" |
| 8 | + "github.com/aws/aws-cryptographic-material-providers-library/mpl/StormTracker" |
| 9 | + _dafny "github.com/dafny-lang/DafnyRuntimeGo/v4/dafny" |
| 10 | + "github.com/dafny-lang/DafnyStandardLibGo/Wrappers" |
| 11 | + DafnyTime "github.com/dafny-lang/DafnyStandardLibGo/Time_" |
| 12 | +) |
| 13 | + |
| 14 | +type StormTrackingCMC struct { |
| 15 | + stormTracker *StormTracker.StormTracker |
| 16 | + //The Lock contention is not tested. |
| 17 | + sync.Mutex |
| 18 | +} |
| 19 | + |
| 20 | +func New_StormTrackingCMC_(stormTracker *StormTracker.StormTracker) *StormTrackingCMC { |
| 21 | + return &StormTrackingCMC{stormTracker: stormTracker} |
| 22 | +} |
| 23 | + |
| 24 | +func (cmc *StormTrackingCMC) PutCacheEntry(input AwsCryptographyMaterialProvidersTypes.PutCacheEntryInput) Wrappers.Result { |
| 25 | + cmc.Lock() |
| 26 | + defer cmc.Unlock() |
| 27 | + return cmc.stormTracker.PutCacheEntry(input) |
| 28 | +} |
| 29 | +func (cmc *StormTrackingCMC) PutCacheEntry_k(input AwsCryptographyMaterialProvidersTypes.PutCacheEntryInput) Wrappers.Result { |
| 30 | + cmc.Lock() |
| 31 | + defer cmc.Unlock() |
| 32 | + return cmc.stormTracker.PutCacheEntry(input) |
| 33 | +} |
| 34 | +func (cmc *StormTrackingCMC) UpdateUsageMetadata(input AwsCryptographyMaterialProvidersTypes.UpdateUsageMetadataInput) Wrappers.Result { |
| 35 | + cmc.Lock() |
| 36 | + defer cmc.Unlock() |
| 37 | + return cmc.stormTracker.UpdateUsageMetadata(input) |
| 38 | +} |
| 39 | +func (cmc *StormTrackingCMC) UpdateUsageMetadata_k(input AwsCryptographyMaterialProvidersTypes.UpdateUsageMetadataInput) Wrappers.Result { |
| 40 | + cmc.Lock() |
| 41 | + defer cmc.Unlock() |
| 42 | + return cmc.stormTracker.UpdateUsageMetadata(input) |
| 43 | +} |
| 44 | +func (cmc *StormTrackingCMC) GetCacheEntry(input AwsCryptographyMaterialProvidersTypes.GetCacheEntryInput) Wrappers.Result { |
| 45 | + return cmc.GetCacheEntry_k(input) |
| 46 | +} |
| 47 | +func (cmc *StormTrackingCMC) GetCacheEntry_k(input AwsCryptographyMaterialProvidersTypes.GetCacheEntryInput) Wrappers.Result { |
| 48 | + maxInFlight := DafnyTime.CurrentRelativeTimeMilli() + cmc.stormTracker.InFlightTTL |
| 49 | + for { |
| 50 | + res := cmc.GetFromInner(input) |
| 51 | + if res.IsFailure() { |
| 52 | + return Companion_Default___.CreateGetCacheEntryFailure(res.Dtor_error().(AwsCryptographyMaterialProvidersTypes.Error)) |
| 53 | + } else if res.Dtor_value().(StormTracker.CacheState).Is_Full() { |
| 54 | + return Companion_Default___.CreateGetCacheEntrySuccess(res.Dtor_value().(StormTracker.CacheState).Dtor_data()) |
| 55 | + } else if res.Dtor_value().(StormTracker.CacheState).Is_EmptyFetch() { |
| 56 | + return Companion_Default___.CreateGetCacheEntryFailure(AwsCryptographyMaterialProvidersTypes.Companion_Error_.Create_EntryDoesNotExist_(_dafny.SeqOfChars([]_dafny.Char("Entry doesn't exists")...))) |
| 57 | + } else { |
| 58 | + if DafnyTime.CurrentRelativeTimeMilli() <= maxInFlight { |
| 59 | + time.Sleep(time.Duration(cmc.stormTracker.SleepMilli)) |
| 60 | + } else { |
| 61 | + return Companion_Default___.CreateGetCacheEntryFailure(AwsCryptographyMaterialProvidersTypes.Companion_Error_.Create_InFlightTTLExceeded_(_dafny.SeqOfChars([]_dafny.Char("Storm cache inFlightTTL exceeded.")...))) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | +func (cmc *StormTrackingCMC) DeleteCacheEntry(input AwsCryptographyMaterialProvidersTypes.DeleteCacheEntryInput) Wrappers.Result { |
| 67 | + cmc.Lock() |
| 68 | + defer cmc.Unlock() |
| 69 | + return cmc.stormTracker.DeleteCacheEntry(input) |
| 70 | + |
| 71 | +} |
| 72 | +func (cmc *StormTrackingCMC) DeleteCacheEntry_k(input AwsCryptographyMaterialProvidersTypes.DeleteCacheEntryInput) Wrappers.Result { |
| 73 | + cmc.Lock() |
| 74 | + defer cmc.Unlock() |
| 75 | + return cmc.stormTracker.DeleteCacheEntry(input) |
| 76 | +} |
| 77 | + |
| 78 | +func (cmc *StormTrackingCMC) String() string { |
| 79 | + return "StormTrackerCMC" |
| 80 | +} |
| 81 | + |
| 82 | +func (cmc *StormTrackingCMC) GetFromInner(input AwsCryptographyMaterialProvidersTypes.GetCacheEntryInput) Wrappers.Result { |
| 83 | + cmc.Lock() |
| 84 | + defer cmc.Unlock() |
| 85 | + return cmc.stormTracker.GetFromCache(input) |
| 86 | +} |
0 commit comments