Skip to content

Commit

Permalink
make linter happy
Browse files Browse the repository at this point in the history
  • Loading branch information
muraty committed Oct 6, 2020
1 parent 66f451d commit 97450d8
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 20 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module kimo

go 1.14
go 1.15

require (
github.com/BurntSushi/toml v0.3.1
github.com/cenkalti/log v1.0.0
github.com/go-sql-driver/mysql v1.5.0
github.com/shirou/gopsutil v2.20.8+incompatible
github.com/shirou/gopsutil v2.20.9+incompatible
github.com/urfave/cli v1.22.4
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 // indirect
)
7 changes: 3 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shirou/gopsutil v2.20.8+incompatible h1:8c7Atn0FAUZJo+f4wYbN0iVpdWniCQk7IYwGtgdh1mY=
github.com/shirou/gopsutil v2.20.8+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/gopsutil v2.20.9+incompatible h1:msXs2frUV+O/JLva9EDLpuJ84PrFsdCTCQex8PUdtkQ=
github.com/shirou/gopsutil v2.20.9+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/urfave/cli v1.22.4 h1:u7tSpNPPswAFymm8IehJhy4uJMlUuU/GmqSkvJ1InXA=
github.com/urfave/cli v1.22.4/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 h1:W0lCpv29Hv0UaM1LXb9QlBHLNP8UFfcKjblhVCWftOM=
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
4 changes: 3 additions & 1 deletion server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import (
"time"
)

