本文整理匯總了Golang中net.Addr.Network方法的典型用法代碼示例。如果您正苦於以下問題:Golang Addr.Network方法的具體用法?Golang Addr.Network怎麽用?Golang Addr.Network使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類net.Addr
的用法示例。
在下文中一共展示了Addr.Network方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: officialAddr
func officialAddr(unresolvedAddr string, resolvedAddr net.Addr) (*util.UnresolvedAddr, error) {
unresolvedHost, unresolvedPort, err := net.SplitHostPort(unresolvedAddr)
if err != nil {
return nil, err
}
resolvedHost, resolvedPort, err := net.SplitHostPort(resolvedAddr.String())
if err != nil {
return nil, err
}
var host string
if unresolvedHost != "" {
// A host was provided, use it.
host = unresolvedHost
} else {
// A host was not provided. Ask the system, and fall back to the listener.
if hostname, err := os.Hostname(); err == nil {
host = hostname
} else {
host = resolvedHost
}
}
var port string
if unresolvedPort != "0" {
// A port was provided, use it.
port = unresolvedPort
} else {
// A port was not provided, but the system assigned one.
port = resolvedPort
}
return util.NewUnresolvedAddr(resolvedAddr.Network(), net.JoinHostPort(host, port)), nil
}
示例2: dial
func (c *Client) dial(addr net.Addr) (net.Conn, error) {
type connError struct {
cn net.Conn
err error
}
ch := make(chan connError)
go func() {
nc, err := net.Dial(addr.Network(), addr.String())
ch <- connError{nc, err}
}()
select {
case ce := <-ch:
return ce.cn, ce.err
case <-time.After(c.netTimeout()):
// Too slow. Fall through.
}
// Close the conn if it does end up finally coming in
go func() {
ce := <-ch
if ce.err == nil {
ce.cn.Close()
}
}()
return nil, &ConnectTimeoutError{addr}
}
示例3: dial
func (c *Client) dial(addr net.Addr) (net.Conn, error) {
nc, err := net.DialTimeout(addr.Network(), addr.String(), c.netTimeout())
if err == nil {
return nc, nil
}
return nil, err
}
示例4: verifyAddr
// verifyAddr starts a server listener at the specified addr and
// then dials a client to verify a connection is established.
func verifyAddr(addr net.Addr, t *testing.T) {
ln, err := net.Listen(addr.Network(), addr.String())
if err != nil {
t.Error(err)
return
}
acceptChan := make(chan struct{})
go func() {
_, err := ln.Accept()
if err != nil {
t.Error(err)
}
close(acceptChan)
}()
addr = ln.Addr()
conn, err := net.Dial(addr.Network(), addr.String())
if err != nil {
t.Errorf("could not connect to %s", addr)
return
}
select {
case <-acceptChan:
// success.
case <-time.After(500 * time.Millisecond):
t.Error("timeout waiting for client connection after 500ms")
}
conn.Close()
}
示例5: createTestNode
// createTestNode creates an rpc server using the specified address,
// gossip instance, KV database and a node using the specified slice
// of engines. The server, clock and node are returned. If gossipBS is
// not nil, the gossip bootstrap address is set to gossipBS.
func createTestNode(
addr net.Addr, engines []engine.Engine, gossipBS net.Addr, t *testing.T,
) (*grpc.Server, net.Addr, *hlc.Clock, *Node, *stop.Stopper) {
cfg := storage.StoreConfig{}
stopper := stop.NewStopper()
cfg.Clock = hlc.NewClock(hlc.UnixNano)
nodeRPCContext := rpc.NewContext(log.AmbientContext{}, nodeTestBaseContext, cfg.Clock, stopper)
cfg.ScanInterval = 10 * time.Hour
cfg.ConsistencyCheckInterval = 10 * time.Hour
grpcServer := rpc.NewServer(nodeRPCContext)
serverCfg := makeTestConfig()
cfg.Gossip = gossip.NewTest(
0,
nodeRPCContext,
grpcServer,
serverCfg.GossipBootstrapResolvers,
stopper,
metric.NewRegistry(),
)
ln, err := netutil.ListenAndServeGRPC(stopper, grpcServer, addr)
if err != nil {
t.Fatal(err)
}
if gossipBS != nil {
// Handle possibility of a :0 port specification.
if gossipBS.Network() == addr.Network() && gossipBS.String() == addr.String() {
gossipBS = ln.Addr()
}
r, err := resolver.NewResolverFromAddress(gossipBS)
if err != nil {
t.Fatalf("bad gossip address %s: %s", gossipBS, err)
}
cfg.Gossip.SetResolvers([]resolver.Resolver{r})
cfg.Gossip.Start(ln.Addr())
}
retryOpts := base.DefaultRetryOptions()
retryOpts.Closer = stopper.ShouldQuiesce()
distSender := kv.NewDistSender(kv.DistSenderConfig{
Clock: cfg.Clock,
RPCContext: nodeRPCContext,
RPCRetryOptions: &retryOpts,
}, cfg.Gossip)
cfg.AmbientCtx.Tracer = tracing.NewTracer()
sender := kv.NewTxnCoordSender(
cfg.AmbientCtx,
distSender,
cfg.Clock,
false,
stopper,
kv.MakeTxnMetrics(metric.TestSampleInterval),
)
cfg.DB = client.NewDB(sender)
cfg.Transport = storage.NewDummyRaftTransport()
cfg.MetricsSampleInterval = metric.TestSampleInterval
node := NewNode(cfg, status.NewMetricsRecorder(cfg.Clock), metric.NewRegistry(), stopper,
kv.MakeTxnMetrics(metric.TestSampleInterval), sql.MakeEventLogger(nil))
roachpb.RegisterInternalServer(grpcServer, node)
return grpcServer, ln.Addr(), cfg.Clock, node, stopper
}
示例6: initDescriptor
// initDescriptor initializes the node descriptor with the server
// address and the node attributes.
func (n *Node) initDescriptor(addr net.Addr, attrs proto.Attributes) {
n.Descriptor.Address = proto.Addr{
Network: addr.Network(),
Address: addr.String(),
}
n.Descriptor.Attrs = attrs
}
示例7: startGossipAtAddr
func startGossipAtAddr(
nodeID roachpb.NodeID,
addr net.Addr,
stopper *stop.Stopper,
t *testing.T,
registry *metric.Registry,
) *Gossip {
rpcContext := rpc.NewContext(log.AmbientContext{}, &base.Config{Insecure: true}, nil, stopper)
server := rpc.NewServer(rpcContext)
g := NewTest(nodeID, rpcContext, server, nil, stopper, registry)
ln, err := netutil.ListenAndServeGRPC(stopper, server, addr)
if err != nil {
t.Fatal(err)
}
addr = ln.Addr()
if err := g.SetNodeDescriptor(&roachpb.NodeDescriptor{
NodeID: nodeID,
Address: util.MakeUnresolvedAddr(addr.Network(), addr.String()),
}); err != nil {
t.Fatal(err)
}
g.start(addr)
time.Sleep(time.Millisecond)
return g
}
示例8: ListenAndServe
// listen and run
func ListenAndServe(addr string) error {
var netaddr net.Addr
var err error
if strings.Contains(addr, "/") {
netaddr, err = net.ResolveUnixAddr("unix", addr)
if err != nil {
return err
}
} else {
netaddr, err = net.ResolveTCPAddr("tcp", addr)
if err != nil {
return err
}
}
// listen
l, err := net.Listen(netaddr.Network(), netaddr.String())
if err != nil {
return err
}
// same with ServeRpc
http.Handle(GetRpcPath(codecName), &rpcHandler{NewServerCodec})
err = http.Serve(l, nil)
return err
}
示例9: Dial
// Dial connects to the address addr and returns a Message-oriented connection.
func Dial(addr net.Addr) (*Conn, error) {
conn, err := net.Dial(addr.Network(), addr.String())
if err != nil {
return nil, err
}
return WrapConn(conn), nil
}
示例10: Dial
func (fw *firewall) Dial(addr net.Addr) (net.Conn, error) {
if fw.rule != nil && !fw.rule.Match(addr) {
return nil, &net.OpError{Op: "dial", Net: addr.Network(), Addr: addr, Err: errors.New("unreachable host")}
}
return fw.t.Dial(addr)
}
示例11: Init
// Init returns a new RedisLimiter.
// Options:
// - `address` net.Addr
//
// @return *RedisLimiter, error
func Init(address net.Addr) (*RedisLimiter, error) {
rl := &RedisLimiter{
// http://godoc.org/github.com/garyburd/redigo/redis#Pool
Pool: &redis.Pool{
MaxIdle: MaxIdle,
IdleTimeout: IdleTimeout,
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
Dial: func() (redis.Conn, error) {
c, err := redis.Dial(address.Network(), address.String())
if err != nil {
return nil, err
}
return c, err
},
},
PrefixQuota: "RateLimit:Quota:",
PrefixRemaining: "RateLimit:Remaining:",
PrefixReset: "RateLimit:Reset:",
Duration: LimitInterval,
Quota: MaxQuota,
}
_, err := rl.ping()
return rl, err
}
示例12: start
// start initializes the infostore with the rpc server address and
// then begins processing connecting clients in an infinite select
// loop via goroutine. Periodically, clients connected and awaiting
// the next round of gossip are awoken via the conditional variable.
func (s *server) start(addr net.Addr) {
s.mu.Lock()
defer s.mu.Unlock()
s.mu.is.NodeAddr = util.MakeUnresolvedAddr(addr.Network(), addr.String())
broadcast := func() {
// Close the old ready and open a new one. This will broadcast to all
// receivers and setup a fresh channel to replace the closed one.
s.mu.Lock()
defer s.mu.Unlock()
ready := make(chan struct{})
close(s.mu.ready)
s.mu.ready = ready
}
unregister := s.mu.is.registerCallback(".*", func(_ string, _ roachpb.Value) {
broadcast()
})
s.stopper.RunWorker(func() {
<-s.stopper.ShouldQuiesce()
s.mu.Lock()
unregister()
s.mu.Unlock()
broadcast()
})
}
示例13: NewAddr
func NewAddr(addr net.Addr) *Addr {
return &Addr{
Addr: addr,
s: addr.String(),
n: addr.Network(),
}
}
示例14: MustGetLocalAddrFromAddr
//從一個net.Listener裏麵讀取需要Dial的地址(測試用的比較多)
func MustGetLocalAddrFromAddr(addr net.Addr) string {
tcpAddr, err := net.ResolveTCPAddr(addr.Network(), addr.String())
if err != nil {
panic(err)
}
return "127.0.0.1:" + strconv.Itoa(tcpAddr.Port)
}
示例15: start
// start initializes the infostore with the rpc server address and
// then begins processing connecting clients in an infinite select
// loop via goroutine. Periodically, clients connected and awaiting
// the next round of gossip are awoken via the conditional variable.
func (s *server) start(grpcServer *grpc.Server, addr net.Addr) {
s.mu.Lock()
s.is.NodeAddr = util.MakeUnresolvedAddr(addr.Network(), addr.String())
s.mu.Unlock()
RegisterGossipServer(grpcServer, s)
broadcast := func() {
ready := make(chan struct{})
s.mu.Lock()
close(s.ready)
s.ready = ready
s.mu.Unlock()
}
unregister := s.is.registerCallback(".*", func(_ string, _ roachpb.Value) {
broadcast()
})
s.stopper.RunWorker(func() {
<-s.stopper.ShouldDrain()
s.mu.Lock()
unregister()
s.mu.Unlock()
broadcast()
})
}