Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed some nits. #47

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
package dhcplb

import (
"github.com/golang/glog"
"net"
"time"

"github.com/golang/glog"
)

// LogMessage holds the info of a log line.
Expand Down Expand Up @@ -83,7 +84,7 @@ func (h *loggerHelper) LogSuccess(start time.Time, server *DHCPServer, packet []
}
err := h.personalizedLogger.Log(msg)
if err != nil {
glog.Errorf("Failed to log error: %s", err)
glog.Errorf("Failed to log success: %s", err)
}
}
}
20 changes: 10 additions & 10 deletions lib/throttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,41 +79,41 @@ func (c *Throttle) setRate(MaxRatePerItem int) {

// NewThrottle returns a Throttle struct
//
// Capacity:
// capacity:
// Maximum capacity of the LRU cache
//
// CacheRate (per second):
// cacheRate (per second):
// Maximum allowed rate for adding new items to the cache. By that way it
// prevents the cache invalidation to happen too soon for the existing rate
// items in the cache. Cache rate will be infinite for 0 or negative values.
//
// MaxRatePerItem (per second):
// maxRatePerItem (per second):
// Maximum allowed requests rate for each key in the cache. Throttling will
// be disabled for 0 or negative values. No cache will be created in that case.
func NewThrottle(Capacity int, CacheRate int, MaxRatePerItem int) (*Throttle, error) {
if MaxRatePerItem <= 0 {
func NewThrottle(capacity int, cacheRate int, maxRatePerItem int) (*Throttle, error) {
if maxRatePerItem <= 0 {
glog.Info("No throttling will be done")
}

cache, err := lru.New[string, *rate.Limiter](Capacity)
cache, err := lru.New[string, *rate.Limiter](capacity)
if err != nil {
return nil, err
}

// Keep track of the item creation rate.
var cacheLimiter *rate.Limiter
if CacheRate <= 0 {
if cacheRate <= 0 {
glog.Info("No cache rate limiting will be done")
cacheLimiter = rate.NewLimiter(rate.Inf, 1) // bucket size is ignored
} else {
cacheLimiter = rate.NewLimiter(rate.Limit(CacheRate), CacheRate)
cacheLimiter = rate.NewLimiter(rate.Limit(cacheRate), cacheRate)
}

throttle := &Throttle{
lru: cache,
maxRatePerItem: MaxRatePerItem,
maxRatePerItem: maxRatePerItem,
cacheLimiter: cacheLimiter,
cacheRate: CacheRate,
cacheRate: cacheRate,
}

return throttle, nil
Expand Down
Loading