-
-
Notifications
You must be signed in to change notification settings - Fork 512
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[management] Add buffering for getAccount requests during login (#2449)
- Loading branch information
1 parent
8c2d37d
commit 3ed9072
Showing
4 changed files
with
329 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"sync" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// AccountRequest holds the result channel to return the requested account. | ||
type AccountRequest struct { | ||
AccountID string | ||
ResultChan chan *AccountResult | ||
} | ||
|
||
// AccountResult holds the account data or an error. | ||
type AccountResult struct { | ||
Account *Account | ||
Err error | ||
} | ||
|
||
type AccountCache struct { | ||
store Store | ||
getAccountRequests map[string][]*AccountRequest | ||
mu sync.Mutex | ||
getAccountRequestCh chan *AccountRequest | ||
bufferInterval time.Duration | ||
} | ||
|
||
func NewAccountCache(ctx context.Context, store Store) *AccountCache { | ||
bufferIntervalStr := os.Getenv("NB_GET_ACCOUNT_BUFFER_INTERVAL") | ||
bufferInterval, err := time.ParseDuration(bufferIntervalStr) | ||
if err != nil && bufferIntervalStr != "" { | ||
log.WithContext(ctx).Warnf("failed to parse account cache buffer interval: %s", err) | ||
bufferInterval = 300 * time.Millisecond | ||
} | ||
|
||
log.WithContext(ctx).Infof("set account cache buffer interval to %s", bufferInterval) | ||
|
||
ac := AccountCache{ | ||
store: store, | ||
getAccountRequests: make(map[string][]*AccountRequest), | ||
getAccountRequestCh: make(chan *AccountRequest), | ||
bufferInterval: bufferInterval, | ||
} | ||
|
||
go ac.processGetAccountRequests(ctx) | ||
|
||
return &ac | ||
} | ||
func (ac *AccountCache) GetAccountWithBackpressure(ctx context.Context, accountID string) (*Account, error) { | ||
req := &AccountRequest{ | ||
AccountID: accountID, | ||
ResultChan: make(chan *AccountResult, 1), | ||
} | ||
|
||
log.WithContext(ctx).Tracef("requesting account %s with backpressure", accountID) | ||
startTime := time.Now() | ||
ac.getAccountRequestCh <- req | ||
|
||
result := <-req.ResultChan | ||
log.WithContext(ctx).Tracef("got account with backpressure after %s", time.Since(startTime)) | ||
return result.Account, result.Err | ||
} | ||
|
||
func (ac *AccountCache) processGetAccountBatch(ctx context.Context, accountID string) { | ||
ac.mu.Lock() | ||
requests := ac.getAccountRequests[accountID] | ||
delete(ac.getAccountRequests, accountID) | ||
ac.mu.Unlock() | ||
|
||
if len(requests) == 0 { | ||
return | ||
} | ||
|
||
startTime := time.Now() | ||
account, err := ac.store.GetAccount(ctx, accountID) | ||
log.WithContext(ctx).Tracef("getting account %s in batch took %s", accountID, time.Since(startTime)) | ||
result := &AccountResult{Account: account, Err: err} | ||
|
||
for _, req := range requests { | ||
req.ResultChan <- result | ||
close(req.ResultChan) | ||
} | ||
} | ||
|
||
func (ac *AccountCache) processGetAccountRequests(ctx context.Context) { | ||
for { | ||
select { | ||
case req := <-ac.getAccountRequestCh: | ||
ac.mu.Lock() | ||
ac.getAccountRequests[req.AccountID] = append(ac.getAccountRequests[req.AccountID], req) | ||
if len(ac.getAccountRequests[req.AccountID]) == 1 { | ||
go func(ctx context.Context, accountID string) { | ||
time.Sleep(ac.bufferInterval) | ||
ac.processGetAccountBatch(ctx, accountID) | ||
}(ctx, req.AccountID) | ||
} | ||
ac.mu.Unlock() | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
} |
Oops, something went wrong.