Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Domain overrides settings #35 #106

Merged
merged 15 commits into from
Sep 6, 2021
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ tlsfuzzer: ## tlsfuzzer
test: test-unit test-functional test-endtoend test-ws test-http2 ## test

test-unit: ## test unit
GPC_SYNC_STORING=1 go test -v -race -count=1 --tags=unit ./...
GPC_SYNC_STORING=1 TESTING=1 go test -v -race -count=1 --tags=unit ./...

test-functional: ## test functional
GPC_SYNC_STORING=1 go test -v -race -count=1 --tags=functional ./...
GPC_SYNC_STORING=1 TESTING=1 go test -v -race -count=1 --tags=functional ./...

test-endtoend: ## test endtoend
GPC_SYNC_STORING=1 go test -v -race -count=1 --tags=endtoend ./...
go test -v -race -count=1 --tags=endtoend ./...

test-ws: ## test websocket
cd test/full-setup && npm install
Expand All @@ -111,7 +111,7 @@ test-http2: ## test HTTP2
fi

cover: ## coverage
GPC_SYNC_STORING=1 go test -race -count=1 --tags=unit,functional -coverprofile c.out ./...
GPC_SYNC_STORING=1 TESTING=1 go test -race -count=1 --tags=unit,functional -coverprofile c.out ./...
go tool cover -func=c.out
go tool cover -html=c.out

