Skip to content

Commit

Permalink
feat: short flag opt, release notification, gofmt, simplify code add …
Browse files Browse the repository at this point in the history
…change route
  • Loading branch information
bubbajoe committed Jun 14, 2024
1 parent ff9ecb3 commit da7e866
Show file tree
Hide file tree
Showing 30 changed files with 126 additions and 122 deletions.
36 changes: 18 additions & 18 deletions .github/workflows/discord.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
on:
release:
types: [published]
release:
types: [published]

jobs:
github-releases-to-discord:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Github Releases To Discord
uses: SethCohen/github-releases-to-discord@v1.13.1
with:
webhook_url: ${{ secrets.DISCORD_WEBHOOK_URL }}
color: "2105893"
username: "Release Changelog"
avatar_url: "https://github.com/dgate-io.png"
content: "||@everyone||"
footer_title: "Changelog"
footer_timestamp: true
jobs:
github-releases-to-discord:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Github Releases To Discord
uses: SethCohen/github-releases-to-discord@v1.13.1
with:
webhook_url: ${{ secrets.DISCORD_WEBHOOK_URL }}
color: "2105893"
username: "Release Changelog"
avatar_url: "https://github.com/dgate-io.png"
content: "||@everyone||"
footer_title: "Changelog"
footer_timestamp: true
7 changes: 4 additions & 3 deletions cmd/dgate-cli/commands/run_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ func Run(client dgclient.DGateClient, version string) error {
}

