Skip to content

Commit

Permalink
feat: add optional delay after connect (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnodorp authored Dec 1, 2024
1 parent f6f0735 commit b217269
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ Usage:
Flags:
-p, --applicationProtocol string multiplexer will parse to message echo/http/iso8583 (default "echo")
--delay duration delay after connect
-h, --help help for server
-l, --listen string multiplexer will listen on (default "8000")
-t, --targetServer string multiplexer will forward message to (default "127.0.0.1:1234")
--timeout int timeout in seconds (default 60)
Global Flags:
-v, --verbose verbose log
Expand Down
4 changes: 3 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
targetServer string
applicationProtocol string
timeout int
delay time.Duration
)

// serverCmd represents the server command
Expand All @@ -60,7 +61,7 @@ var serverCmd = &cobra.Command{
os.Exit(2)
}

mux := multiplexer.New(targetServer, port, msgReader, time.Duration(timeout)*time.Second)
mux := multiplexer.New(targetServer, port, msgReader, delay, time.Duration(timeout)*time.Second)
go func() {
err := mux.Start()
if err != nil {
Expand Down Expand Up @@ -92,4 +93,5 @@ func init() {
serverCmd.Flags().StringVarP(&targetServer, "targetServer", "t", "127.0.0.1:1234", "multiplexer will forward message to")
serverCmd.Flags().StringVarP(&applicationProtocol, "applicationProtocol", "p", "echo", "multiplexer will parse to message echo/http/iso8583/modbus")
serverCmd.Flags().IntVar(&timeout, "timeout", 60, "timeout in seconds")
serverCmd.Flags().DurationVar(&delay, "delay", 0, "delay after connect")
}
10 changes: 9 additions & 1 deletion pkg/multiplexer/multiplexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type (
port string
messageReader message.Reader
timeout time.Duration
delay time.Duration
l net.Listener
quit chan struct{}
wg *sync.WaitGroup
Expand All @@ -43,12 +44,13 @@ const (
Packet
)

func New(targetServer, port string, messageReader message.Reader, timeout time.Duration) Multiplexer {
func New(targetServer, port string, messageReader message.Reader, delay time.Duration, timeout time.Duration) Multiplexer {
return Multiplexer{
targetServer: targetServer,
port: port,
messageReader: messageReader,
quit: make(chan struct{}),
delay: delay,
timeout: timeout,
}
}
Expand Down Expand Up @@ -169,6 +171,12 @@ func (mux *Multiplexer) createTargetConn() net.Conn {
}

slog.Info(fmt.Sprintf("new target connection: %v <-> %v", conn.LocalAddr(), conn.RemoteAddr()))

if mux.delay > 0 {
slog.Info(fmt.Sprintf("waiting %s, before using new target connection", mux.delay))
time.Sleep(mux.delay)
}

return conn
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/multiplexer/multiplexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestMultiplexer_Start(t *testing.T) {
}()
const muxServer = "127.0.0.1:1235"

mux := New(l.Addr().String(), "1235", message.EchoMessageReader{})
mux := New(l.Addr().String(), "1235", message.EchoMessageReader{}, 0, 5*time.Second)

errChan := make(chan error, 1)
go func() {
Expand Down

0 comments on commit b217269

Please sign in to comment.