提升通过域名查找服务的性能,轻松支持海量域名

This commit is contained in:
GoEdgeLab
2021-11-15 16:56:31 +08:00
parent 842684ca29
commit 2c114a6b1d
6 changed files with 310 additions and 26 deletions

View File

@@ -2,7 +2,9 @@ package serverconfigs
import (
"github.com/iwind/TeaGo/assert"
"github.com/iwind/TeaGo/types"
"testing"
"time"
)
func TestServerAddressGroup_Protocol(t *testing.T) {
@@ -32,3 +34,79 @@ func TestServerAddressGroup_Protocol(t *testing.T) {
a.IsTrue(group.Addr() == "/tmp/my.sock")
}
}
func TestServerAddressGroup_MatchServerName(t *testing.T) {
var group = NewServerAddressGroup("")
for i := 0; i < 1_000_000; i++ {
group.Add(&ServerConfig{
ServerNames: []*ServerNameConfig{
{
Name: "hello" + types.String(i) + ".com",
SubNames: []string{},
},
},
})
}
group.Add(&ServerConfig{
ServerNames: []*ServerNameConfig{
{
Name: "hello.com",
SubNames: []string{},
},
},
})
group.Add(&ServerConfig{
ServerNames: []*ServerNameConfig{
{
Name: "*.hello.com",
SubNames: []string{},
},
},
})
var before = time.Now()
defer func() {
t.Log(time.Since(before).Seconds()*1000, "ms")
}()
t.Log(group.MatchServerName("hello99999.com").AllStrictNames())
t.Log(group.MatchServerName("hello.com").AllStrictNames())
t.Log(group.MatchServerName("world.hello.com").AllFuzzyNames())
for i := 0; i < 100_000; i++ {
_ = group.MatchServerName("world.hello.com")
}
}
func TestServerAddressGroup_MatchServerCNAME(t *testing.T) {
var group = NewServerAddressGroup("")
group.Add(&ServerConfig{
ServerNames: []*ServerNameConfig{
{
Name: "hello.com",
SubNames: []string{},
},
},
SupportCNAME: true,
})
group.Add(&ServerConfig{
ServerNames: []*ServerNameConfig{
{
Name: "*.hello.com",
SubNames: []string{},
},
},
})
var before = time.Now()
defer func() {
t.Log(time.Since(before).Seconds()*1000, "ms")
}()
server := group.MatchServerCNAME("hello.com")
if server != nil {
t.Log(server.AllStrictNames())
} else {
t.Log(server)
}
t.Log(group.MatchServerCNAME("world.hello.com"))
}