()
| 49 | } |
| 50 | |
| 51 | func batchUpdate() { |
| 52 | // check if there's any data to update |
| 53 | hasData := false |
| 54 | for i := 0; i < BatchUpdateTypeCount; i++ { |
| 55 | batchUpdateLocks[i].Lock() |
| 56 | if len(batchUpdateStores[i]) > 0 { |
| 57 | hasData = true |
| 58 | batchUpdateLocks[i].Unlock() |
| 59 | break |
| 60 | } |
| 61 | batchUpdateLocks[i].Unlock() |
| 62 | } |
| 63 | |
| 64 | if !hasData { |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | common.SysLog("batch update started") |
| 69 | for i := 0; i < BatchUpdateTypeCount; i++ { |
| 70 | batchUpdateLocks[i].Lock() |
| 71 | store := batchUpdateStores[i] |
| 72 | batchUpdateStores[i] = make(map[int]int) |
| 73 | batchUpdateLocks[i].Unlock() |
| 74 | // TODO: maybe we can combine updates with same key? |
| 75 | for key, value := range store { |
| 76 | switch i { |
| 77 | case BatchUpdateTypeUserQuota: |
| 78 | err := increaseUserQuota(key, value) |
| 79 | if err != nil { |
| 80 | common.SysError("failed to batch update user quota: " + err.Error()) |
| 81 | } |
| 82 | case BatchUpdateTypeTokenQuota: |
| 83 | err := increaseTokenQuota(key, value) |
| 84 | if err != nil { |
| 85 | common.SysError("failed to batch update token quota: " + err.Error()) |
| 86 | } |
| 87 | case BatchUpdateTypeUsedQuota: |
| 88 | updateUserUsedQuota(key, value) |
| 89 | case BatchUpdateTypeRequestCount: |
| 90 | updateUserRequestCount(key, value) |
| 91 | case BatchUpdateTypeChannelUsedQuota: |
| 92 | updateChannelUsedQuota(key, value) |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | common.SysLog("batch update finished") |
| 97 | } |
| 98 | |
| 99 | func RecordExist(err error) (bool, error) { |
| 100 | if err == nil { |
no test coverage detected