Skip to content

Commit

Permalink
Further refactoring and finishing of TODO's
Browse files Browse the repository at this point in the history
  • Loading branch information
AJ Bahnken committed Aug 30, 2017
1 parent b7e61fd commit 62cc459
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/signal"
"strconv"
"syscall"
"time"

log "github.com/Sirupsen/logrus"
)
Expand All @@ -18,6 +19,7 @@ type Configuration struct {
PgUsers []string `json:"pgUsers"`
Debug bool `json:"debug"`
Cleartext bool `json:"cleartext"`
TcpTimeout int `json:"server_timeout"`
HpFeedsConfig `json:"hpfeedsConfig"`
}

Expand Down Expand Up @@ -51,6 +53,7 @@ func main() {
debug := config.Debug
cleartext := config.Cleartext
hpFeedsConfig := config.HpFeedsConfig
tcpTimeout := time.Duration(config.TcpTimeout) * time.Second

if debug {
log.SetLevel(log.DebugLevel)
Expand All @@ -66,6 +69,7 @@ func main() {
addr,
pgUsers,
cleartext,
tcpTimeout,
hpfeedsChannel,
hpFeedsConfig.Enabled,
)
Expand Down
4 changes: 3 additions & 1 deletion pgconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"net"
"time"

log "github.com/Sirupsen/logrus"
)
Expand All @@ -18,7 +19,8 @@ type PostgresConnection struct {
postgresPacket readBuf
}

func NewPostgresConnection(conn net.Conn) *PostgresConnection {
func NewPostgresConnection(conn net.Conn, tcpTimeout time.Duration) *PostgresConnection {
conn.SetDeadline(time.Now().Add(tcpTimeout))
return &PostgresConnection{
buffer: make([]byte, maxBufSize),
connection: conn,
Expand Down
1 change: 1 addition & 0 deletions pghoney.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"pgUsers":["postgres"],
"debug":false,
"cleartext":false,
"server_timeout":10,
"hpfeedsConfig":{
"port":10000,
"host":"127.0.0.1",
Expand Down
22 changes: 8 additions & 14 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import (
log "github.com/Sirupsen/logrus"
)

var (
// TODO: Make configurable
tcpTimeout = 10 * time.Second
)

type PostgresServer struct {
listener net.Listener

Expand All @@ -27,11 +22,12 @@ type PostgresServer struct {
addr string
port string

pgUsers map[string]bool
cleartext bool
pgUsers map[string]bool
cleartext bool
tcpTimeout time.Duration
}

func NewPostgresServer(port string, addr string, users []string, cleartext bool, hpfeedsChan chan []byte, hpfeedsEnabled bool) *PostgresServer {
func NewPostgresServer(port string, addr string, users []string, cleartext bool, tcpTimeout time.Duration, hpfeedsChan chan []byte, hpfeedsEnabled bool) *PostgresServer {
listener, err := net.Listen("tcp", addr+":"+port)
if err != nil {
log.Errorf("Error listening: %s", err)
Expand All @@ -51,6 +47,7 @@ func NewPostgresServer(port string, addr string, users []string, cleartext bool,
addr: addr,
port: port,
cleartext: cleartext,
tcpTimeout: tcpTimeout,
pgUsers: pgUsers,
}
}
Expand All @@ -63,17 +60,16 @@ func (p *PostgresServer) Close() {
func (p *PostgresServer) Listen() {
log.Infof("Starting to listening on %s:%s...", p.addr, p.port)
for {
//FIXME: conn is an example of primitive obsession
conn, err := p.listener.Accept()
if err != nil {
log.Warn("Error accepting: %s", err)
continue
}

conn.SetDeadline(time.Now().Add(tcpTimeout))
pgConn := NewPostgresConnection(conn, p.tcpTimeout)

p.waitGroup.Add(1)
go p.handleRequest(conn)
go p.handleRequest(pgConn)
}
}

Expand Down Expand Up @@ -104,10 +100,8 @@ func (p *PostgresServer) sendToHpFeeds(pgConn *PostgresConnection) error {
return err
}

func (p *PostgresServer) handleRequest(conn net.Conn) {
func (p *PostgresServer) handleRequest(pgConn *PostgresConnection) {
defer p.waitGroup.Done()

pgConn := NewPostgresConnection(conn)
defer pgConn.Close()

for {
Expand Down

0 comments on commit 62cc459

Please sign in to comment.