code cleanup, grouping declarations

This commit is contained in:
Philipp Mieden 2019-05-21 11:17:03 +02:00
parent f20402ef5f
commit 0735b5ad18
8 changed files with 48 additions and 30 deletions

View File

@ -16,6 +16,11 @@ import (
const pathContentserver = "/contentserver" const pathContentserver = "/contentserver"
var (
testServerSocketAddr string
testServerWebserverAddr string
)
func dump(t *testing.T, v interface{}) { func dump(t *testing.T, v interface{}) {
jsonBytes, err := json.MarshalIndent(v, "", " ") jsonBytes, err := json.MarshalIndent(v, "", " ")
if err != nil { if err != nil {
@ -42,11 +47,6 @@ func getAvailableAddr() string {
return "127.0.0.1:" + strconv.Itoa(getFreePort()) return "127.0.0.1:" + strconv.Itoa(getFreePort())
} }
var (
testServerSocketAddr string
testServerWebserverAddr string
)
func initTestServer(t testing.TB) (socketAddr, webserverAddr string) { func initTestServer(t testing.TB) (socketAddr, webserverAddr string) {
socketAddr = getAvailableAddr() socketAddr = getAvailableAddr()
webserverAddr = getAvailableAddr() webserverAddr = getAvailableAddr()

View File

@ -34,8 +34,11 @@ func (c *connectionPool) run(connectionPoolSize int, waitTimeout time.Duration)
entryTime time.Time entryTime time.Time
chanConn chan net.Conn chanConn chan net.Conn
} }
connectionPool := make(map[int]*poolEntry, connectionPoolSize)
waitPool := map[int]*waitPoolEntry{} var (
connectionPool = make(map[int]*poolEntry, connectionPoolSize)
waitPool = map[int]*waitPoolEntry{}
)
for i := 0; i < connectionPoolSize; i++ { for i := 0; i < connectionPoolSize; i++ {
connectionPool[i] = &poolEntry{ connectionPool[i] = &poolEntry{
conn: nil, conn: nil,
@ -110,8 +113,10 @@ RunLoop:
} }
} }
// waitpool cleanup // waitpool cleanup
waitPoolLoosers := []int{} var (
now := time.Now() waitPoolLoosers = []int{}
now = time.Now()
)
for i, waitPoolEntry := range waitPool { for i, waitPoolEntry := range waitPool {
if now.Sub(waitPoolEntry.entryTime) > waitTimeout { if now.Sub(waitPoolEntry.entryTime) > waitTimeout {
waitPoolLoosers = append(waitPoolLoosers, i) waitPoolLoosers = append(waitPoolLoosers, i)

View File

@ -64,8 +64,10 @@ func (c *socketTransport) call(handler server.Handler, request interface{}, resp
jsonBytes = append([]byte(fmt.Sprintf("%s:%d", handler, len(jsonBytes))), jsonBytes...) jsonBytes = append([]byte(fmt.Sprintf("%s:%d", handler, len(jsonBytes))), jsonBytes...)
// send request // send request
written := 0 var (
l := len(jsonBytes) written = 0
l = len(jsonBytes)
)
for written < l { for written < l {
n, err := conn.Write(jsonBytes[written:]) n, err := conn.Write(jsonBytes[written:])
if err != nil { if err != nil {
@ -76,9 +78,11 @@ func (c *socketTransport) call(handler server.Handler, request interface{}, resp
} }
// read response // read response
responseBytes := []byte{} var (
buf := make([]byte, 4096) responseBytes = []byte{}
responseLength := 0 buf = make([]byte, 4096)
responseLength = 0
)
for { for {
n, err := conn.Read(buf) n, err := conn.Read(buf)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {

View File

@ -4,6 +4,6 @@ package content
const ( const (
// Indent for json indentation // Indent for json indentation
Indent string = "\t" Indent string = "\t"
// PathSeparator seprator for paths in URIs // PathSeparator separator for paths in URIs
PathSeparator = "/" PathSeparator = "/"
) )

View File

@ -52,15 +52,21 @@ func (node *RepoNode) InPath(path []*Item) bool {
// GetPath get a path for a repo node // GetPath get a path for a repo node
func (node *RepoNode) GetPath() []*Item { func (node *RepoNode) GetPath() []*Item {
parentNode := node.parent
pathLength := 0 var (
parentNode = node.parent
pathLength = 0
)
for parentNode != nil { for parentNode != nil {
parentNode = parentNode.parent parentNode = parentNode.parent
pathLength++ pathLength++
} }
parentNode = node.parent parentNode = node.parent
i := 0
path := make([]*Item, pathLength) var (
i = 0
path = make([]*Item, pathLength)
)
for parentNode != nil { for parentNode != nil {
path[i] = parentNode.ToItem([]string{}) path[i] = parentNode.ToItem([]string{})
parentNode = parentNode.parent parentNode = parentNode.parent

View File

@ -3,11 +3,12 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/foomo/contentserver/metrics"
"github.com/foomo/contentserver/status"
"os" "os"
"strings" "strings"
"github.com/foomo/contentserver/metrics"
"github.com/foomo/contentserver/status"
"github.com/foomo/contentserver/log" "github.com/foomo/contentserver/log"
"github.com/foomo/contentserver/server" "github.com/foomo/contentserver/server"
) )
@ -18,11 +19,8 @@ const (
logLevelWarning = "warning" logLevelWarning = "warning"
logLevelRecord = "record" logLevelRecord = "record"
logLevelError = "error" logLevelError = "error"
)
const (
ServiceName = "Content Server"
ServiceName = "Content Server"
DefaultHealthzHandlerAddress = ":8080" DefaultHealthzHandlerAddress = ":8080"
DefaultPrometheusListener = ":9200" DefaultPrometheusListener = ":9200"
) )

View File

@ -70,9 +70,12 @@ func (s *socketServer) writeResponse(conn net.Conn, reply []byte) {
func (s *socketServer) handleConnection(conn net.Conn) { func (s *socketServer) handleConnection(conn net.Conn) {
log.Debug("socketServer.handleConnection") log.Debug("socketServer.handleConnection")
var headerBuffer [1]byte
header := "" var (
i := 0 headerBuffer [1]byte
header = ""
i = 0
)
for { for {
i++ i++
// fmt.Println("---->", i) // fmt.Println("---->", i)

View File

@ -4,8 +4,10 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
const MetricLabelHandler = "handler" const (
const MetricLabelStatus = "status" MetricLabelHandler = "handler"
MetricLabelStatus = "status"
)
type Metrics struct { type Metrics struct {
ServiceRequestCounter *prometheus.CounterVec // count the number of requests for each service function ServiceRequestCounter *prometheus.CounterVec // count the number of requests for each service function