44 "context"
55 "net"
66 "regexp"
7+ "runtime"
78 "strconv"
89 "strings"
910
@@ -264,6 +265,11 @@ func loadCode(cfg *Config, prefix string) []codeInfo {
264265}
265266
266267func NewClient (ctx context.Context , underlay tunnel.Client ) (* Client , error ) {
268+ m1 := runtime.MemStats {}
269+ m2 := runtime.MemStats {}
270+ m3 := runtime.MemStats {}
271+ m4 := runtime.MemStats {}
272+
267273 cfg := config .FromContext (ctx , Name ).(* Config )
268274 var cancel context.CancelFunc
269275 ctx , cancel = context .WithCancel (ctx )
@@ -304,10 +310,14 @@ func NewClient(ctx context.Context, underlay tunnel.Client) (*Client, error) {
304310 return nil , common .NewError ("unknown strategy: " + cfg .Router .DomainStrategy )
305311 }
306312
313+ runtime .ReadMemStats (& m1 )
314+
315+ geodataLoader := geodata .GetGeodataLoader ()
316+
307317 ipCode := loadCode (cfg , "geoip:" )
308318 for _ , c := range ipCode {
309319 code := c .code
310- cidrs , err := geodata .LoadGeoIP (code )
320+ cidrs , err := geodataLoader .LoadGeoIP (code )
311321 if err != nil {
312322 log .Error (err )
313323 } else {
@@ -316,6 +326,8 @@ func NewClient(ctx context.Context, underlay tunnel.Client) (*Client, error) {
316326 }
317327 }
318328
329+ runtime .ReadMemStats (& m2 )
330+
319331 siteCode := loadCode (cfg , "geosite:" )
320332 for _ , c := range siteCode {
321333 code := c .code
@@ -334,7 +346,7 @@ func NewClient(ctx context.Context, underlay tunnel.Client) (*Client, error) {
334346 continue
335347 }
336348
337- domainList , err := geodata .LoadGeoSite (code )
349+ domainList , err := geodataLoader .LoadGeoSite (code )
338350 if err != nil {
339351 log .Error (err )
340352 } else {
@@ -360,6 +372,8 @@ func NewClient(ctx context.Context, underlay tunnel.Client) (*Client, error) {
360372 }
361373 }
362374
375+ runtime .ReadMemStats (& m3 )
376+
363377 domainInfo := loadCode (cfg , "domain:" )
364378 for _ , info := range domainInfo {
365379 client .domains [info .strategy ] = append (client .domains [info .strategy ], & v2router.Domain {
@@ -433,5 +447,13 @@ func NewClient(ctx context.Context, underlay tunnel.Client) (*Client, error) {
433447 }
434448
435449 log .Info ("router client created" )
450+
451+ runtime .ReadMemStats (& m4 )
452+
453+ log .Debugf ("GeoIP rules -> Alloc: %s; TotalAlloc: %s" , common .HumanFriendlyTraffic (m2 .Alloc - m1 .Alloc ), common .HumanFriendlyTraffic (m2 .TotalAlloc - m1 .TotalAlloc ))
454+ log .Debugf ("GeoSite rules -> Alloc: %s; TotalAlloc: %s" , common .HumanFriendlyTraffic (m3 .Alloc - m2 .Alloc ), common .HumanFriendlyTraffic (m3 .TotalAlloc - m2 .TotalAlloc ))
455+ log .Debugf ("Plaintext rules -> Alloc: %s; TotalAlloc: %s" , common .HumanFriendlyTraffic (m4 .Alloc - m3 .Alloc ), common .HumanFriendlyTraffic (m4 .TotalAlloc - m3 .TotalAlloc ))
456+ log .Debugf ("Total(router) -> Alloc: %s; TotalAlloc: %s" , common .HumanFriendlyTraffic (m4 .Alloc - m1 .Alloc ), common .HumanFriendlyTraffic (m4 .TotalAlloc - m1 .TotalAlloc ))
457+
436458 return client , nil
437459}
0 commit comments