Skip to content

Commit

Permalink
Add src, dst current time
Browse files Browse the repository at this point in the history
Signed-off-by: Dongri Jin <[email protected]>
  • Loading branch information
dongrie committed Nov 8, 2023
1 parent 9f88b20 commit 8748e4c
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type RelayService struct {
interval time.Duration
optimizeInterval time.Duration
optimizeCount int64
currentTime time.Time
srcCurrentTime time.Time
dstCurrentTime time.Time
}

// NewRelayService returns a new service
Expand All @@ -48,7 +49,8 @@ func NewRelayService(
interval: interval,
optimizeInterval: optimizeInterval,
optimizeCount: optimizeCount,
currentTime: time.Now(),
srcCurrentTime: time.Now(),
dstCurrentTime: time.Now(),
}
}

Expand Down Expand Up @@ -136,16 +138,22 @@ func (srv *RelayService) Serve(ctx context.Context) error {
}

func (srv *RelayService) optimizeRelay(pseqs RelayPackets) bool {
// time interval
elapseTime := time.Since(srv.currentTime)
if elapseTime >= srv.optimizeInterval {
return true
}
// packet count
srcPacketCount := len(pseqs.Src)
dstPacketCount := len(pseqs.Dst)
if int64(srcPacketCount) >= srv.optimizeCount || int64(dstPacketCount) >= srv.optimizeCount {
return true
}
// time interval
if time.Since(srv.srcCurrentTime) >= srv.optimizeInterval || time.Since(srv.dstCurrentTime) >= srv.optimizeInterval {
return true
}
// set current time
if srcPacketCount > 0 {
srv.srcCurrentTime = time.Now()
}
if dstPacketCount > 0 {
srv.dstCurrentTime = time.Now()
}
return false
}

0 comments on commit 8748e4c

Please sign in to comment.