Skip to content

Commit

Permalink
feat: free memory periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
thoas committed Sep 25, 2023
1 parent a18ca8d commit 4281140
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
4 changes: 1 addition & 3 deletions cmd/picfit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ func main() {
os.Exit(1)
}

err := server.Run(context.Background(), config)

if err != nil {
if err := server.Run(context.Background(), config); err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
Expand Down
8 changes: 6 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Options struct {
EnableUpload bool `mapstructure:"enable_upload"`
EnablePrometheus bool `mapstructure:"enable_prometheus"`
MimetypeDetector string `mapstructure:"mimetype_detector"`
FreeMemoryInterval int `mapstructure:"free_memory_interval"`
}

// Sentry is a struct to configure sentry using a dsn
Expand Down Expand Up @@ -125,11 +126,14 @@ func load(content string, isPath bool) (*Config, error) {
}
}

err = viper.Unmarshal(&config)
if err != nil {
if err = viper.Unmarshal(&config); err != nil {
return nil, err
}

if config.Options.FreeMemoryInterval == 0 {
config.Options.FreeMemoryInterval = 10
}

return config, nil
}

Expand Down
26 changes: 23 additions & 3 deletions server/http.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"context"
"fmt"
"net/http"
"net/http/pprof"
Expand Down Expand Up @@ -39,6 +40,10 @@ func NewHTTPServer(cfg *config.Config, processor *picfit.Processor) (*HTTPServer
return server, nil
}

func (s *HTTPServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
s.engine.ServeHTTP(w, req)
}

func (s *HTTPServer) Init() error {
var (
router = gin.New()
Expand Down Expand Up @@ -223,8 +228,23 @@ func prometheusHandler() gin.HandlerFunc {
}

// Run loads a new http server
func (s *HTTPServer) Run() error {
s.engine.Run(fmt.Sprintf(":%s", strconv.Itoa(s.config.Port)))
func (s *HTTPServer) Run(ctx context.Context) error {
addr := fmt.Sprintf(":%s", strconv.Itoa(s.config.Port))
srv := &http.Server{
Addr: addr,
Handler: s.engine.Handler(),
}

return nil
cerr := make(chan error)
go func() {
s.processor.Logger.Debug(fmt.Sprintf("Starting HTTP server on %s", addr))
cerr <- srv.ListenAndServe()
}()
select {
case err := <-cerr:
return err
case <-ctx.Done():
s.processor.Logger.Debug("Shutdown HTTP server")
return srv.Shutdown(context.Background())
}
}
40 changes: 23 additions & 17 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package server

import (
"context"
"net/http"
"os/signal"
"runtime/debug"
"syscall"
"time"

"github.com/thoas/picfit"
"github.com/thoas/picfit/config"
)

type Server struct {
http *HTTPServer
}

func New(ctx context.Context, cfg *config.Config) (*Server, error) {
func New(ctx context.Context, cfg *config.Config) (*HTTPServer, error) {
processor, err := picfit.NewProcessor(ctx, cfg)
if err != nil {
return nil, err
Expand All @@ -23,16 +22,7 @@ func New(ctx context.Context, cfg *config.Config) (*Server, error) {
return nil, err
}

server := &Server{http: httpServer}
return server, nil
}

func (s *Server) Run() error {
return s.http.Run()
}

func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
s.http.engine.ServeHTTP(w, req)
return httpServer, nil
}

// Run runs the application and launch servers
Expand All @@ -46,6 +36,22 @@ func Run(ctx context.Context, path string) error {
if err != nil {
return err
}
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer stop()
if err := server.Run(ctx); err != nil {
return err
}

go func() {
for range time.Tick(time.Duration(cfg.Options.FreeMemoryInterval) * time.Second) {
debug.FreeOSMemory()
}
}()

select { // nolint:gosimple
case <-ctx.Done():
stop()
}

return server.Run()
return nil
}

0 comments on commit 4281140

Please sign in to comment.