mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-09 17:45:03 +09:00
addr scheme + fixes + peerservice test
This commit is contained in:
parent
6056226b25
commit
2edd9c7b6d
9 changed files with 296 additions and 21 deletions
26
net/transport/mock_transport/component.go
Normal file
26
net/transport/mock_transport/component.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package mock_transport
|
||||
|
||||
import (
|
||||
"github.com/anyproto/any-sync/app"
|
||||
"go.uber.org/mock/gomock"
|
||||
)
|
||||
|
||||
func NewTransportComponent(ctrl *gomock.Controller, name string) TransportComponent {
|
||||
return TransportComponent{
|
||||
CName: name,
|
||||
MockTransport: NewMockTransport(ctrl),
|
||||
}
|
||||
}
|
||||
|
||||
type TransportComponent struct {
|
||||
CName string
|
||||
*MockTransport
|
||||
}
|
||||
|
||||
func (t TransportComponent) Init(a *app.App) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t TransportComponent) Name() (name string) {
|
||||
return t.CName
|
||||
}
|
|
@ -5,8 +5,9 @@ type configGetter interface {
|
|||
}
|
||||
|
||||
type Config struct {
|
||||
ListenAddrs []string `yaml:"listenAddrs"`
|
||||
WriteTimeoutSec int `yaml:"writeTimeoutSec"`
|
||||
DialTimeoutSec int `yaml:"dialTimeoutSec"`
|
||||
MaxStreams int64 `yaml:"maxStreams"`
|
||||
ListenAddrs []string `yaml:"listenAddrs"`
|
||||
WriteTimeoutSec int `yaml:"writeTimeoutSec"`
|
||||
DialTimeoutSec int `yaml:"dialTimeoutSec"`
|
||||
MaxStreams int64 `yaml:"maxStreams"`
|
||||
KeepAlivePeriodSec int `yaml:"keepAlivePeriodSec"`
|
||||
}
|
||||
|
|
|
@ -2,12 +2,15 @@ package quic
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/anyproto/any-sync/net/peer"
|
||||
"github.com/anyproto/any-sync/net/transport"
|
||||
"github.com/quic-go/quic-go"
|
||||
"net"
|
||||
)
|
||||
|
||||
func newConn(cctx context.Context, qconn quic.Connection) transport.MultiConn {
|
||||
cctx = peer.CtxWithPeerAddr(cctx, transport.Quic+"://"+qconn.RemoteAddr().String())
|
||||
return &quicMultiConn{
|
||||
cctx: cctx,
|
||||
Connection: qconn,
|
||||
|
@ -26,6 +29,11 @@ func (q *quicMultiConn) Context() context.Context {
|
|||
func (q *quicMultiConn) Accept() (conn net.Conn, err error) {
|
||||
stream, err := q.Connection.AcceptStream(context.Background())
|
||||
if err != nil {
|
||||
if errors.Is(err, quic.ErrServerClosed) {
|
||||
err = transport.ErrConnClosed
|
||||
} else if aerr, ok := err.(*quic.ApplicationError); ok && aerr.ErrorCode == 2 {
|
||||
err = transport.ErrConnClosed
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return quicNetConn{
|
||||
|
@ -48,7 +56,7 @@ func (q *quicMultiConn) Open(ctx context.Context) (conn net.Conn, err error) {
|
|||
}
|
||||
|
||||
func (q *quicMultiConn) Addr() string {
|
||||
return q.RemoteAddr().String()
|
||||
return transport.Quic + "://" + q.RemoteAddr().String()
|
||||
}
|
||||
|
||||
func (q *quicMultiConn) IsClosed() bool {
|
||||
|
|
|
@ -2,6 +2,7 @@ package quic
|
|||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"github.com/anyproto/any-sync/app"
|
||||
"github.com/anyproto/any-sync/app/logger"
|
||||
|
@ -42,9 +43,16 @@ type quicTransport struct {
|
|||
func (q *quicTransport) Init(a *app.App) (err error) {
|
||||
q.secure = a.MustComponent(secureservice.CName).(secureservice.SecureService)
|
||||
q.conf = a.MustComponent("config").(configGetter).GetQuic()
|
||||
if q.conf.MaxStreams <= 0 {
|
||||
q.conf.MaxStreams = 128
|
||||
}
|
||||
if q.conf.KeepAlivePeriodSec <= 0 {
|
||||
q.conf.KeepAlivePeriodSec = 25
|
||||
}
|
||||
q.quicConf = &quic.Config{
|
||||
HandshakeIdleTimeout: time.Duration(q.conf.DialTimeoutSec) * time.Second,
|
||||
MaxIncomingStreams: q.conf.MaxStreams,
|
||||
KeepAlivePeriod: time.Duration(q.conf.KeepAlivePeriodSec) * time.Second,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -61,12 +69,19 @@ func (q *quicTransport) Run(ctx context.Context) (err error) {
|
|||
if q.accepter == nil {
|
||||
return fmt.Errorf("can't run service without accepter")
|
||||
}
|
||||
tlConf, _, err := q.secure.TlsConfig()
|
||||
|
||||
var tlsConf tls.Config
|
||||
tlsConf.GetConfigForClient = func(_ *tls.ClientHelloInfo) (*tls.Config, error) {
|
||||
conf, _, tlsErr := q.secure.TlsConfig()
|
||||
return conf, tlsErr
|
||||
}
|
||||
tlsConf.NextProtos = []string{"anysync"}
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, listAddr := range q.conf.ListenAddrs {
|
||||
list, err := quic.ListenAddr(listAddr, tlConf, q.quicConf)
|
||||
list, err := quic.ListenAddr(listAddr, &tlsConf, q.quicConf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -74,7 +74,7 @@ func TestQuicTransport_Dial(t *testing.T) {
|
|||
// common write deadline - 66700 rps
|
||||
// subconn write deadline - 67100 rps
|
||||
func TestWriteBenchReuse(t *testing.T) {
|
||||
t.Skip()
|
||||
//t.Skip()
|
||||
var (
|
||||
numSubConn = 10
|
||||
numWrites = 10000
|
||||
|
|
|
@ -11,6 +11,11 @@ var (
|
|||
ErrConnClosed = errors.New("connection closed")
|
||||
)
|
||||
|
||||
const (
|
||||
Yamux = "yamux"
|
||||
Quic = "quic"
|
||||
)
|
||||
|
||||
// Transport is a common interface for a network transport
|
||||
type Transport interface {
|
||||
// SetAccepter sets accepter that will be called for new connections
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
)
|
||||
|
||||
func NewMultiConn(cctx context.Context, luConn *connutil.LastUsageConn, addr string, sess *yamux.Session) transport.MultiConn {
|
||||
cctx = peer.CtxWithPeerAddr(cctx, sess.RemoteAddr().String())
|
||||
cctx = peer.CtxWithPeerAddr(cctx, transport.Yamux+"://"+sess.RemoteAddr().String())
|
||||
return &yamuxConn{
|
||||
ctx: cctx,
|
||||
luConn: luConn,
|
||||
|
@ -44,7 +44,7 @@ func (y *yamuxConn) Context() context.Context {
|
|||
}
|
||||
|
||||
func (y *yamuxConn) Addr() string {
|
||||
return y.addr
|
||||
return transport.Yamux + "://" + y.addr
|
||||
}
|
||||
|
||||
func (y *yamuxConn) Accept() (conn net.Conn, err error) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue