代码拉取完成,页面将自动刷新
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package connection
import (
"context"
"net"
"sync"
"testing"
)
// bootstrapConnection creates a listener that will listen for a single connection
// on the return address. The user provided run function will be called with the accepted
// connection. The user is responsible for closing the connection.
func bootstrapConnections(t *testing.T, num int, run func(net.Conn)) net.Addr {
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Errorf("Could not set up a listener: %v", err)
t.FailNow()
}
go func() {
for i := 0; i < num; i++ {
c, err := l.Accept()
if err != nil {
t.Errorf("Could not accept a connection: %v", err)
}
go run(c)
}
_ = l.Close()
}()
return l.Addr()
}
type netconn struct {
net.Conn
closed chan struct{}
d *dialer
}
func (nc *netconn) Close() error {
nc.closed <- struct{}{}
nc.d.connclosed(nc)
return nc.Conn.Close()
}
type dialer struct {
Dialer
opened map[*netconn]struct{}
closed map[*netconn]struct{}
sync.Mutex
}
func newdialer(d Dialer) *dialer {
return &dialer{Dialer: d, opened: make(map[*netconn]struct{}), closed: make(map[*netconn]struct{})}
}
func (d *dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
d.Lock()
defer d.Unlock()
c, err := d.Dialer.DialContext(ctx, network, address)
if err != nil {
return nil, err
}
nc := &netconn{Conn: c, closed: make(chan struct{}, 1), d: d}
d.opened[nc] = struct{}{}
return nc, nil
}
func (d *dialer) connclosed(nc *netconn) {
d.Lock()
defer d.Unlock()
d.closed[nc] = struct{}{}
}
func (d *dialer) lenopened() int {
d.Lock()
defer d.Unlock()
return len(d.opened)
}
func (d *dialer) lenclosed() int {
d.Lock()
defer d.Unlock()
return len(d.closed)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。