Skip to content

Commit

Permalink
split router package into smaller packages (wip)
Browse files Browse the repository at this point in the history
casualties:
- no longer logging message bus stuff (but we don't want that forever)
- no longer using gocheck output in travis, as gocheck doesn't deal with
  sub-packages very well (only runs top-level and behaves weirdly)

new packages:
- registry
- route
- config
- log
  • Loading branch information
Alex Suraci and Mark Rushakoff committed Aug 16, 2013
1 parent 897a15f commit daa3e7c
Show file tree
Hide file tree
Showing 30 changed files with 452 additions and 357 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ install:
- go get -v ./...
- go build -v ./...

script: go test -gocheck.v -v ./...
script: go test -v ./...
4 changes: 3 additions & 1 deletion access_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"net/http"
"os"
"time"

"github.com/cloudfoundry/gorouter/route"
)

type AccessLogRecord struct {
Request *http.Request
Response *http.Response
RouteEndpoint *RouteEndpoint
RouteEndpoint *route.Endpoint
StartedAt time.Time
FirstByteAt time.Time
FinishedAt time.Time
Expand Down
4 changes: 3 additions & 1 deletion access_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/url"
"regexp"
"time"

"github.com/cloudfoundry/gorouter/route"
)

type AccessLoggerSuite struct{}
Expand Down Expand Up @@ -35,7 +37,7 @@ func (s *AccessLoggerSuite) CreateAccessLogRecord() *AccessLogRecord {
StatusCode: http.StatusOK,
}

