From cc64c146a35d78b6733dace539c104d7c42c8d8b Mon Sep 17 00:00:00 2001
From: Garet Halliday <ghalliday@gfxlabs.io>
Date: Tue, 13 Sep 2022 17:34:25 -0500
Subject: [PATCH] i think this is every interface needed for stats, need to
 implement

---
 lib/gat/gatling/client/client.go           | 39 ++++++++++++++++
 lib/gat/gatling/conn_pool/conn_pool.go     |  4 +-
 lib/gat/gatling/conn_pool/server/server.go | 54 +++++++++++++++++++++-
 lib/gat/interfaces.go                      | 23 +++++++++
 4 files changed, 117 insertions(+), 3 deletions(-)

diff --git a/lib/gat/gatling/client/client.go b/lib/gat/gatling/client/client.go
index 33a258e3..307f238b 100644
--- a/lib/gat/gatling/client/client.go
+++ b/lib/gat/gatling/client/client.go
@@ -23,6 +23,7 @@ import (
 	"reflect"
 	"strings"
 	"sync"
+	"time"
 )
 
 // / client state, one per client
@@ -47,6 +48,8 @@ type Client struct {
 	stats      any // TODO: Reporter
 	admin      bool
 
+	connectTime time.Time
+
 	server gat.ConnectionPool
 
 	last_addr_id int
@@ -67,6 +70,42 @@ type Client struct {
 	mu sync.Mutex
 }
 
+func (c *Client) State() string {
+	return "TODO" // TODO
+}
+
+func (c *Client) Addr() string {
+	return c.conn.RemoteAddr().String()
+}
+
+func (c *Client) Port() int {
+	return 0 // TODO
+}
+
+func (c *Client) LocalAddr() string {
+	return c.conn.LocalAddr().String()
+}
+
+func (c *Client) LocalPort() int {
+	return 0 // TODO
+}
+
+func (c *Client) ConnectTime() time.Time {
+	return c.connectTime
+}
+
+func (c *Client) RequestTime() time.Time {
+	return c.currentConn.RequestTime()
+}
+
+func (c *Client) Wait() time.Duration {
+	return c.currentConn.Wait()
+}
+
+func (c *Client) RemotePid() int {
+	return int(c.pid)
+}
+
 func NewClient(
 	gatling gat.Gat,
 	conf *config.Global,
diff --git a/lib/gat/gatling/conn_pool/conn_pool.go b/lib/gat/gatling/conn_pool/conn_pool.go
index d2dfea51..7ff05b27 100644
--- a/lib/gat/gatling/conn_pool/conn_pool.go
+++ b/lib/gat/gatling/conn_pool/conn_pool.go
@@ -2,7 +2,6 @@ package conn_pool
 
 import (
 	"context"
-	"fmt"
 	"log"
 	"math/rand"
 	"reflect"
@@ -147,7 +146,8 @@ func (c *ConnectionPool) chooseConnections() *connections {
 	for _, srvConf := range s.conf.Servers {
 		srv, err := server.Dial(
 			context.Background(),
-			fmt.Sprintf("%s:%d", srvConf.Host, srvConf.Port),
+			srvConf.Host,
+			srvConf.Port,
 			c.user, s.conf.Database,
 			srvConf.Username, srvConf.Password,
 			nil)
diff --git a/lib/gat/gatling/conn_pool/server/server.go b/lib/gat/gatling/conn_pool/server/server.go
index 5d09a66e..eb162093 100644
--- a/lib/gat/gatling/conn_pool/server/server.go
+++ b/lib/gat/gatling/conn_pool/server/server.go
@@ -24,6 +24,7 @@ import (
 
 type Server struct {
 	addr   string
+	port   uint16
 	remote net.Addr
 	conn   net.Conn
 	r      *bufio.Reader
@@ -35,6 +36,7 @@ type Server struct {
 	secret_key int32
 
 	connected_at     time.Time
+	request_time     time.Time
 	stats            any // TODO: stats
 	application_name string
 
@@ -53,12 +55,14 @@ type Server struct {
 
 func Dial(ctx context.Context,
 	addr string,
+	port uint16,
 	user *config.User,
 	db string, dbuser string, dbpass string,
 	stats any,
 ) (*Server, error) {
 	s := &Server{
 		addr: addr,
+		port: port,
 
 		bound_prepared_statments: make(map[string]*protocol.Parse),
 		bound_portals:            make(map[string]*protocol.Bind),
@@ -67,7 +71,7 @@ func Dial(ctx context.Context,
 		dbpass: dbpass,
 	}
 	var err error
-	s.conn, err = net.Dial("tcp", addr)
+	s.conn, err = net.Dial("tcp", fmt.Sprintf("%s:%d", addr, port))
 	if err != nil {
 		return nil, err
 	}
@@ -103,6 +107,54 @@ func (s *Server) GetDatabase() string {
 	return s.db
 }
 
+func (s *Server) State() string {
+	return "TODO" // TODO
+}
+
+func (s *Server) Address() string {
+	return s.addr
+}
+
+func (s *Server) Port() int {
+	return int(s.port)
+}
+
+func (s *Server) LocalAddr() string {
+	return s.conn.LocalAddr().String()
+}
+
+func (s *Server) LocalPort() int {
+	return 0
+}
+
+func (s *Server) ConnectTime() time.Time {
+	return s.connected_at
+}
+
+func (s *Server) RequestTime() time.Time {
+	return s.request_time
+}
+
+func (s *Server) Wait() time.Duration {
+	return time.Now().Sub(s.request_time) // TODO this won't take into account the last requests running time
+}
+
+func (s *Server) CloseNeeded() bool {
+	return false
+}
+
+func (s *Server) Client() gat.Client {
+	return nil // TODO
+}
+
+func (s *Server) RemotePid() int {
+	return int(s.process_id)
+}
+
+func (s *Server) TLS() string {
+	return "" // TODO
+}
+
 func (s *Server) GetServerInfo() []*protocol.ParameterStatus {
 	return s.server_info
 }
diff --git a/lib/gat/interfaces.go b/lib/gat/interfaces.go
index 07f29764..827b54c6 100644
--- a/lib/gat/interfaces.go
+++ b/lib/gat/interfaces.go
@@ -4,6 +4,7 @@ import (
 	"context"
 	"gfx.cafe/gfx/pggat/lib/config"
 	"gfx.cafe/gfx/pggat/lib/gat/protocol"
+	"time"
 )
 
 type ClientID struct {
@@ -17,6 +18,16 @@ type Client interface {
 	GetCurrentConn() (Connection, error)
 	SetCurrentConn(conn Connection)
 
+	State() string
+	Addr() string
+	Port() int
+	LocalAddr() string
+	LocalPort() int
+	ConnectTime() time.Time
+	RequestTime() time.Time
+	Wait() time.Duration
+	RemotePid() int
+
 	Send(pkt protocol.Packet) error
 	Flush() error
 	Recv() <-chan protocol.Packet
@@ -101,6 +112,18 @@ type Shard interface {
 
 type Connection interface {
 	GetDatabase() string
+	State() string
+	Address() string
+	Port() int
+	LocalAddr() string
+	LocalPort() int
+	ConnectTime() time.Time
+	RequestTime() time.Time
+	Wait() time.Duration
+	CloseNeeded() bool
+	Client() Client
+	RemotePid() int
+	TLS() string
 
 	// Cancel the current running query
 	Cancel() error
-- 
GitLab