当前位置:网站首页>Go language custom DNS resolver practice yyds dry inventory

Go language custom DNS resolver practice yyds dry inventory

2022-06-26 07:31:00 FunTester

Finished writing  Java Customize DNS Parser practice and  Java Customize DNS Parser load balancing implementation after , Nature also needs to be right Go Language testing extends the same functionality , Some detours , The ultimate goal has been achieved . Share today Go Language HTTP Interface test customization DNS The implementation of parsing . Here only http Library as demo ,fasthttp Try to share again later .

Set up net.Dialer

Let's share net.Dialer How to set it up .net.Dialer Dialer , My understanding is that HTTP Connection establishment class , Be similar to Java Language HttpClient In the library org.apache.http.impl.conn.PoolingHttpClientConnectionManager Some functions .(HttpClient4.5x Recommend this in the future ).

// clients  Initialize the request client 
// @Description:
// @return fhttp.Client
func clients() http.Client {
	dialer := &net.Dialer{
		Timeout: 1 * time.Second,
	}

	return http.Client{
		Timeout: time.Duration(5) * time.Second, // Timeout time 
		Transport: &http.Transport{
			MaxIdleConnsPerHost:   200,   // Maximum number of free connections per route 
			MaxConnsPerHost:       10000, // Maximum number of connections per route 
			IdleConnTimeout:       90 * time.Second,
			TLSHandshakeTimeout:   10 * time.Second,
			ExpectContinueTimeout: 1 * time.Second,
			DialContext:           dialer.DialContext,
		},
	}
}


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

Strange knowledge points

In the course of this study , Found out Go Linguistic net/http The library also supports another interesting feature , Binding DNS service IP, This sometimes partly solves the need to send the request for a fixed domain name to a fixed machine .

The simple setting method is as follows :

	dialer := &net.Dialer{
		Timeout: 1 * time.Second,
	}
	dialer.Resolver = &net.Resolver{
		Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
			return dialer.DialContext(ctx, "tcp", "114.114.114.114:53") //  adopt tcp request nameserver Domain name resolution 
		},
	}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

Other settings are the same as above .

Customize net.Dialer

stay http.Transport Creating parameters , There is one DialContext Parameter is specified to create unencrypted TCP Connected dial function . Parameter type is func(ctx context.Context, network, addr string) (net.Conn, error) Method , Here I used to call it closure . We just need to implement this method .

In the following example, I set two IP To test load balancing ( The next issue will be a text version and a video version ).

DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
				host, port, err := net.SplitHostPort(address)
				if err != nil {
					return nil, err
				}
				// By customizing nameserver Get domain name resolution IP
				//ips, _ := dialer.Resolver.LookupHost(ctx, host)
				//for _, s := range ips {
				// log.Println(s)
				//}

				//  Create links 
				if host == "fun.tester" {
					ip := "127.0.0.1"
					log.Println(ip)
					conn, err := dialer.DialContext(ctx, network, ip+":"+port)
					if err == nil {
						return conn, nil
					}
				}
				return dialer.DialContext(ctx, network, address)
			},

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

In the middle, there is a part through the custom nameserver Get the domain name resolution result IP The process of , Commented out , Keep it for later use .

test

The test cases are as follows :

// TestFaast
// @Description:  Test customization DNS Parsing function 
// @param t
func TestFaast(t *testing.T) {
	url := "http://fun.tester:12345/test"
	get := fhttp.Get(url, nil)
	//for i := 0; i < 10; i++ {
	// //go log.Println(string(fhttp.Response(get)))
	// go func() {
	// log.Println(string(fhttp.Response(get)))
	// }()
	//}
	response := fhttp.Response(get)
	log.Println(string(response))
}


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

Console output :

=== RUN   TestFaast
2022/02/07 15:34:47 127.0.0.1
2022/02/07 15:34:48 Have Fun ~ Tester !
--- PASS: TestFaast (0.31s)
PASS


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

Test service

Test service based on moco_FunTester Simple services written in the framework , The script is as follows :

    static void main(String[] args) {
        def util = new ArgsUtil(args)
        def server = getServerNoLog(util.getIntOrdefault(0, 12345))
        server.response(delay(textRes("Have Fun ~ Tester !"), 100))
        def run = run(server)
        waitForKey("fan")
        run.stop()
    }

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

Have Fun ~ Tester !

原网站

版权声明
本文为[FunTester]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202171109067626.html