b := &RouteEndpoint{
b := &route.Endpoint{
ApplicationId: "my_awesome_id",
Host: "127.0.0.1",
Port: 4567,
Expand Down
2 changes: 1 addition & 1 deletion config.go → config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package router
package config

import (
vcap "github.com/cloudfoundry/gorouter/common"
Expand Down
2 changes: 1 addition & 1 deletion config_test.go → config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package router
package config

import (
. "launchpad.net/gocheck"
Expand Down
8 changes: 8 additions & 0 deletions config/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package config

import (
. "launchpad.net/gocheck"
"testing"
)

func Test(t *testing.T) { TestingT(t) }
File renamed without changes.
20 changes: 12 additions & 8 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package router
import (
"errors"
"fmt"
steno "github.com/cloudfoundry/gosteno"
. "launchpad.net/gocheck"
"net"
"os/exec"
"strconv"
"testing"
"time"

steno "github.com/cloudfoundry/gosteno"
. "launchpad.net/gocheck"

"github.com/cloudfoundry/gorouter/config"
)

func Test(t *testing.T) {
Expand All @@ -21,13 +24,14 @@ func Test(t *testing.T) {

steno.Init(config)

log = steno.NewLogger("test")
// log = steno.NewLogger("test")

TestingT(t)
}

func SpecConfig(natsPort, statusPort, proxyPort uint16) *Config {
c := DefaultConfig()
func SpecConfig(natsPort, statusPort, proxyPort uint16) *config.Config {
c := config.DefaultConfig()

c.Port = proxyPort
c.Index = 2
c.TraceKey = "my_trace_key"
Expand All @@ -41,20 +45,20 @@ func SpecConfig(natsPort, statusPort, proxyPort uint16) *Config {
c.DropletStaleThreshold = 0
c.PublishActiveAppsInterval = 0

c.Status = StatusConfig{
c.Status = config.StatusConfig{
Port: statusPort,
User: "user",
Pass: "pass",
}

c.Nats = NatsConfig{
c.Nats = config.NatsConfig{
Host: "localhost",
Port: natsPort,
User: "nats",
Pass: "nats",
}

c.Logging = LoggingConfig{
c.Logging = config.LoggingConfig{
File: "/dev/stderr",
Level: "info",
}
Expand Down
23 changes: 14 additions & 9 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"time"

mbus "github.com/cloudfoundry/go_cfmessagebus"
"github.com/cloudfoundry/gorouter/test"
. "launchpad.net/gocheck"

"github.com/cloudfoundry/gorouter/config"
"github.com/cloudfoundry/gorouter/log"
"github.com/cloudfoundry/gorouter/test"
)

type IntegrationSuite struct {
Config *Config
Config *config.Config
mbusClient mbus.MessageBus
router *Router

Expand All @@ -37,23 +40,25 @@ func (s *IntegrationSuite) TestNatsConnectivity(c *C) {
statusPort := nextAvailPort()

s.Config = SpecConfig(s.natsPort, statusPort, proxyPort)

// ensure the threshold is longer than the interval that we check,
// because we set the route's timestamp to time.Now() on the interval
// as part of pausing
s.Config.PruneStaleDropletsInterval = 1 * time.Second
s.Config.DropletStaleThreshold = 2 * s.Config.PruneStaleDropletsInterval

SetupLoggerFromConfig(s.Config)
log.SetupLoggerFromConfig(s.Config)

s.router = NewRouter(s.Config)

s.router.Run()

s.mbusClient = s.router.mbusClient

// ensure the threshold is longer than the interval that we check,
// because we set the route's timestamp to time.Now() on the interval
// as part of pausing
staleCheckInterval := s.router.registry.pruneStaleDropletsInterval
staleThreshold := 2 * staleCheckInterval
staleCheckInterval := s.Config.PruneStaleDropletsInterval
staleThreshold := s.Config.DropletStaleThreshold

s.router.registry.dropletStaleThreshold = staleThreshold
s.Config.DropletStaleThreshold = staleThreshold

zombieApp := test.NewGreetApp([]string{"zombie.vcap.me"}, proxyPort, s.mbusClient, nil)
zombieApp.Listen()
Expand Down
8 changes: 8 additions & 0 deletions log/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package log

import (
. "launchpad.net/gocheck"
"testing"
)

func Test(t *testing.T) { TestingT(t) }
70 changes: 70 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package log

import (
"github.com/cloudfoundry/gorouter/common"
"github.com/cloudfoundry/gorouter/config"
steno "github.com/cloudfoundry/gosteno"
"os"
)

var logger *steno.Logger

var Counter = common.NewLogCounter()

func init() {
stenoConfig := &steno.Config{
Sinks: []steno.Sink{steno.NewIOSink(os.Stderr)},
Codec: steno.NewJsonCodec(),
Level: steno.LOG_ALL,
}

steno.Init(stenoConfig)
logger = steno.NewLogger("router.init")
}

func SetupLoggerFromConfig(c *config.Config) {
l, err := steno.GetLogLevel(c.Logging.Level)
if err != nil {
panic(err)
}

s := make([]steno.Sink, 0)
if c.Logging.File != "" {
s = append(s, steno.NewFileSink(c.Logging.File))
} else {
s = append(s, steno.NewIOSink(os.Stdout))
}

if c.Logging.Syslog != "" {
s = append(s, steno.NewSyslogSink(c.Logging.Syslog))
}

s = append(s, Counter)

stenoConfig := &steno.Config{
Sinks: s,
Codec: steno.NewJsonCodec(),
Level: l,
}

steno.Init(stenoConfig)
logger = steno.NewLogger("router.global")
}

func Fatal(msg string) { logger.Fatal(msg) }
func Error(msg string) { logger.Error(msg) }
func Warn(msg string) { logger.Warn(msg) }
func Info(msg string) { logger.Info(msg) }
func Debug(msg string) { logger.Debug(msg) }

func Fatald(data map[string]interface{}, msg string) { logger.Fatald(data, msg) }
func Errord(data map[string]interface{}, msg string) { logger.Errord(data, msg) }
func Warnd(data map[string]interface{}, msg string) { logger.Warnd(data, msg) }
func Infod(data map[string]interface{}, msg string) { logger.Infod(data, msg) }
func Debugd(data map[string]interface{}, msg string) { logger.Debugd(data, msg) }

func Fatalf(msg string, vals ...interface{}) { logger.Fatalf(msg, vals...) }
func Errorf(msg string, vals ...interface{}) { logger.Errorf(msg, vals...) }
func Warnf(msg string, vals ...interface{}) { logger.Warnf(msg, vals...) }
func Infof(msg string, vals ...interface{}) { logger.Infof(msg, vals...) }
func Debugf(msg string, vals ...interface{}) { logger.Debugf(msg, vals...) }
10 changes: 6 additions & 4 deletions logger_test.go → log/logger_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package router
package log

import (
steno "github.com/cloudfoundry/gosteno"
. "launchpad.net/gocheck"

"github.com/cloudfoundry/gorouter/config"
)

type LoggerSuite struct{}

var _ = Suite(&LoggerSuite{})

func (s *LoggerSuite) TestSetupLoggerFromConfig(c *C) {
cfg := DefaultConfig()
cfg := config.DefaultConfig()
cfg.Logging.File = "/tmp/gorouter.log"

SetupLoggerFromConfig(cfg)

count := logCounter.GetCount("info")
count := Counter.GetCount("info")
logger := steno.NewLogger("test")
logger.Info("Hello")
c.Assert(logCounter.GetCount("info"), Equals, count+1)
c.Assert(Counter.GetCount("info"), Equals, count+1)
}
50 changes: 0 additions & 50 deletions logger.go

This file was deleted.

12 changes: 8 additions & 4 deletions perf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"github.com/cloudfoundry/go_cfmessagebus/mock_cfmessagebus"
"strconv"
"testing"

"github.com/cloudfoundry/gorouter/config"
"github.com/cloudfoundry/gorouter/registry"
"github.com/cloudfoundry/gorouter/route"
)

const (
Expand All @@ -12,17 +16,17 @@ const (
)

func BenchmarkRegister(b *testing.B) {
c := DefaultConfig()
c := config.DefaultConfig()
mbus := mock_cfmessagebus.NewMockMessageBus()
r := NewRegistry(c, mbus)
r := registry.NewRegistry(c, mbus)
p := NewProxy(c, r, NewVarz(r))

for i := 0; i < b.N; i++ {
str := strconv.Itoa(i)
p.Register(&RouteEndpoint{
p.Register(&route.Endpoint{
Host: "localhost",
Port: uint16(i),
Uris: []Uri{Uri("bench.vcap.me." + str)},
Uris: []route.Uri{route.Uri("bench.vcap.me." + str)},
})
}
}
Loading

0 comments on commit daa3e7c

Please sign in to comment.