Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
Rebuild API cache from entitystore (#692)
Browse files Browse the repository at this point in the history
* API cache now works across restarts of dispatch server
* Ensure all HTTP methods are stored as uppercase

Signed-off-by: Berndt Jung <bjung@vmware.com>
  • Loading branch information
berndtj authored and kars7e committed Oct 19, 2018
1 parent cd61b73 commit 0b32b82
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
19 changes: 18 additions & 1 deletion pkg/api-manager/gateway/local/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (

log "github.com/sirupsen/logrus"

apimanager "github.com/vmware/dispatch/pkg/api-manager"
"github.com/vmware/dispatch/pkg/api-manager/gateway"
"github.com/vmware/dispatch/pkg/client"
entitystore "github.com/vmware/dispatch/pkg/entity-store"
"github.com/vmware/dispatch/pkg/errors"
"github.com/vmware/dispatch/pkg/http"
"github.com/vmware/dispatch/pkg/trace"
Expand All @@ -23,6 +25,7 @@ import (
type Gateway struct {
Server *http.Server

store entitystore.EntityStore
fnClient client.FunctionsClient

sync.RWMutex
Expand All @@ -33,15 +36,17 @@ type Gateway struct {
}

// NewGateway creates a new local API gateway
func NewGateway(functionsClient client.FunctionsClient) (*Gateway, error) {
func NewGateway(store entitystore.EntityStore, functionsClient client.FunctionsClient) (*Gateway, error) {
c := &Gateway{
fnClient: functionsClient,
Server: http.NewServer(nil),
store: store,
apis: make(map[string]*gateway.API),
pathLookup: make(map[string][]*gateway.API),
hostLookup: make(map[string][]*gateway.API),
methodLookup: make(map[string][]*gateway.API),
}
c.rebuildCache()
return c, nil
}

Expand Down Expand Up @@ -106,6 +111,18 @@ func (g *Gateway) DeleteAPI(ctx context.Context, api *gateway.API) error {
// rebuildCache iterates over all configured APIs and populates lookup caches. Could optimized
// to only add changes.
func (g *Gateway) rebuildCache() {
// Store should only be nil for tests
if g.store != nil {
var apis []*apimanager.API
err := g.store.ListGlobal(context.TODO(), entitystore.Options{}, &apis)
if err != nil {
log.Errorf("error syncing APIs: %v", err)
}
g.apis = make(map[string]*gateway.API)
for _, api := range apis {
g.apis[api.Name] = &api.API
}
}
g.hostLookup = make(map[string][]*gateway.API)
g.methodLookup = make(map[string][]*gateway.API)
g.pathLookup = make(map[string][]*gateway.API)
Expand Down
4 changes: 2 additions & 2 deletions pkg/api-manager/gateway/local/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestGatewayGetRequest(t *testing.T) {
fnClient.On("RunFunction", mock.Anything, mock.Anything, mock.Anything).Return(
&v1.Run{}, nil,
)
gw, err := NewGateway(fnClient)
gw, err := NewGateway(nil, fnClient)
assert.NoError(t, err)

api1 := &gateway.API{
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestGatewayPostRequest(t *testing.T) {
fnClient.On("RunFunction", mock.Anything, mock.Anything, mock.Anything).Return(
&v1.Run{}, nil,
)
gw, err := NewGateway(fnClient)
gw, err := NewGateway(nil, fnClient)
assert.NoError(t, err)

api1 := &gateway.API{
Expand Down
6 changes: 5 additions & 1 deletion pkg/api-manager/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func apiModelOntoEntity(organizationID string, m *v1.API) *API {
} else {
uris = m.Uris
}
var methods []string
for _, method := range m.Methods {
methods = append(methods, strings.ToUpper(method))
}
e := API{
BaseEntity: entitystore.BaseEntity{
Name: *m.Name,
Expand All @@ -70,7 +74,7 @@ func apiModelOntoEntity(organizationID string, m *v1.API) *API {
Enabled: m.Enabled,
TLS: m.TLS,
Hosts: m.Hosts,
Methods: m.Methods,
Methods: methods,
Protocols: m.Protocols,
URIs: uris,
CORS: m.Cors,
Expand Down
2 changes: 1 addition & 1 deletion pkg/dispatchserver/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func runLocal(config *serverConfig) {
functionsHandler, functionsShutdown := initFunctions(config, functionsDeps)
defer functionsShutdown()

gw, err := local.NewGateway(functions)
gw, err := local.NewGateway(store, functions)
if err != nil {
log.Fatalf("Error creating API Gateway: %v", err)
}
Expand Down

0 comments on commit 0b32b82

Please sign in to comment.