边缘网关 + Agent 的端口中继:Agent 主动连出到网关,把内网目标挂到网关端口上; 通过邀请链接按设备授权,可选审批,首次连接起计时,到期、闲置自动收回。 - portrelay-server:边缘网关(端口池、白名单与授权租约、邀请与授权网页) - portrelay-agent:Agent(yamux 隧道、本地 API 与网页面板、流量统计) - mac-native:SwiftUI 菜单栏 App
37 lines
761 B
Go
37 lines
761 B
Go
package traffic
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestLimiterDoesNotSerializeCallers(t *testing.T) {
|
|
// 1 MB/s,初始令牌 1 MB;两个调用方各取 1 MB,总耗时应约 1 秒而不是被锁串行化
|
|
l := NewLimiter(1)
|
|
start := time.Now()
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 2; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
l.Allow(1024 * 1024)
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
if d := time.Since(start); d < 800*time.Millisecond || d > 1500*time.Millisecond {
|
|
t.Fatalf("expected ~1s total wait, got %v", d)
|
|
}
|
|
}
|
|
|
|
func TestNilLimiter(t *testing.T) {
|
|
var l *Limiter
|
|
l.Allow(1 << 20)
|
|
if l.MBPerSec() != 0 {
|
|
t.Fatal("nil limiter should report 0")
|
|
}
|
|
if NewLimiter(0) != nil {
|
|
t.Fatal("NewLimiter(0) should be nil (unlimited)")
|
|
}
|
|
}
|