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"
var (
testServerSocketAddr string
testServerWebserverAddr string
)
func dump(t *testing.T, v interface{}) {
jsonBytes, err := json.MarshalIndent(v, "", " ")
if err != nil {
@ -42,11 +47,6 @@ func getAvailableAddr() string {
return "127.0.0.1:" + strconv.Itoa(getFreePort())
}
var (
testServerSocketAddr string
testServerWebserverAddr string
)
func initTestServer(t testing.TB) (socketAddr, webserverAddr string) {
socketAddr = getAvailableAddr()
webserverAddr = getAvailableAddr()

View File

@ -34,8 +34,11 @@ func (c *connectionPool) run(connectionPoolSize int, waitTimeout time.Duration)
entryTime time.Time
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++ {
connectionPool[i] = &poolEntry{
conn: nil,
@ -110,8 +113,10 @@ RunLoop:
}
}
// waitpool cleanup
waitPoolLoosers := []int{}
now := time.Now()
var (
waitPoolLoosers = []int{}
now = time.Now()
)
for i, waitPoolEntry := range waitPool {
if now.Sub(waitPoolEntry.entryTime) > waitTimeout {
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...)
// send request
written := 0
l := len(jsonBytes)
var (
written = 0
l = len(jsonBytes)
)
for written < l {
n, err := conn.Write(jsonBytes[written:])
if err != nil {
@ -76,9 +78,11 @@ func (c *socketTransport) call(handler server.Handler, request interface{}, resp
}
// read response
responseBytes := []byte{}
buf := make([]byte, 4096)
responseLength := 0
var (
responseBytes = []byte{}
buf = make([]byte, 4096)
responseLength = 0
)
for {
n, err := conn.Read(buf)
if err != nil && err != io.EOF {

View File

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

View File

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

View File

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

View File

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

View File

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