func NewHttpClient(connectTimeout, readTimeout time.Duration) *http.Client {
// NewHTTPClient returns a http client with custom connect & read timeout
func NewHTTPClient(connectTimeout, readTimeout time.Duration) *http.Client {
return &http.Client{
Transport: &http.Transport{
Dial: TimeoutDialer(connectTimeout, readTimeout),
},
}
}

// TimeoutDialer is used to set connect & read timeouts for the client
func TimeoutDialer(connectTimeout, readTimeout time.Duration) func(net, addr string) (c net.Conn, err error) {
return func(netw, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(netw, addr, connectTimeout)
Expand Down
4 changes: 3 additions & 1 deletion server/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ type MysqlProcess struct {
Address types.IPPort `json:"address"`
}

// todo: DRY. too much duplicated codes inside New.. functions
// NewMysql is used to create a Mysql type.
func NewMysql(dsn string) *Mysql {
m := new(Mysql)
m.DSN = dsn
return m
}

// Mysql is used to get processes from mysql.
type Mysql struct {
DSN string
Processes []MysqlProcess
}

// FetchProcesses is used to fetch processlist table from information_schema.
func (m *Mysql) FetchProcesses(ctx context.Context, procsC chan<- []*MysqlProcess, errC chan<- error) {
log.Infoln("Requesting to mysql...")
db, err := sql.Open("mysql", m.DSN)
Expand Down
5 changes: 4 additions & 1 deletion server/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/cenkalti/log"
)

// KimoProcess is combined with processes from mysql to daemon through tcpproxy
type KimoProcess struct {
DaemonProcess *types.DaemonProcess
MysqlProcess *MysqlProcess
Expand All @@ -20,9 +21,10 @@ type KimoProcess struct {
Server *Server
}

// FetchDaemonProcess is used to fetch process information a daemon
func (kp *KimoProcess) FetchDaemonProcess(ctx context.Context, host string, port uint32) (*types.DaemonProcess, error) {
// todo: use request with context
var httpClient = NewHttpClient(kp.Server.Config.DaemonConnectTimeout*time.Second, kp.Server.Config.DaemonReadTimeout*time.Second)
var httpClient = NewHTTPClient(kp.Server.Config.DaemonConnectTimeout*time.Second, kp.Server.Config.DaemonReadTimeout*time.Second)
url := fmt.Sprintf("http://%s:%d/proc?port=%d", host, kp.KimoRequest.Server.Config.DaemonPort, port)
log.Debugf("Requesting to %s\n", url)
response, err := httpClient.Get(url)
Expand All @@ -47,6 +49,7 @@ func (kp *KimoProcess) FetchDaemonProcess(ctx context.Context, host string, port
return &dp, nil
}

// SetDaemonProcess is used to set daemon process of a KimoProcess
func (kp *KimoProcess) SetDaemonProcess(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
var host string
Expand Down
18 changes: 12 additions & 6 deletions server/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (

// KimoServerResponse is type for returning a response from kimo server
type KimoServerResponse struct {
ServerProcesses []ServerProcess `json:"processes"`
Processes []Process `json:"processes"`
}

// ServerProcess is the final processes that is combined from DaemonProcess + TCPProxyRecord + MysqlProcess
type ServerProcess struct {
// Process is the final processes that is combined with DaemonProcess + TCPProxyRecord + MysqlProcess
type Process struct {
ID int32 `json:"id"`
MysqlUser string `json:"mysql_user"`
DB string `json:"db"`
Expand All @@ -31,6 +31,7 @@ type ServerProcess struct {
Host string `json:"host"`
}

// KimoRequest is used to represent a new request to the server
type KimoRequest struct {
Mysql *Mysql
Server *Server
Expand All @@ -39,6 +40,7 @@ type KimoRequest struct {
KimoProcesses []*KimoProcess
}

// NewKimoRequest is used to create a new KimoRequest
func (s *Server) NewKimoRequest() *KimoRequest {
kr := new(KimoRequest)
kr.Mysql = NewMysql(s.Config.DSN)
Expand All @@ -49,6 +51,7 @@ func (s *Server) NewKimoRequest() *KimoRequest {
return kr
}

// InitializeKimoProcesses initialize kimo processes combined with tcpproxy records and mysql processes
func (kr *KimoRequest) InitializeKimoProcesses(mps []*MysqlProcess, tprs []*TCPProxyRecord) error {
for _, mp := range mps {
tpr := kr.findTCPProxyRecord(mp.Address, tprs)
Expand Down Expand Up @@ -92,6 +95,7 @@ func (kr *KimoRequest) findTCPProxyRecord(addr types.IPPort, proxyRecords []*TCP
return nil
}

// Setup is used for setting up kimo processes with fetching information from mysql and tcpproxy
func (kr *KimoRequest) Setup(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
Expand Down Expand Up @@ -126,6 +130,7 @@ func (kr *KimoRequest) Setup(ctx context.Context) error {

}

// GenerateKimoProcesses is used to combine all information (mysql + tcpproxy + daemon) of a process
func (kr *KimoRequest) GenerateKimoProcesses(ctx context.Context) {
log.Infof("Generating %d kimo processes...\n", len(kr.KimoProcesses))
var wg sync.WaitGroup
Expand All @@ -136,9 +141,10 @@ func (kr *KimoRequest) GenerateKimoProcesses(ctx context.Context) {
wg.Wait()
}

// ReturnResponse is used to return a response from server
func (kr *KimoRequest) ReturnResponse(ctx context.Context, w http.ResponseWriter) {
log.Infof("Returning response with %d kimo processes...\n", len(kr.KimoProcesses))
serverProcesses := make([]ServerProcess, 0)
processes := make([]Process, 0)
for _, kp := range kr.KimoProcesses {

ut, err := strconv.ParseUint(kp.MysqlProcess.Time, 10, 32)
Expand All @@ -147,7 +153,7 @@ func (kr *KimoRequest) ReturnResponse(ctx context.Context, w http.ResponseWriter
}
t := uint32(ut)

serverProcesses = append(serverProcesses, ServerProcess{
processes = append(processes, Process{
ID: kp.MysqlProcess.ID,
MysqlUser: kp.MysqlProcess.User,
DB: kp.MysqlProcess.DB.String,
Expand All @@ -164,7 +170,7 @@ func (kr *KimoRequest) ReturnResponse(ctx context.Context, w http.ResponseWriter
w.Header().Set("Content-Type", "application/json")

response := &KimoServerResponse{
ServerProcesses: serverProcesses,
Processes: processes,
}
json.NewEncoder(w).Encode(response)
}
6 changes: 5 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ import (
"time"

"github.com/cenkalti/log"
_ "github.com/go-sql-driver/mysql"
)

// NewServer is used to create a new Server type
func NewServer(cfg *config.Config) *Server {
s := new(Server)
s.Config = &cfg.Server
log.Infoln("Creating a new server...")
return s
}

// Server is a type for handling server side
type Server struct {
Config *config.Server
}

// Processes is a handler for returning process list
func (s *Server) Processes(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "access-control-allow-origin, access-control-allow-headers")
Expand All @@ -41,11 +43,13 @@ func (s *Server) Processes(w http.ResponseWriter, req *http.Request) {

}

// Health is a dummy endpoint for load balancer health check
func (s *Server) Health(w http.ResponseWriter, req *http.Request) {
// todo: real health check
fmt.Fprintf(w, "OK")
}

// Run is used to run http handlers
func (s *Server) Run() error {
log.Infof("Running server on %s \n", s.Config.ListenAddress)
http.HandleFunc("/procs", s.Processes)
Expand Down
10 changes: 7 additions & 3 deletions server/tcpproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,30 @@ type TCPProxyRecord struct {
ServerIn types.IPPort `json:"server_in"`
}

// TCPConns is a type for TCP Proxy management api response
type TCPConns struct {
Records []*TCPProxyRecord `json:"conns"`
}

// TCPProxy is used for getting info from tcp proxy
type TCPProxy struct {
MgmtAddress string
HttpClient *http.Client
HTTPClient *http.Client
}

// NewTCPProxy is used to create a new TCPProxy
func NewTCPProxy(mgmtAddress string, connectTimeout, readTimeout time.Duration) *TCPProxy {
t := new(TCPProxy)
t.MgmtAddress = mgmtAddress
t.HttpClient = NewHttpClient(connectTimeout*time.Second, readTimeout*time.Second)
t.HTTPClient = NewHTTPClient(connectTimeout*time.Second, readTimeout*time.Second)
return t
}

// FetchRecords is used to fetch connection records from tcp proxy.
func (t *TCPProxy) FetchRecords(ctx context.Context, recordsC chan<- []*TCPProxyRecord, errC chan<- error) {
url := fmt.Sprintf("http://%s/conns?json=true", t.MgmtAddress)
log.Infof("Requesting to tcpproxy %s\n", url)
response, err := t.HttpClient.Get(url)
response, err := t.HTTPClient.Get(url)
if err != nil {
errC <- err
return
Expand Down

0 comments on commit 97450d8

Please sign in to comment.