Skip to content

Commit

Permalink
[mod] optimize limit rule initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
asciimoo committed Mar 3, 2018
1 parent 9b602c6 commit 1e8f7bb
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion http_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func (r *LimitRule) Init() error {

func (h *httpBackend) Init(jar http.CookieJar) {
rand.Seed(time.Now().UnixNano())
h.LimitRules = make([]*LimitRule, 0, 8)
h.Client = &http.Client{
Jar: jar,
Timeout: 10 * time.Second,
Expand All @@ -98,6 +97,9 @@ func (r *LimitRule) Match(domain string) bool {
}

func (h *httpBackend) GetMatchingRule(domain string) *LimitRule {
if h.LimitRules == nil {
return nil
}
h.lock.RLock()
defer h.lock.RUnlock()
for _, r := range h.LimitRules {
Expand Down Expand Up @@ -183,6 +185,9 @@ func (h *httpBackend) Do(request *http.Request, bodySize int) (*Response, error)

func (h *httpBackend) Limit(rule *LimitRule) error {
h.lock.Lock()
if h.LimitRules == nil {
h.LimitRules = make([]*LimitRule, 0, 8)
}
h.LimitRules = append(h.LimitRules, rule)
h.lock.Unlock()
return rule.Init()
Expand Down

0 comments on commit 1e8f7bb

Please sign in to comment.