Expand Down
62 changes: 31 additions & 31 deletions cache/engine/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func initLogs() {
func TestCircuitBreakerWithPingTimeout(t *testing.T) {
initLogs()

config.Config = config.Configuration{
cfg := config.Configuration{
Cache: config.Cache{
Host: utils.GetEnv("REDIS_HOST", "localhost"),
Port: "6379",
Expand All @@ -51,9 +51,9 @@ func TestCircuitBreakerWithPingTimeout(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker("testing", config.Config.CircuitBreaker)
circuit_breaker.InitCircuitBreaker("testing", cfg.CircuitBreaker)

rdb := client.Connect("testing", config.Config.Cache)
rdb := client.Connect("testing", cfg.Cache)

assert.Equal(t, "closed", circuit_breaker.CB("testing").State().String())

Expand All @@ -67,7 +67,7 @@ func TestCircuitBreakerWithPingTimeout(t *testing.T) {
assert.False(t, val)
assert.Equal(t, "half-open", circuit_breaker.CB("testing").State().String())

rdb = client.Connect("testing", config.Config.Cache)
rdb = client.Connect("testing", cfg.Cache)

val = rdb.Ping()
assert.True(t, val)
Expand All @@ -77,7 +77,7 @@ func TestCircuitBreakerWithPingTimeout(t *testing.T) {
func TestClose(t *testing.T) {
initLogs()

config.Config = config.Configuration{
cfg := config.Configuration{
Cache: config.Cache{
Host: utils.GetEnv("REDIS_HOST", "localhost"),
Port: "6379",
Expand All @@ -91,9 +91,9 @@ func TestClose(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker("testing", config.Config.CircuitBreaker)
circuit_breaker.InitCircuitBreaker("testing", cfg.CircuitBreaker)

rdb := client.Connect("testing", config.Config.Cache)
rdb := client.Connect("testing", cfg.Cache)

assert.True(t, rdb.Ping())

Expand All @@ -105,7 +105,7 @@ func TestClose(t *testing.T) {
func TestSetGet(t *testing.T) {
initLogs()

config.Config = config.Configuration{
cfg := config.Configuration{
Cache: config.Cache{
Host: utils.GetEnv("REDIS_HOST", "localhost"),
Port: "6379",
Expand All @@ -119,9 +119,9 @@ func TestSetGet(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker("testing", config.Config.CircuitBreaker)
circuit_breaker.InitCircuitBreaker("testing", cfg.CircuitBreaker)

rdb := client.Connect("testing", config.Config.Cache)
rdb := client.Connect("testing", cfg.Cache)

done, err := rdb.Set("test", "sample", 0)
assert.True(t, done)
Expand All @@ -135,7 +135,7 @@ func TestSetGet(t *testing.T) {
func TestSetGetWithExpiration(t *testing.T) {
initLogs()

config.Config = config.Configuration{
cfg := config.Configuration{
Cache: config.Cache{
Host: utils.GetEnv("REDIS_HOST", "localhost"),
Port: "6379",
Expand All @@ -149,9 +149,9 @@ func TestSetGetWithExpiration(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker("testing", config.Config.CircuitBreaker)
circuit_breaker.InitCircuitBreaker("testing", cfg.CircuitBreaker)

rdb := client.Connect("testing", config.Config.Cache)
rdb := client.Connect("testing", cfg.Cache)

done, err := rdb.Set("test", "sample", 1*time.Millisecond)
assert.True(t, done)
Expand All @@ -167,7 +167,7 @@ func TestSetGetWithExpiration(t *testing.T) {
func TestDel(t *testing.T) {
initLogs()

config.Config = config.Configuration{
cfg := config.Configuration{
Cache: config.Cache{
Host: utils.GetEnv("REDIS_HOST", "localhost"),
Port: "6379",
Expand All @@ -181,9 +181,9 @@ func TestDel(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker("testing", config.Config.CircuitBreaker)
circuit_breaker.InitCircuitBreaker("testing", cfg.CircuitBreaker)

rdb := client.Connect("testing", config.Config.Cache)
rdb := client.Connect("testing", cfg.Cache)

done, err := rdb.Set("test", "sample", 0)
assert.True(t, done)
Expand All @@ -204,7 +204,7 @@ func TestDel(t *testing.T) {
func TestExpire(t *testing.T) {
initLogs()

config.Config = config.Configuration{
cfg := config.Configuration{
Cache: config.Cache{
Host: utils.GetEnv("REDIS_HOST", "localhost"),
Port: "6379",
Expand All @@ -218,9 +218,9 @@ func TestExpire(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker("testing", config.Config.CircuitBreaker)
circuit_breaker.InitCircuitBreaker("testing", cfg.CircuitBreaker)

rdb := client.Connect("testing", config.Config.Cache)
rdb := client.Connect("testing", cfg.Cache)

done, err := rdb.Set("test", "sample", 0)
assert.True(t, done)
Expand All @@ -239,7 +239,7 @@ func TestExpire(t *testing.T) {
func TestPushList(t *testing.T) {
initLogs()

config.Config = config.Configuration{
cfg := config.Configuration{
Cache: config.Cache{
Host: utils.GetEnv("REDIS_HOST", "localhost"),
Port: "6379",
Expand All @@ -253,9 +253,9 @@ func TestPushList(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker("testing", config.Config.CircuitBreaker)
circuit_breaker.InitCircuitBreaker("testing", cfg.CircuitBreaker)

rdb := client.Connect("testing", config.Config.Cache)
rdb := client.Connect("testing", cfg.Cache)

err := rdb.Push("test", []string{"a", "b", "c"})
assert.Nil(t, err)
Expand All @@ -268,7 +268,7 @@ func TestPushList(t *testing.T) {
func TestDelWildcard(t *testing.T) {
initLogs()

config.Config = config.Configuration{
cfg := config.Configuration{
Cache: config.Cache{
Host: utils.GetEnv("REDIS_HOST", "localhost"),
Port: "6379",
Expand All @@ -282,9 +282,9 @@ func TestDelWildcard(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker("testing", config.Config.CircuitBreaker)
circuit_breaker.InitCircuitBreaker("testing", cfg.CircuitBreaker)

rdb := client.Connect("testing", config.Config.Cache)
rdb := client.Connect("testing", cfg.Cache)

done, err := rdb.Set("test_1", "sample", 0)
assert.True(t, done)
Expand Down Expand Up @@ -324,7 +324,7 @@ func TestDelWildcard(t *testing.T) {
func TestPurgeAll(t *testing.T) {
initLogs()

config.Config = config.Configuration{
cfg := config.Configuration{
Cache: config.Cache{
Host: utils.GetEnv("REDIS_HOST", "localhost"),
Port: "6379",
Expand All @@ -338,9 +338,9 @@ func TestPurgeAll(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker("testing", config.Config.CircuitBreaker)
circuit_breaker.InitCircuitBreaker("testing", cfg.CircuitBreaker)

rdb := client.Connect("testing", config.Config.Cache)
rdb := client.Connect("testing", cfg.Cache)

done, err := rdb.Set("test_1", "sample", 0)
assert.True(t, done)
Expand Down Expand Up @@ -380,7 +380,7 @@ func TestPurgeAll(t *testing.T) {
func TestEncodeDecode(t *testing.T) {
initLogs()

config.Config = config.Configuration{
cfg := config.Configuration{
Cache: config.Cache{
Host: utils.GetEnv("REDIS_HOST", "localhost"),
Port: "6379",
Expand All @@ -394,9 +394,9 @@ func TestEncodeDecode(t *testing.T) {
},
}

circuit_breaker.InitCircuitBreaker("testing", config.Config.CircuitBreaker)
circuit_breaker.InitCircuitBreaker("testing", cfg.CircuitBreaker)

rdb := client.Connect("testing", config.Config.Cache)
rdb := client.Connect("testing", cfg.Cache)

str := []byte("test string")
var decoded []byte
Expand Down
24 changes: 13 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,47 +244,49 @@ func getSliceFromMap(domains map[string]DomainSet) []DomainSet {
}

// DomainConf - Returns the configuration for the requested domain (Global Access).
func DomainConf(domain string, scheme string) *Configuration {
func DomainConf(domain string, scheme string) (Configuration, bool) {
return Config.DomainConf(domain, scheme)
}

// DomainConf - Returns the configuration for the requested domain.
func (c Configuration) DomainConf(domain string, scheme string) *Configuration {
func (c Configuration) DomainConf(domain string, scheme string) (Configuration, bool) {
var found bool

// Memoization
if c.domainsCache == nil {
c.domainsCache = make(map[string]*Configuration)
c.domainsCache = make(map[string]Configuration)
}

keyCache := fmt.Sprintf("%s%s%s", domain, utils.StringSeparatorOne, scheme)
if val, ok := c.domainsCache[keyCache]; ok {
log.Debugf("Cached configuration for %s", keyCache)
return val
return val, true
}

c.domainsCache[keyCache] = c.domainConfLookup(utils.StripPort(domain), scheme)
c.domainsCache[keyCache], found = c.domainConfLookup(utils.StripPort(domain), scheme)

return c.domainsCache[keyCache]
return c.domainsCache[keyCache], found
}

func (c Configuration) domainConfLookup(domain string, scheme string) *Configuration {
func (c Configuration) domainConfLookup(domain string, scheme string) (Configuration, bool) {
// First round: host & scheme
for _, v := range c.Domains {
if v.Server.Upstream.Host == domain && v.Server.Upstream.Scheme == scheme {
return &v
return v, true
}
}

// Second round: host
for _, v := range c.Domains {
if v.Server.Upstream.Host == domain {
return &v
return v, true
}
}

// Third round: global
if c.Server.Upstream.Host == domain {
return &c
return c, true
}

return nil
return Configuration{}, false
}
2 changes: 1 addition & 1 deletion config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Configuration struct {
CircuitBreaker circuitbreaker.CircuitBreaker `yaml:"circuit_breaker"`
Domains Domains `yaml:"domains"`
Log Log `yaml:"log"`
domainsCache map[string]*Configuration
domainsCache map[string]Configuration
}

// Domains - Overrides per domain.
Expand Down
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.14

require (
github.com/NYTimes/gziphandler v1.1.1
github.com/go-http-utils/etag v0.0.0-20161124023236-513ea8f21eb1
github.com/go-http-utils/fresh v0.0.0-20161124030543-7231e26a4b27 // indirect
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a // indirect
github.com/go-http-utils/etag v0.0.0-20161124023236-513ea8f21eb1 // indirect
github.com/go-http-utils/fresh v0.0.0-20161124030543-7231e26a4b27
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a
github.com/go-redis/redis/v8 v8.3.3
github.com/go-redsync/redsync/v4 v4.0.4
github.com/gorilla/websocket v1.4.2
Expand All @@ -16,11 +16,12 @@ require (
github.com/pkg/errors v0.9.1
github.com/sdeoras/dispatcher v1.0.2
github.com/sirupsen/logrus v1.3.0
github.com/soheilhy/cmux v0.1.5
github.com/sony/gobreaker v0.4.1
github.com/stretchr/testify v1.6.1
github.com/ugorji/go/codec v1.2.0
github.com/yhat/wsutil v0.0.0-20170731153501-1d66fa95c997
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb
gopkg.in/yaml.v2 v2.3.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ github.com/sdeoras/dispatcher v1.0.2 h1:2PIohOpkza/bzcTjEMFrKC/PmfPkNgz+gCWdUh8L
github.com/sdeoras/dispatcher v1.0.2/go.mod h1:WXv44FUh84I6S5lw9CgG14ClHMZQuPfLTtkLK1bhImE=
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=
github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0=
github.com/sony/gobreaker v0.4.1 h1:oMnRNZXX5j85zso6xCPRNPtmAycat+WcoKbklScLDgQ=
github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -111,6 +113,8 @@ golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0 h1:wBouT66WTYFXdxfVdz9sVWARVd/2vfGcmI45D2gj45M=
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb h1:eBmm0M9fYhWpKZLjQUUKka/LtIxf46G4fxeEz5KJr9U=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
2 changes: 1 addition & 1 deletion server/handler/connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestEndToEndCallConnect(t *testing.T) {
assert.Nil(t, err)

rr := httptest.NewRecorder()
h := http.HandlerFunc(handler.HandleRequest(config.Config))
h := http.HandlerFunc(handler.HandleRequest)

h.ServeHTTP(rr, req)

Expand Down
Loading