app := &cli.App{
Name: "dgate-cli",
Usage: "a command line interface for DGate (API Gateway) Admin API",
Version: version,
Name: "dgate-cli",
Usage: "a command line interface for DGate (API Gateway) Admin API",
Version: version,
UseShortOptionHandling: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "admin",
Expand Down
2 changes: 1 addition & 1 deletion functional-tests/raft_tests/test1.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version: v1
log_level: info
log_level: debug
debug: true
tags:
- "dev"
Expand Down
1 change: 1 addition & 0 deletions internal/admin/changestate/change_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type ChangeState interface {
WaitForChanges() error
ReloadState(bool, ...*spec.ChangeLog) error
ChangeHash() uint32
ChangeLogs() []*spec.ChangeLog

// Readiness
Ready() bool
Expand Down
5 changes: 5 additions & 0 deletions internal/admin/changestate/testutil/change_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func (m *MockChangeState) WaitForChanges() error {
return m.Called().Error(0)
}

// ChangeLogs implements changestate.ChangeState.
func (m *MockChangeState) ChangeLogs() []*spec.ChangeLog {
return m.Called().Get(0).([]*spec.ChangeLog)
}

var _ changestate.ChangeState = &MockChangeState{}

func NewMockChangeState() *MockChangeState {
Expand Down
16 changes: 6 additions & 10 deletions internal/admin/routes/collection_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ func ConfigureCollectionAPI(server chi.Router, logger *zap.Logger, cs changestat
return
}

if repl := cs.Raft(); repl != nil {
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}

util.JsonResponse(w, http.StatusCreated, spec.TransformDGateCollections(
Expand Down Expand Up @@ -276,11 +274,9 @@ func ConfigureCollectionAPI(server chi.Router, logger *zap.Logger, cs changestat
return
}

if repl := cs.Raft(); repl != nil {
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}

util.JsonResponse(w, http.StatusCreated, doc)
Expand Down
9 changes: 4 additions & 5 deletions internal/admin/routes/domain_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ func ConfigureDomainAPI(server chi.Router, logger *zap.Logger, cs changestate.Ch
return
}

if repl := cs.Raft(); repl != nil {
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return

}
util.JsonResponse(w, http.StatusCreated, spec.TransformDGateDomains(
rm.GetDomainsByNamespace(domain.NamespaceName)...))
Expand Down
25 changes: 18 additions & 7 deletions internal/admin/routes/misc_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ import (

func ConfigureChangeLogAPI(server chi.Router, cs changestate.ChangeState, appConfig *config.DGateConfig) {
server.Get("/changelog/hash", func(w http.ResponseWriter, r *http.Request) {
if repl := cs.Raft(); repl != nil {
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}
// TODO: find a way to get the raft log hash
// perhaps generate based on current log commands and computed hash
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}

if b, err := json.Marshal(map[string]any{
Expand All @@ -30,6 +26,21 @@ func ConfigureChangeLogAPI(server chi.Router, cs changestate.ChangeState, appCon
w.Write([]byte(b))
}
})
server.Get("/changelog/count", func(w http.ResponseWriter, r *http.Request) {
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}

if b, err := json.Marshal(map[string]any{
"count": len(cs.ChangeLogs()),
}); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
} else {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(b))
}
})
}

func ConfigureHealthAPI(server chi.Router, version string, cs changestate.ChangeState) {
Expand Down
10 changes: 5 additions & 5 deletions internal/admin/routes/module_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ func ConfigureModuleAPI(server chi.Router, logger *zap.Logger, cs changestate.Ch
util.JsonError(w, http.StatusBadRequest, err.Error())
return
}
if repl := cs.Raft(); repl != nil {
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}

if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}

util.JsonResponse(w, http.StatusCreated, spec.TransformDGateModules(
rm.GetModulesByNamespace(mod.NamespaceName)...))
})
Expand Down
8 changes: 3 additions & 5 deletions internal/admin/routes/namespace_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ func ConfigureNamespaceAPI(server chi.Router, logger *zap.Logger, cs changestate
return
}

if repl := cs.Raft(); repl != nil {
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}

util.JsonResponse(w, http.StatusCreated, spec.TransformDGateNamespaces(rm.GetNamespaces()...))
Expand Down
8 changes: 3 additions & 5 deletions internal/admin/routes/route_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ func ConfigureRouteAPI(server chi.Router, logger *zap.Logger, cs changestate.Cha
return
}

if repl := cs.Raft(); repl != nil {
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}

util.JsonResponse(w, http.StatusCreated, spec.TransformDGateRoutes(
Expand Down
8 changes: 3 additions & 5 deletions internal/admin/routes/secret_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ func ConfigureSecretAPI(server chi.Router, logger *zap.Logger, cs changestate.Ch
util.JsonError(w, http.StatusBadRequest, err.Error())
return
}
if repl := cs.Raft(); repl != nil {
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}
secrets := rm.GetSecretsByNamespace(sec.NamespaceName)
util.JsonResponse(w, http.StatusCreated,
Expand Down
11 changes: 5 additions & 6 deletions internal/admin/routes/service_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,12 @@ func ConfigureServiceAPI(server chi.Router, logger *zap.Logger, cs changestate.C
return
}

if repl := cs.Raft(); repl != nil {
logger.Debug("Waiting for raft barrier")
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}
logger.Debug("Waiting for raft barrier")
if err := cs.WaitForChanges(); err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
}

svcs := rm.GetServicesByNamespace(svc.NamespaceName)
util.JsonResponse(w, http.StatusCreated,
spec.TransformDGateServices(svcs...))
Expand Down
2 changes: 1 addition & 1 deletion internal/config/store_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ func StoreConfig[T any, C any](config C) (T, error) {
}
err = decoder.Decode(config)
return output, err
}
}
2 changes: 1 addition & 1 deletion internal/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ func singlefyAsterisks(pattern string) string {
return singlefyAsterisks(pattern)
}
return pattern
}
}
21 changes: 9 additions & 12 deletions internal/proxy/change_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ import (
// processChangeLog - processes a change log and applies the change to the proxy state
func (ps *ProxyState) processChangeLog(cl *spec.ChangeLog, reload, store bool) (err error) {
defer func() {
ps.raftReady.Store(true)
if err != nil && !cl.Cmd.IsNoop() {
ps.ready.Store(true)
if changeHash, err := HashAny(ps.changeHash, cl); err != nil {
ps.logger.Error("error hashing change log", zap.Error(err))
return
} else {
ps.changeHash = changeHash
}
}
}()
if !cl.Cmd.IsNoop() {
if len(ps.changeLogs) > 0 {
Expand Down Expand Up @@ -101,17 +109,6 @@ func (ps *ProxyState) processChangeLog(cl *spec.ChangeLog, reload, store bool) (
ps.logger.Error("Error registering change log", zap.Error(err))
return
}
// update change log hash only when the change is successfully applied
// even if the change is a noop, we still need to update the hash
changeHash, err := HashAny(ps.changeHash, cl)
if err != nil {
if !ps.config.Debug {
return err
}
ps.logger.Error("error updating change log hash", zap.Error(err))
} else {
ps.changeHash = changeHash
}
}
}
if store {
Expand Down
2 changes: 2 additions & 0 deletions internal/proxy/dynamic_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ func (ps *ProxyState) Start() (err error) {
if !ps.replicationEnabled {
if err = ps.restoreFromChangeLogs(false); err != nil {
return err
} else {
ps.ready.Store(true)
}
}

Expand Down
34 changes: 18 additions & 16 deletions internal/proxy/proxy_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,29 @@ import (
)

type ProxyState struct {
version string
debugMode bool
startTime time.Time
config *config.DGateConfig
logger *zap.Logger
printer console.Printer
store *proxystore.ProxyStore
proxyLock *sync.RWMutex
changeHash uint32
changeLogs []*spec.ChangeLog

version string
debugMode bool
changeHash uint32
startTime time.Time
logger *zap.Logger
printer console.Printer
config *config.DGateConfig
store *proxystore.ProxyStore
changeLogs []*spec.ChangeLog
metrics *ProxyMetrics
sharedCache cache.TCache
proxyLock *sync.RWMutex

rm *resources.ResourceManager
skdr scheduler.Scheduler

providers avl.Tree[string, *RequestContextProvider]
modPrograms avl.Tree[string, *goja.Program]

raftReady atomic.Bool
ready atomic.Bool
replicationSettings *ProxyReplication
replicationEnabled bool

routers avl.Tree[string, *router.DynamicRouter]
routers avl.Tree[string, *router.DynamicRouter]

ReverseProxyBuilder reverse_proxy.Builder
ProxyTransportBuilder proxy_transport.Builder
Expand Down Expand Up @@ -106,7 +104,7 @@ func NewProxyState(logger *zap.Logger, conf *config.DGateConfig) *ProxyState {
}
state := &ProxyState{
startTime: time.Now(),
raftReady: atomic.Bool{},
ready: atomic.Bool{},
logger: logger,
debugMode: conf.Debug,
config: conf,
Expand Down Expand Up @@ -168,9 +166,13 @@ func (ps *ProxyState) ChangeHash() uint32 {
return ps.changeHash
}

func (ps *ProxyState) ChangeLogs() []*spec.ChangeLog {
return ps.changeLogs
}

func (ps *ProxyState) Ready() bool {
if ps.replicationEnabled {
return ps.raftReady.Load()
return ps.ready.Load()
}
return true
}
Expand Down
1 change: 1 addition & 0 deletions internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func NewRouterWithMux(mux *chi.Mux) *DynamicRouter {
func NewMux() *chi.Mux {
return chi.NewRouter()
}

// ReplaceRouter replaces the router
func (r *DynamicRouter) ReplaceMux(router *chi.Mux) {
r.lock.Lock()
Expand Down
1 change: 0 additions & 1 deletion pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ func TestCache_MaxItems_Overwrite(t *testing.T) {
assert.True(t, ok, "expected key to be found")
assert.Equal(t, 1, n, "expected value to be 1, got %d", n)


c.Bucket("test").SetWithTTL("key", 2, time.Millisecond*100)
n, ok = c.Bucket("test").Get("key")
assert.True(t, ok, "expected key to be found")
Expand Down
2 changes: 1 addition & 1 deletion pkg/dgclient/document_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestDGClient_GetDocument(t *testing.T) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(&dgclient.ResponseWrapper[*spec.Document]{
Data: &spec.Document{
ID: "test",
ID: "test",
CollectionName: "test",
},
})
Expand Down
Loading

0 comments on commit da7e866

Please sign in to comment.