aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Barnett <noreply@jamesbarnett.xyz>2018-12-27 21:52:25 +0000
committerJames Barnett <noreply@jamesbarnett.xyz>2018-12-27 21:52:25 +0000
commit7672da39aafb3e9a2fcbb0bce236dff8b7613355 (patch)
tree19e8619a6ff94136cf38e871c40ff8acdd612e8b
parent370f8120e4ddb8cef35c62f77b060599c92a0c53 (diff)
downloadslowpoke-7672da39aafb3e9a2fcbb0bce236dff8b7613355.tar.xz
slowpoke-7672da39aafb3e9a2fcbb0bce236dff8b7613355.zip
Remove cmd package. Cleanup logging.
-rw-r--r--main.go (renamed from cmd/slowpoke/main.go)6
-rw-r--r--slowpoke.go17
2 files changed, 6 insertions, 17 deletions
diff --git a/cmd/slowpoke/main.go b/main.go
index 93f62b0..5983a33 100644
--- a/cmd/slowpoke/main.go
+++ b/main.go
@@ -8,8 +8,6 @@ import (
"github.com/jessevdk/go-flags"
"github.com/op/go-logging"
-
- "github.com/jamesbarnett91/slowpoke"
)
var log = logging.MustGetLogger("main")
@@ -18,7 +16,7 @@ var opts struct {
TargetAddress string `short:"t" long:"target" description:"The target address in host:port form" required:"true"`
Port int `short:"p" long:"port" description:"The port Slowpoke should listen for connections on" required:"true"`
Verbose []bool `short:"v" long:"verbose" description:"Log verbosity level. -v or -vv"`
- Latency time.Duration `short:"l" long:"latency" default:"0ms" description:"The amount of latency to apply to data packets, specified as a number and unit. E.g. 15ms or 2s. Supported units are 'us', 'ms', 's', 'm' and 'h'"`
+ Latency time.Duration `short:"l" long:"latency" default:"0ms" description:"The duration of latency to apply to data packets, specified as a number and unit. E.g. 15ms or 2s. Supported units are 'us', 'ms', 's', 'm' and 'h'"`
BufferSize int `short:"b" long:"buffer" default:"1500" description:"The size of the transfer buffer in bytes. Latency is applied between each buffer flush. Therefore total latency applied is equal to '(totalDataTransferred/bufferSize) * latency'"`
}
@@ -86,7 +84,7 @@ func waitForClients(listener net.Listener, targetAddr *net.TCPAddr) {
}
log.Infof("Accepted connection from client %v\n", client.RemoteAddr())
- s := slowpoke.New(client, targetAddr, opts.Latency, opts.BufferSize, log)
+ s := NewSlowpoke(client, targetAddr, opts.Latency, opts.BufferSize, log)
go s.StartTransfer()
}
diff --git a/slowpoke.go b/slowpoke.go
index 4a36306..fb491a1 100644
--- a/slowpoke.go
+++ b/slowpoke.go
@@ -1,4 +1,4 @@
-package slowpoke
+package main
import (
"io"
@@ -18,7 +18,7 @@ type Slowpoke struct {
logger *logging.Logger
}
-func New(conn net.Conn, targetAddr *net.TCPAddr, latency time.Duration, bufferSize int, logger *logging.Logger) *Slowpoke {
+func NewSlowpoke(conn net.Conn, targetAddr *net.TCPAddr, latency time.Duration, bufferSize int, logger *logging.Logger) *Slowpoke {
return &Slowpoke{
conn: conn,
targetAddr: targetAddr,
@@ -55,25 +55,16 @@ func (s *Slowpoke) createBuffer() []byte {
func (s *Slowpoke) transferWithLatency(source net.Conn, target net.Conn) {
byteBuffer := s.createBuffer()
- var transferDirection string
- // If the data source is the client then we are sending
- if source == s.conn {
- transferDirection = "%d bytes sent"
- } else {
- transferDirection = "%d bytes received"
- }
-
for {
bytesRead, readError := source.Read(byteBuffer)
if bytesRead > 0 {
+ s.logger.Debugf("Transferring %d bytes from %s to %s with %s added latency", bytesRead, source.RemoteAddr(), target.RemoteAddr(), s.latency)
+
if s.latency != 0 {
- s.logger.Debugf(transferDirection+" with latency of %s", bytesRead, s.latency)
time.Sleep(s.latency)
- } else {
- s.logger.Debugf(transferDirection, bytesRead)
}
bytesWritten, writeError := target.Write(byteBuffer[0:bytesRead])