storj/pkg/rpc/rpcpeer/peer_drpc.go
Jeff Wendling 013e0d94bc pkg/rpc: ensure connections are quickly closed
drpc will call Close on any transport we pass to it, but some
transports (like tls.Conn) will attempt to notify the remote
side of things. we don't want to do that, so pass a new
interface that just closes the underlying socket.

Change-Id: I53344d2747de21b3146abe4f82b8394bb8948cb5
2019-11-12 15:53:36 +00:00

34 lines
687 B
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package rpcpeer
import (
"context"
"crypto/tls"
"net"
"storj.io/drpc/drpcctx"
)
// drpcInternalFromContext returns a peer from the context using drpc.
func drpcInternalFromContext(ctx context.Context) (*Peer, error) {
tr, ok := drpcctx.Transport(ctx)
if !ok {
return nil, Error.New("unable to get drpc peer from context")
}
conn, ok := tr.(interface {
RemoteAddr() net.Addr
ConnectionState() tls.ConnectionState
})
if !ok {
return nil, Error.New("drpc transport does not have required methods")
}
return &Peer{
Addr: conn.RemoteAddr(),
State: conn.ConnectionState(),
}, nil
}