github.com/ulule/limiter/v3@v3.11.3-0.20230613131926-4cb9c1da4633/drivers/store/common/context.go (about)

     1  package common
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/ulule/limiter/v3"
     7  )
     8  
     9  // GetContextFromState generate a new limiter.Context from given state.
    10  func GetContextFromState(now time.Time, rate limiter.Rate, expiration time.Time, count int64) limiter.Context {
    11  	limit := rate.Limit
    12  	remaining := int64(0)
    13  	reached := true
    14  
    15  	if count <= limit {
    16  		remaining = limit - count
    17  		reached = false
    18  	}
    19  
    20  	reset := expiration.Unix()
    21  
    22  	return limiter.Context{
    23  		Limit:     limit,
    24  		Remaining: remaining,
    25  		Reset:     reset,
    26  		Reached:   reached,
    27  	}
    28  }