-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor the project directory structure
refactor netlist & domain data loader
- Loading branch information
1 parent
9b117ef
commit 473da64
Showing
72 changed files
with
1,606 additions
and
1,367 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,69 @@ | ||
// Copyright (C) 2020-2021, IrineSistiana | ||
// | ||
// This file is part of mosdns. | ||
// | ||
// mosdns is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// mosdns is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package concurrent_limiter | ||
|
||
import ( | ||
"github.com/IrineSistiana/mosdns/dispatcher/pkg/concurrent_map" | ||
) | ||
|
||
type ClientQueryLimiter struct { | ||
maxQueries int | ||
m *concurrent_map.ConcurrentMap | ||
} | ||
|
||
func NewClientQueryLimiter(maxQueries int) *ClientQueryLimiter { | ||
return &ClientQueryLimiter{ | ||
maxQueries: maxQueries, | ||
m: concurrent_map.NewConcurrentMap(64), | ||
} | ||
} | ||
|
||
func (l *ClientQueryLimiter) Acquire(key string) bool { | ||
return l.m.TestAndSet(key, l.acquireTestAndSet) | ||
} | ||
|
||
func (l *ClientQueryLimiter) acquireTestAndSet(v interface{}, ok bool) (newV interface{}, wantUpdate, passed bool) { | ||
n := 0 | ||
if ok { | ||
n = v.(int) | ||
} | ||
if n >= l.maxQueries { | ||
return nil, false, false | ||
} | ||
n++ | ||
return n, true, true | ||
} | ||
|
||
func (l *ClientQueryLimiter) doneTestAndSet(v interface{}, ok bool) (newV interface{}, wantUpdate, passed bool) { | ||
if !ok { | ||
panic("ClientQueryLimiter doneTestAndSet: value is not exist") | ||
} | ||
n := v.(int) | ||
n-- | ||
if n < 0 { | ||
panic("ClientQueryLimiter doneTestAndSet: value becomes negative") | ||
} | ||
if n == 0 { | ||
return nil, true, true | ||
} | ||
return n, true, true | ||
} | ||
|
||
func (l *ClientQueryLimiter) Done(key string) { | ||
l.m.TestAndSet(key, l.doneTestAndSet) | ||
} |
2 changes: 1 addition & 1 deletion
2
dispatcher/utils/server_handler_test.go → ...concurrent_limiter/client_limiter_test.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package utils | ||
package concurrent_limiter | ||
|
||
import ( | ||
"strconv" | ||
|
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,55 @@ | ||
// Copyright (C) 2020-2021, IrineSistiana | ||
// | ||
// This file is part of mosdns. | ||
// | ||
// mosdns is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// mosdns is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package concurrent_limiter | ||
|
||
import "fmt" | ||
|
||
// ConcurrentLimiter is a soft limiter. | ||
type ConcurrentLimiter struct { | ||
bucket chan struct{} | ||
} | ||
|
||
// NewConcurrentLimiter returns a ConcurrentLimiter, max must > 0. | ||
func NewConcurrentLimiter(max int) *ConcurrentLimiter { | ||
if max <= 0 { | ||
panic(fmt.Sprintf("ConcurrentLimiter: invalid max arg: %d", max)) | ||
} | ||
|
||
bucket := make(chan struct{}, max) | ||
for i := 0; i < max; i++ { | ||
bucket <- struct{}{} | ||
} | ||
|
||
return &ConcurrentLimiter{bucket: bucket} | ||
} | ||
|
||
func (l *ConcurrentLimiter) Wait() <-chan struct{} { | ||
return l.bucket | ||
} | ||
|
||
func (l *ConcurrentLimiter) Done() { | ||
select { | ||
case l.bucket <- struct{}{}: | ||
default: | ||
panic("ConcurrentLimiter: bucket overflow") | ||
} | ||
} | ||
|
||
func (l *ConcurrentLimiter) Available() int { | ||
return len(l.bucket) | ||
} |
34 changes: 34 additions & 0 deletions
34
dispatcher/pkg/concurrent_limiter/concurrent_limiter_test.go
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,34 @@ | ||
package concurrent_limiter | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_ConcurrentLimiter_acquire_release(t *testing.T) { | ||
l := NewConcurrentLimiter(500) | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) | ||
defer cancel() | ||
|
||
wg := new(sync.WaitGroup) | ||
wg.Add(1000) | ||
for i := 0; i < 1000; i++ { | ||
go func() { | ||
defer wg.Done() | ||
select { | ||
case <-l.Wait(): | ||
time.Sleep(time.Millisecond * 200) | ||
l.Done() | ||
case <-ctx.Done(): | ||
t.Fail() | ||
} | ||
}() | ||
} | ||
|
||
wg.Wait() | ||
if l.Available() != 500 { | ||
t.Fatal("token leaked") | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
dispatcher/utils/concurrent_lru_test.go → ...pkg/concurrent_lru/concurrent_lru_test.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package utils | ||
package concurrent_lru | ||
|
||
import ( | ||
"reflect" | ||
|
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
2 changes: 1 addition & 1 deletion
2
dispatcher/utils/concurrent_map_test.go → ...pkg/concurrent_map/concurrent_map_test.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package utils | ||
package concurrent_map | ||
|
||
import ( | ||
"strconv" | ||
|
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
Oops, something went wrong.