diff --git a/main.go b/main.go index ecbe5b5..d120377 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "github.com/mittwald/mittnite/cmd" log "github.com/sirupsen/logrus" + "os" ) func init() { @@ -10,6 +11,9 @@ func init() { Formatter.TimestampFormat = "02-01-2006 15:04:05" Formatter.FullTimestamp = true log.SetFormatter(Formatter) + if os.Getenv("MITTNITE_LOG_LEVEL") == "debug" { + log.SetLevel(log.DebugLevel) + } } func main() { diff --git a/pkg/proc/job_listener.go b/pkg/proc/job_listener.go index d15f2e7..f6e034f 100644 --- a/pkg/proc/job_listener.go +++ b/pkg/proc/job_listener.go @@ -141,23 +141,34 @@ func (l *Listener) run(ctx context.Context) <-chan error { fromUpstreamErrors := make(chan error) go func() { + defer func() { + if tcpUpstream, ok := upstream.(*net.TCPConn); ok { + tcpUpstream.CloseWrite() + } + close(toUpstreamErrors) + }() + if _, err := io.Copy(upstream, conn); err != nil && !errors.Is(err, io.EOF) { toUpstreamErrors <- err } - close(toUpstreamErrors) }() go func() { + defer func() { + if tcpConn, ok := conn.(*net.TCPConn); ok { + tcpConn.CloseWrite() + } + close(fromUpstreamErrors) + }() + if _, err := io.Copy(conn, upstream); err != nil && !errors.Is(err, io.EOF) { fromUpstreamErrors <- err } - close(fromUpstreamErrors) }() - select { - case <-toUpstreamErrors: - case <-fromUpstreamErrors: - } + // wait for both channels (do not use `select-case`) + <-toUpstreamErrors + <-fromUpstreamErrors }() } }() diff --git a/pkg/proc/runner.go b/pkg/proc/runner.go index 62d33c0..4cdc23b 100644 --- a/pkg/proc/runner.go +++ b/pkg/proc/runner.go @@ -3,6 +3,7 @@ package proc import ( "context" "fmt" + "runtime" "sync" "time" @@ -104,6 +105,7 @@ func (r *Runner) Run() error { // watch files case <-ticker.C: + log.Debugf("active goroutines: %d", runtime.NumGoroutine()) for _, job := range r.jobs { job.Watch() if r.keepRunning {