Yggdrasil demo tweaks

This commit is contained in:
Neil Alexander 2020-06-10 17:18:37 +01:00
parent 399b6ae334
commit 63a24e81c4
2 changed files with 18 additions and 9 deletions

View file

@ -96,9 +96,9 @@ func main() {
httpServer := &http.Server{ httpServer := &http.Server{
Addr: ":0", Addr: ":0",
TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){}, TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){},
ReadTimeout: 30 * time.Second, ReadTimeout: 5 * time.Second,
WriteTimeout: 30 * time.Second, WriteTimeout: 5 * time.Second,
IdleTimeout: 60 * time.Second, IdleTimeout: 15 * time.Second,
BaseContext: func(_ net.Listener) context.Context { BaseContext: func(_ net.Listener) context.Context {
return context.Background() return context.Background()
}, },

View file

@ -25,8 +25,8 @@ import (
func (n *Node) yamuxConfig() *yamux.Config { func (n *Node) yamuxConfig() *yamux.Config {
cfg := yamux.DefaultConfig() cfg := yamux.DefaultConfig()
cfg.EnableKeepAlive = true cfg.EnableKeepAlive = false
cfg.KeepAliveInterval = time.Second * 30 cfg.ConnectionWriteTimeout = time.Second * 5
cfg.MaxMessageSize = 65535 cfg.MaxMessageSize = 65535
cfg.ReadBufSize = 655350 cfg.ReadBufSize = 655350
return cfg return cfg
@ -40,6 +40,8 @@ func (n *Node) listenFromYgg() {
return return
} }
var session *yamux.Session var session *yamux.Session
// If the remote address is lower than ours then we'll be the
// server. Otherwse we'll be the client.
if strings.Compare(conn.RemoteAddr().String(), n.DerivedServerName()) < 0 { if strings.Compare(conn.RemoteAddr().String(), n.DerivedServerName()) < 0 {
session, err = yamux.Server(conn, n.yamuxConfig()) session, err = yamux.Server(conn, n.yamuxConfig())
} else { } else {
@ -55,6 +57,11 @@ func (n *Node) listenFromYgg() {
func (n *Node) listenFromYggConn(session *yamux.Session) { func (n *Node) listenFromYggConn(session *yamux.Session) {
n.sessions.Store(session.RemoteAddr().String(), session) n.sessions.Store(session.RemoteAddr().String(), session)
defer n.sessions.Delete(session.RemoteAddr()) defer n.sessions.Delete(session.RemoteAddr())
defer func() {
if err := session.Close(); err != nil {
n.log.Println("session.Close:", err)
}
}()
for { for {
st, err := session.AcceptStream() st, err := session.AcceptStream()
@ -90,16 +97,18 @@ func (n *Node) Dial(network, address string) (net.Conn, error) {
func (n *Node) DialContext(ctx context.Context, network, address string) (net.Conn, error) { func (n *Node) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
s, ok1 := n.sessions.Load(address) s, ok1 := n.sessions.Load(address)
session, ok2 := s.(*yamux.Session) session, ok2 := s.(*yamux.Session)
if !ok1 || !ok2 { if !ok1 || !ok2 || (ok1 && ok2 && session.IsClosed()) {
conn, err := n.dialer.DialContext(ctx, network, address) conn, err := n.dialer.DialContext(ctx, network, address)
if err != nil { if err != nil {
n.log.Println("n.dialer.DialContext:", err) n.log.Println("n.dialer.DialContext:", err)
return nil, err return nil, err
} }
if strings.Compare(address, n.DerivedServerName()) > 0 { // If the remote address is lower than ours then we will be the
session, err = yamux.Client(conn, n.yamuxConfig()) // server. Otherwise we'll be the client.
} else { if strings.Compare(conn.RemoteAddr().String(), n.DerivedServerName()) < 0 {
session, err = yamux.Server(conn, n.yamuxConfig()) session, err = yamux.Server(conn, n.yamuxConfig())
} else {
session, err = yamux.Client(conn, n.yamuxConfig())
} }
if err != nil { if err != nil {
return nil, err return nil, err