Пример #1
0
func (d *dialer) Dial(network, addr string) (Conn, error) {
	d.Lock()
	if d.conns == nil {
		d.conns = make(map[string][]Conn)
	}
	if c, ok := d.conns[addr]; ok {
		if len(c) > 0 {
			conn := c[0]
			c[0], c = c[len(c)-1], c[:len(c)-1]
			d.Unlock()
			return conn, nil
		}
	}
	d.Unlock()
	if d.isTLS {
		config := tls.Config{InsecureSkipVerify: true}
		c, err := tls.Dial("tcp", addr, &config)
		return &conn{
			Client: client.NewClient(c),
			Conn:   c,
			dialer: d,
		}, err
	} else {
		c, err := net.Dial(network, addr)
		return &conn{
			Client: client.NewClient(c),
			Conn:   c,
			dialer: d,
		}, err
	}
}
Пример #2
0
func (d *dialer) Dial(network, addr string) (Conn, error) {
	d.Lock()
	if d.conns == nil {
		d.conns = make(map[string][]Conn)
	}
	if c, ok := d.conns[addr]; ok {
		if len(c) > 0 {
			conn := c[0]
			c[0], c = c[len(c)-1], c[:len(c)-1]
			d.Unlock()
			return conn, nil
		}
	}
	d.Unlock()
	c, err := net.Dial(network, addr)
	return &conn{
		Client: client.NewClient(c),
		Conn:   c,
		dialer: d,
	}, err
}