Skip to content

Commit

Permalink
Adding config option to limit connections to the db
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynom committed Apr 17, 2020
1 parent 606d672 commit 96c7dc6
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 23 deletions.
9 changes: 8 additions & 1 deletion cmd/web/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,17 @@

[server.backend]
# Drivers: https://github.com/golang/go/wiki/SQLDrivers
# Postgres: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
driver = "postgres"

# Postgres: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
url = "postgresql://postgres@localhost:15432/eri?sslmode=disable"

# Maximum connections allowed to the backend
maxConnections = 10

# Maximum idle connections (must be less than maxConnections). 0 Means no idle connections are retained.
maxIdleConnections = 2

[server.graphql]

prettyOutput = true
Expand Down
6 changes: 4 additions & 2 deletions cmd/web/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ type Config struct {
Prefix string `toml:"prefix"`
} `toml:"profiler"`
Backend struct {
Driver string `toml:"driver"`
URL string `toml:"url"`
Driver string `toml:"driver"`
URL string `toml:"url"`
MaxConnections uint `toml:"maxConnections"`
MaxIdleConnections uint `toml:"maxIdleConnections"`
} `toml:"backend"`
GraphQL struct {
PrettyOutput bool `toml:"prettyOutput" flag:"pretty" env:"PRETTY"`
Expand Down
36 changes: 34 additions & 2 deletions cmd/web/erihttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package erihttp
import (
"io"
"log"
"net"
"net/http"
"time"

"github.com/Dynom/ERI/cmd/web/config"
"github.com/sirupsen/logrus"
"golang.org/x/net/netutil"
)

func BuildHTTPServer(mux http.Handler, config config.Config, logWriter io.Writer, handlers ...func(h http.Handler) http.Handler) *http.Server {
func BuildHTTPServer(mux http.Handler, config config.Config, logger logrus.FieldLogger, logWriter io.Writer, handlers ...func(h http.Handler) http.Handler) *Server {
for _, h := range handlers {
mux = h(mux)
}
Expand All @@ -30,5 +33,34 @@ func BuildHTTPServer(mux http.Handler, config config.Config, logWriter io.Writer
ErrorLog: log.New(logWriter, "", 0),
}

return server
listener, err := net.Listen("tcp", config.Server.ListenOn)
if err != nil {
logger.WithFields(logrus.Fields{
"error": err,
"listen_on": config.Server.ListenOn,
}).Error("Unable to start listener")
}

if config.Server.ConnectionLimit > 0 {
listener = netutil.LimitListener(listener, int(config.Server.ConnectionLimit))
}

server.RegisterOnShutdown(func() {
err := listener.Close()
logger.WithError(err).Debug("Closing listener")
})

return &Server{
server: server,
listener: listener,
}
}

type Server struct {
server *http.Server
listener net.Listener
}

func (s *Server) ServeERI() error {
return s.server.Serve(s.listener)
}
5 changes: 4 additions & 1 deletion cmd/web/erihttp/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/Dynom/ERI/cmd/web/config"
"github.com/sirupsen/logrus/hooks/test"
)

func TestBuildHTTPServer(t *testing.T) {
Expand All @@ -26,7 +27,9 @@ func TestBuildHTTPServer(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logWriter := &bytes.Buffer{}
got := BuildHTTPServer(tt.args.mux, tt.args.config, logWriter, tt.args.handlers...)
l, _ := test.NewNullLogger()

got := BuildHTTPServer(tt.args.mux, tt.args.config, l, logWriter, tt.args.handlers...)
if gotLogWriter := logWriter.String(); gotLogWriter != tt.wantLogWriter {
t.Errorf("BuildHTTPServer() gotLogWriter = %v, want %v", gotLogWriter, tt.wantLogWriter)
}
Expand Down
19 changes: 2 additions & 17 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package main

import (
"io"
"net"
"net/http"
"os"
"time"

"github.com/Dynom/ERI/cmd/web/pubsub/gcp"
"github.com/Dynom/ERI/runtimer"
"github.com/rs/cors"
"golang.org/x/net/netutil"

"github.com/Pimmr/rig"

Expand Down Expand Up @@ -148,7 +146,7 @@ func main() {
AllowedHeaders: conf.Server.CORS.AllowedHeaders,
})

s := erihttp.BuildHTTPServer(mux, conf, logWriter,
s := erihttp.BuildHTTPServer(mux, conf, logger, logWriter,
handlers.WithPathStrip(logger, conf.Server.PathStrip),
handlers.NewRateLimitHandler(logger, bucket, conf.Server.RateLimiter.ParkedTTL.AsDuration()),
handlers.WithRequestLogger(logger),
Expand All @@ -161,19 +159,6 @@ func main() {
"listen_on": conf.Server.ListenOn,
}).Info("Done, serving requests")

listener, err := net.Listen("tcp", conf.Server.ListenOn)
if err != nil {
logger.WithFields(logrus.Fields{
"error": err,
"listen_on": conf.Server.ListenOn,
}).Error("Unable to start listener")
}
defer listener.Close()

if conf.Server.ConnectionLimit > 0 {
listener = netutil.LimitListener(listener, int(conf.Server.ConnectionLimit))
}

err = s.Serve(listener)
err = s.ServeERI()
logger.Errorf("HTTP server stopped %s", err)
}
3 changes: 3 additions & 0 deletions cmd/web/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ func createPGPersister(conf config.Config, logger logrus.FieldLogger, hitList *h
return nil, nil, err
}

sqlConn.SetMaxOpenConns(int(conf.Server.Backend.MaxConnections))
sqlConn.SetMaxIdleConns(int(conf.Server.Backend.MaxIdleConnections))

err = sqlConn.Ping()
if err != nil {
return nil, nil, err
Expand Down

0 comments on commit 96c7dc6

Please sign in to comment.