Skip to content

Commit

Permalink
Small changes on refactor APIKeyLastUseCache get (pipe-cd#4216)
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhtc1202 authored Feb 24, 2023
1 parent b8f0dec commit f804bfb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,26 @@ func (c *APIKeyLastUsedTimeUpdater) updateAPIKeyLastUsedTime(ctx context.Context
}

for _, apiKey := range apiKeys {
lastUsedTimeByte, err := c.apiKeyLastUsedTimeCache.Get(apiKey.Id)
cachedLastUse, err := c.apiKeyLastUsedTimeCache.Get(apiKey.Id)
if err != nil {
c.logger.Error("failed to update last used time",
c.logger.Error("failed to fetch last used time from cache",
zap.String("id", apiKey.Id),
zap.Error(err),
)
continue
}

lastUsedTime, err := bytes2int64(lastUsedTimeByte.([]byte))
s := string(cachedLastUse.([]byte))
lastUsedTime, err := strconv.ParseInt(s, 10, 64)
if err != nil {
c.logger.Error("failed to update last used time",
c.logger.Error("failed to parse last used time from cache",
zap.String("id", apiKey.Id),
zap.Error(err),
)
continue
}

// Skip update last_used_at in database if no changed.
if lastUsedTime == apiKey.LastUsedAt {
continue
}
Expand All @@ -129,8 +132,3 @@ func (c *APIKeyLastUsedTimeUpdater) updateAPIKeyLastUsedTime(ctx context.Context

return nil
}

func bytes2int64(bytes []byte) (int64, error) {
numString := string(bytes)
return strconv.ParseInt(numString, 10, 64)
}
34 changes: 19 additions & 15 deletions pkg/app/server/grpcapi/web_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1528,15 +1528,28 @@ func (a *WebAPI) ListAPIKeys(ctx context.Context, req *webservice.ListAPIKeysReq
}

for i := range apiKeys {
// Get LastUsedTIme from Redis
var lastUsedAtFromCache int64 = 0
if lastUsedAtFromCacheByte, error := a.apiKeyLastUsedStore.Get(apiKeys[i].Id); error == nil {
lastUsedAtFromCache = bytes2int64(lastUsedAtFromCacheByte.([]byte))
cachedLastUse, err := a.apiKeyLastUsedStore.Get(apiKeys[i].Id)
if err != nil {
a.logger.Error("failed to get last used time from cache",
zap.String("id", apiKeys[i].Id),
zap.Error(err),
)
continue
}

if lastUsedAtFromCache > apiKeys[i].LastUsedAt {
apiKeys[i].LastUsedAt = lastUsedAtFromCache
s := string(cachedLastUse.([]byte))
lastUsedAt, err := strconv.ParseInt(s, 10, 64)
if err != nil {
a.logger.Error("failed to parse last used time from cache",
zap.String("id", apiKeys[i].Id),
zap.Error(err),
)
continue
}
if lastUsedAt > apiKeys[i].LastUsedAt {
apiKeys[i].LastUsedAt = lastUsedAt
}

// Redact all sensitive data inside API key before sending to the client.
apiKeys[i].RedactSensitiveData()
}
Expand All @@ -1546,15 +1559,6 @@ func (a *WebAPI) ListAPIKeys(ctx context.Context, req *webservice.ListAPIKeysReq
}, nil
}

func bytes2int64(bytes []byte) int64 {
var numString string
for i := range bytes {
numString += string(bytes[i])
}
num, _ := strconv.ParseInt(numString, 10, 64)
return num
}

// GetInsightData returns the accumulated insight data.
func (a *WebAPI) GetInsightData(ctx context.Context, req *webservice.GetInsightDataRequest) (*webservice.GetInsightDataResponse, error) {
claims, err := rpcauth.ExtractClaims(ctx)
Expand Down

0 comments on commit f804bfb

Please sign in to comment.