Skip to content

Commit

Permalink
Change rule's epireAt -> keepAlive
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Aug 22, 2019
1 parent 0689138 commit 5e7d081
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 65 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ require (
)

// Uncomment for tests with alternate branches of 'dmsg'
//replace github.com/skycoin/dmsg => ../dmsg
replace github.com/skycoin/dmsg => ../dmsg
79 changes: 17 additions & 62 deletions pkg/routing/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"time"
"unsafe"

"github.com/google/uuid"
"github.com/skycoin/dmsg/cipher"
Expand Down Expand Up @@ -37,60 +36,14 @@ const (
RuleForward
)

var bigEndian bool

func init() {
var x uint32 = 0x01020304
if *(*byte)(unsafe.Pointer(&x)) == 0x04 {
bigEndian = false
}
}

func putInt64BigEndian(b []byte, v int64) {
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808

data := *(*[8]byte)(unsafe.Pointer(&v))

if !bigEndian {
b[0] = data[7]
b[1] = data[6]
b[2] = data[5]
b[3] = data[4]
b[4] = data[3]
b[5] = data[2]
b[6] = data[1]
b[7] = data[0]
} else {
b[0] = data[0]
b[1] = data[1]
b[2] = data[2]
b[3] = data[3]
b[4] = data[4]
b[5] = data[5]
b[6] = data[6]
b[7] = data[7]
}
}

func readInt64BigEndian(b []byte) int64 {
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808

if bigEndian {
return *(*int64)(unsafe.Pointer(&b[0]))
} else {
bRev := [8]byte{b[7], b[6], b[5], b[4], b[3], b[2], b[1], b[0]}
return *(*int64)(unsafe.Pointer(&bRev[0]))
}
}

// Rule represents a routing rule.
// There are two types of routing rules; App and Forward.
//
type Rule []byte

// KeepAlive returns rule's keep-alive timeout.
func (r Rule) KeepAlive() time.Duration {
return time.Unix(int64(ts), 0)
return time.Duration(binary.BigEndian.Uint64(r))
}

// Type returns type of a rule.
Expand Down Expand Up @@ -184,7 +137,7 @@ type RuleForwardFields struct {

// RuleSummary provides a summary of a RoutingRule.
type RuleSummary struct {
ExpireAt time.Time `json:"expire_at"`
KeepAlive time.Duration `json:"keep_alive"`
Type RuleType `json:"rule_type"`
AppFields *RuleAppFields `json:"app_fields,omitempty"`
ForwardFields *RuleForwardFields `json:"forward_fields,omitempty"`
Expand All @@ -195,19 +148,19 @@ type RuleSummary struct {
func (rs *RuleSummary) ToRule() (Rule, error) {
if rs.Type == RuleApp && rs.AppFields != nil && rs.ForwardFields == nil {
f := rs.AppFields
return AppRule(rs.ExpireAt, f.RespRID, f.RemotePK, f.RemotePort, f.LocalPort, rs.RequestRouteID), nil
return AppRule(rs.KeepAlive, f.RespRID, f.RemotePK, f.RemotePort, f.LocalPort, rs.RequestRouteID), nil
}
if rs.Type == RuleForward && rs.AppFields == nil && rs.ForwardFields != nil {
f := rs.ForwardFields
return ForwardRule(rs.ExpireAt, f.NextRID, f.NextTID, rs.RequestRouteID), nil
return ForwardRule(rs.KeepAlive, f.NextRID, f.NextTID, rs.RequestRouteID), nil
}
return nil, errors.New("invalid routing rule summary")
}

// Summary returns the RoutingRule's summary.
func (r Rule) Summary() *RuleSummary {
summary := RuleSummary{
ExpireAt: r.Expiry(),
KeepAlive: r.KeepAlive(),
Type: r.Type(),
RequestRouteID: r.RequestRouteID(),
}
Expand All @@ -228,15 +181,16 @@ func (r Rule) Summary() *RuleSummary {
}

// AppRule constructs a new consume RoutingRule.
func AppRule(expireAt time.Time, respRoute RouteID, remotePK cipher.PubKey, remotePort, localPort Port,
func AppRule(keepAlive time.Duration, respRoute RouteID, remotePK cipher.PubKey, remotePort, localPort Port,
requestRouteID RouteID) Rule {
rule := make([]byte, RuleHeaderSize)
if expireAt.Unix() <= time.Now().Unix() {
binary.BigEndian.PutUint64(rule[0:], 0)
} else {
binary.BigEndian.PutUint64(rule[0:], uint64(expireAt.Unix()))

if keepAlive < 0 {
keepAlive = 0
}

binary.BigEndian.PutUint64(rule, uint64(keepAlive))

rule[8] = byte(RuleApp)
binary.BigEndian.PutUint32(rule[9:], uint32(respRoute))
rule = append(rule, remotePK[:]...)
Expand All @@ -248,14 +202,15 @@ func AppRule(expireAt time.Time, respRoute RouteID, remotePK cipher.PubKey, remo
}

// ForwardRule constructs a new forward RoutingRule.
func ForwardRule(expireAt time.Time, nextRoute RouteID, nextTrID uuid.UUID, requestRouteID RouteID) Rule {
func ForwardRule(keepAlive time.Duration, nextRoute RouteID, nextTrID uuid.UUID, requestRouteID RouteID) Rule {
rule := make([]byte, RuleHeaderSize)
if expireAt.Unix() <= time.Now().Unix() {
binary.BigEndian.PutUint64(rule[0:], 0)
} else {
binary.BigEndian.PutUint64(rule[0:], uint64(expireAt.Unix()))

if keepAlive < 0 {
keepAlive = 0
}

binary.BigEndian.PutUint64(rule, uint64(keepAlive))

rule[8] = byte(RuleForward)
binary.BigEndian.PutUint32(rule[9:], uint32(nextRoute))
rule = append(rule, nextTrID[:]...)
Expand Down
4 changes: 2 additions & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ github.com/prometheus/procfs/internal/fs
# github.com/sirupsen/logrus v1.4.2
github.com/sirupsen/logrus
github.com/sirupsen/logrus/hooks/syslog
# github.com/skycoin/dmsg v0.0.0-20190805065636-70f4c32a994f => ../dmsg
# github.com/skycoin/dmsg v0.0.0-20190816104216-d18ee6aa05cb => ../dmsg
github.com/skycoin/dmsg/cipher
github.com/skycoin/dmsg
github.com/skycoin/dmsg/disc
Expand All @@ -84,7 +84,7 @@ github.com/stretchr/testify/assert
github.com/stretchr/testify/require
# go.etcd.io/bbolt v1.3.3
go.etcd.io/bbolt
# golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
# golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586
golang.org/x/crypto/ssh/terminal
golang.org/x/crypto/blake2b
golang.org/x/crypto/blake2s
Expand Down

0 comments on commit 5e7d081

Please sign in to comment.