Skip to content

Commit

Permalink
Fix: De-register node in case of failed services or db init
Browse files Browse the repository at this point in the history
  • Loading branch information
qraimbault committed Jul 11, 2020
1 parent af8c7f6 commit dc6d371
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 35 deletions.
8 changes: 8 additions & 0 deletions database/database-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions database/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ func generateFirstUser() error {
}

user := models.User{
Username: "Admin",
Username: "admin",
Password: string(password),
Email: "root@localhost",
}

err = user.InsertG(boil.Infer())

utils.Logger.Info(fmt.Sprintf("First user credentials: Admin - %s", plainPassword), nil)
utils.Logger.Info(fmt.Sprintf("First user credentials: admin - %s", plainPassword), nil)

return err
}
Expand Down
2 changes: 2 additions & 0 deletions orchestrator/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ func StartOrchestrator() {
err = handleDatabaseStartup()
if err != nil {
utils.Logger.StdErrorCritical(err, nil)
_ = state.Manager.RemoveNode()
os.Exit(1)
}

if err = proxy.LoadServices(); err != nil {
utils.Logger.StdErrorCritical(err, nil)
_ = state.Manager.RemoveNode()
os.Exit(1)
}

Expand Down
29 changes: 29 additions & 0 deletions packrd/packed-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions proxy/middlewares.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
package proxy

import (
"github.com/forsam-education/cerberus/state"
"github.com/gofrs/uuid"
"net/http"
)

var middlewares = []func(next http.Handler) http.Handler{
connectionCounter,
injectRequestID,
}

func connectionCounter(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := state.Manager.AddRequest()
next.ServeHTTP(w, r)
if err == nil {
_ = state.Manager.RemoveRequest()
}
})
}

func injectRequestID(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestUUID, err := uuid.NewV4()
Expand Down
22 changes: 1 addition & 21 deletions state/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,6 @@ type manager struct {
IsLeaderNode bool
}

// GetCurrentRequestsCount fetches the current proxy request from the shared manager.
func (manager *manager) GetCurrentRequestsCount() (int, error) {
return manager.RedisClient.Get(CurrentRequestsCount).Int()
}

// AddRequest adds a request to the current request count in the shared manager.
func (manager *manager) AddRequest() error {
return manager.RedisClient.Incr(CurrentRequestsCount).Err()
}

// RemoveRequest removes a request from the current request count in the shared manager.
func (manager *manager) RemoveRequest() error {
return manager.RedisClient.Decr(CurrentRequestsCount).Err()
}

// GetAndResetRequestCount performs a getset on the manager to get the request count and reset it to 0 in an atomic operation.
func (manager *manager) GetAndResetRequestCount() (int, error) {
return manager.RedisClient.GetSet(CurrentRequestsCount, 0).Int()
}

// GetCurrentNodesCount fetches the cerberus nodes from the shared manager.
func (manager *manager) GetCurrentNodesCount() (int, error) {
return manager.RedisClient.Get(CurrentNodesCount).Int()
Expand Down Expand Up @@ -161,7 +141,7 @@ func (manager *manager) AddService(service *models.Service) error {
return err
}

return manager.RedisClient.Set(service.ServicePath, string(serialized), viper.GetDuration(utils.RedisServerServiceTTL)*time.Minute).Err()
return manager.RedisClient.Set(service.ServicePath, string(serialized), viper.GetDuration(utils.RedisServerServiceTTL)*time.Second).Err()
}

func (manager *manager) RemoveService(service *models.Service) error {
Expand Down

0 comments on commit dc6d371

Please sign in to comment.