Reduce CPU usage when --timeout=0

* Add second go routine for when a Timer is not needed.
* goimports updated some project files

Fixes #5531

Signed-off-by: Jhon Honce <jhonce@redhat.com>
This commit is contained in:
Jhon Honce
2020-03-18 09:26:52 -07:00
parent d9eb078e2a
commit 651ddd3560
5 changed files with 65 additions and 41 deletions

View File

@ -83,10 +83,6 @@ func newServer(runtime *libpod.Runtime, duration time.Duration, listener *net.Li
ConnectionCh: make(chan int), ConnectionCh: make(chan int),
} }
server.Timer = time.AfterFunc(server.Duration, func() {
server.ConnectionCh <- NOOPHandler
})
router.NotFoundHandler = http.HandlerFunc( router.NotFoundHandler = http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) { func(w http.ResponseWriter, r *http.Request) {
// We can track user errors... // We can track user errors...
@ -139,36 +135,15 @@ func newServer(runtime *libpod.Runtime, duration time.Duration, listener *net.Li
// Serve starts responding to HTTP requests // Serve starts responding to HTTP requests
func (s *APIServer) Serve() error { func (s *APIServer) Serve() error {
// stalker to count the connections. Should the timer expire it will shutdown the service. // This is initialized here as Timer is not needed until Serve'ing
go func() { if s.Duration > 0 {
for delta := range s.ConnectionCh { s.Timer = time.AfterFunc(s.Duration, func() {
switch delta { s.ConnectionCh <- NOOPHandler
case EnterHandler: })
s.Timer.Stop() go s.ReadChannelWithTimeout()
s.ActiveConnections += 1 } else {
s.TotalConnections += 1 go s.ReadChannelNoTimeout()
case ExitHandler: }
s.Timer.Stop()
s.ActiveConnections -= 1
if s.ActiveConnections == 0 {
// Server will be shutdown iff the timer expires before being reset or stopped
s.Timer = time.AfterFunc(s.Duration, func() {
if err := s.Shutdown(); err != nil {
logrus.Errorf("Failed to shutdown APIServer: %v", err)
os.Exit(1)
}
})
} else {
s.Timer.Reset(s.Duration)
}
case NOOPHandler:
// push the check out another duration...
s.Timer.Reset(s.Duration)
default:
logrus.Errorf("ConnectionCh received unsupported input %d", delta)
}
}
}()
sigChan := make(chan os.Signal, 1) sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
@ -193,6 +168,53 @@ func (s *APIServer) Serve() error {
return nil return nil
} }
func (s *APIServer) ReadChannelWithTimeout() {
// stalker to count the connections. Should the timer expire it will shutdown the service.
for delta := range s.ConnectionCh {
switch delta {
case EnterHandler:
s.Timer.Stop()
s.ActiveConnections += 1
s.TotalConnections += 1
case ExitHandler:
s.Timer.Stop()
s.ActiveConnections -= 1
if s.ActiveConnections == 0 {
// Server will be shutdown iff the timer expires before being reset or stopped
s.Timer = time.AfterFunc(s.Duration, func() {
if err := s.Shutdown(); err != nil {
logrus.Errorf("Failed to shutdown APIServer: %v", err)
os.Exit(1)
}
})
} else {
s.Timer.Reset(s.Duration)
}
case NOOPHandler:
// push the check out another duration...
s.Timer.Reset(s.Duration)
default:
logrus.Warnf("ConnectionCh received unsupported input %d", delta)
}
}
}
func (s *APIServer) ReadChannelNoTimeout() {
// stalker to count the connections.
for delta := range s.ConnectionCh {
switch delta {
case EnterHandler:
s.ActiveConnections += 1
s.TotalConnections += 1
case ExitHandler:
s.ActiveConnections -= 1
case NOOPHandler:
default:
logrus.Warnf("ConnectionCh received unsupported input %d", delta)
}
}
}
// Shutdown is a clean shutdown waiting on existing clients // Shutdown is a clean shutdown waiting on existing clients
func (s *APIServer) Shutdown() error { func (s *APIServer) Shutdown() error {
// Duration == 0 flags no auto-shutdown of the server // Duration == 0 flags no auto-shutdown of the server

View File

@ -152,7 +152,7 @@ func (b *bindingTest) startAPIService() *gexec.Session {
var ( var (
cmd []string cmd []string
) )
cmd = append(cmd, "--log-level=debug", "system", "service", "--timeout=999999", b.sock) cmd = append(cmd, "--log-level=debug", "system", "service", "--timeout=0", b.sock)
return b.runPodman(cmd) return b.runPodman(cmd)
} }

View File

@ -3,11 +3,12 @@ package test_bindings
import ( import (
"context" "context"
"fmt" "fmt"
"net/http"
"time"
"github.com/containers/libpod/pkg/api/handlers" "github.com/containers/libpod/pkg/api/handlers"
"github.com/containers/libpod/pkg/bindings/containers" "github.com/containers/libpod/pkg/bindings/containers"
"github.com/containers/libpod/pkg/bindings/volumes" "github.com/containers/libpod/pkg/bindings/volumes"
"net/http"
"time"
"github.com/containers/libpod/pkg/bindings" "github.com/containers/libpod/pkg/bindings"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"

View File

@ -10,7 +10,7 @@ import (
"time" "time"
"github.com/containers/image/v5/pkg/sysregistriesv2" "github.com/containers/image/v5/pkg/sysregistriesv2"
"github.com/containers/libpod/cmd/podman/varlink" iopodman "github.com/containers/libpod/cmd/podman/varlink"
"github.com/containers/libpod/libpod/define" "github.com/containers/libpod/libpod/define"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )

View File

@ -5,13 +5,14 @@ package integration
import ( import (
"bytes" "bytes"
"fmt" "fmt"
. "github.com/containers/libpod/test/utils"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil" "io/ioutil"
"os" "os"
"strconv" "strconv"
"text/template" "text/template"
. "github.com/containers/libpod/test/utils"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
) )
type endpoint struct { type endpoint struct {