Skip to content

Commit

Permalink
client: if an idle timeout of 0 is specified, do not start the idle t…
Browse files Browse the repository at this point in the history
…imeout goroutine

keys: add Public() to derive public key from private key
node/opts: perform basic param checks for options
node/opts: add quick.Check tests for options
  • Loading branch information
iwasaki-kenta committed Jan 29, 2020
1 parent 6c30632 commit 5767957
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# noise

[![GoDoc][1]][2] [![Discord][7]][8] [![MIT licensed][5]][6] ![Build Status][9] [![Go Report Card][11]][12] [![Coverage Statusd][13]][14]
[![GoDoc][1]][2] [![Discord][7]][8] [![MIT licensed][5]][6] ![Build Status][9] [![Go Report Card][11]][12] [![Coverage Status][13]][14]

[1]: https://godoc.org/github.com/perlin-network/noise?status.svg
[2]: https://godoc.org/github.com/perlin-network/noise
Expand Down
7 changes: 6 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,14 @@ func (c *Client) waitUntilClosed() {
}

func (c *Client) startTimeout(ctx context.Context) {
c.timeout.timer = time.NewTimer(c.node.idleTimeout)
c.timeout.reset = make(chan struct{}, 1)

if c.node.idleTimeout == 0 {
return
}

c.timeout.timer = time.NewTimer(c.node.idleTimeout)

go func() {
defer c.timeout.timer.Stop()

Expand Down
8 changes: 8 additions & 0 deletions keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,11 @@ func (k PrivateKey) String() string {
func (k PrivateKey) MarshalJSON() ([]byte, error) {
return json.Marshal(hex.EncodeToString(k[:]))
}

// Public returns the public key associated to this private key.
func (k PrivateKey) Public() PublicKey {
var publicKey PublicKey
copy(publicKey[:], (ed25519.PrivateKey)(k[:]).Public().(ed25519.PublicKey))

return publicKey
}
17 changes: 17 additions & 0 deletions node_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type NodeOption func(n *Node)
// failed. By default, the max number of attempts a connection is dialed is 3.
func WithNodeMaxDialAttempts(maxDialAttempts int) NodeOption {
return func(n *Node) {
if maxDialAttempts <= 0 {
maxDialAttempts = 1
}

n.maxDialAttempts = maxDialAttempts
}
}
Expand All @@ -23,6 +27,10 @@ func WithNodeMaxDialAttempts(maxDialAttempts int) NodeOption {
// causes the connection pool to release the oldest inbound connection in the pool.
func WithNodeMaxInboundConnections(maxInboundConnections int) NodeOption {
return func(n *Node) {
if maxInboundConnections <= 0 {
maxInboundConnections = 128
}

n.maxInboundConnections = maxInboundConnections
}
}
Expand All @@ -32,12 +40,17 @@ func WithNodeMaxInboundConnections(maxInboundConnections int) NodeOption {
// max number causes the connection pool to release the oldest outbound connection in the pool.
func WithNodeMaxOutboundConnections(maxOutboundConnections int) NodeOption {
return func(n *Node) {
if maxOutboundConnections <= 0 {
maxOutboundConnections = 128
}

n.maxOutboundConnections = maxOutboundConnections
}
}

// WithNodeIdleTimeout sets the duration in which should there be no subsequent reads/writes on a connection, the
// connection shall timeout and have resources related to it released. By default, the timeout is set to be 3 seconds.
// If an idle timeout of 0 is specified, idle timeouts will be disabled.
func WithNodeIdleTimeout(idleTimeout time.Duration) NodeOption {
return func(n *Node) {
n.idleTimeout = idleTimeout
Expand All @@ -48,6 +61,10 @@ func WithNodeIdleTimeout(idleTimeout time.Duration) NodeOption {
// disables any logs.
func WithNodeLogger(logger *zap.Logger) NodeOption {
return func(n *Node) {
if logger == nil {
logger = zap.NewNop()
}

n.logger = logger
}
}
Expand Down
172 changes: 172 additions & 0 deletions node_options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package noise

import (
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"net"
"testing"
"testing/quick"
"time"
)

func TestNodeOptions(t *testing.T) {
a := func(a int) bool {
if a <= 0 {
a = 1
}

n, err := NewNode(WithNodeMaxDialAttempts(a))
if !assert.NoError(t, err) {
return false
}

if !assert.EqualValues(t, n.maxDialAttempts, a) {
return false
}

return true
}

assert.NoError(t, quick.Check(a, nil))

b := func(a int) bool {
if a <= 0 {
a = 128
} else if a > 1000 {
a = 1000
}

n, err := NewNode(WithNodeMaxInboundConnections(a))
if !assert.NoError(t, err) {
return false
}

if !assert.EqualValues(t, n.maxInboundConnections, a) {
return false
}

return true
}

assert.NoError(t, quick.Check(b, nil))

c := func(a int) bool {
if a <= 0 {
a = 128
} else if a > 1000 {
a = 1000
}

n, err := NewNode(WithNodeMaxOutboundConnections(a))
if !assert.NoError(t, err) {
return false
}

if !assert.EqualValues(t, n.maxOutboundConnections, a) {
return false
}

return true
}

assert.NoError(t, quick.Check(c, nil))

d := func(a time.Duration) bool {
n, err := NewNode(WithNodeIdleTimeout(a))
if !assert.NoError(t, err) {
return false
}

if !assert.EqualValues(t, n.idleTimeout, a) {
return false
}

return true
}

assert.NoError(t, quick.Check(d, nil))

e := func(a bool) bool {
var logger *zap.Logger
if a {
logger = zap.NewNop()
}

n, err := NewNode(WithNodeLogger(logger))
if !assert.NoError(t, err) {
return false
}

if !assert.NotNil(t, n.logger) {
return false
}

return true
}

assert.NoError(t, quick.Check(e, nil))

f := func(publicKey PublicKey, host net.IP, port uint16) bool {
h := host.String() // Make-shift 'normalizeIP(net.IP)'.
if h == "<nil>" {
h = ""
}

id := NewID(publicKey, host, port)

n, err := NewNode(WithNodeID(id))
if !assert.NoError(t, err) {
return false
}

if !assert.EqualValues(t, n.id, id) {
return false
}

return true
}

assert.NoError(t, quick.Check(f, nil))

g := func(privateKey PrivateKey) bool {
n, err := NewNode(WithNodePrivateKey(privateKey))
if !assert.NoError(t, err) {
return false
}

if !assert.EqualValues(t, n.privateKey, privateKey) {
return false
}

if !assert.EqualValues(t, n.publicKey, privateKey.Public()) {
return false
}

return true
}

assert.NoError(t, quick.Check(g, nil))

h := func(host net.IP, port uint16, address string) bool {
n, err := NewNode(WithNodeBindHost(host), WithNodeBindPort(port), WithNodeAddress(address))
if !assert.NoError(t, err) {
return false
}

if !assert.EqualValues(t, n.host, host) {
return false
}

if !assert.EqualValues(t, n.port, port) {
return false
}

if !assert.EqualValues(t, n.addr, address) {
return false
}

return true
}

assert.NoError(t, quick.Check(h, nil))
}

0 comments on commit 5767957

Please sign in to comment.