Skip to content

Commit

Permalink
update host registration:
Browse files Browse the repository at this point in the history
Signed-off-by: Pranav Singh <[email protected]>
  • Loading branch information
theBeginner86 committed Sep 27, 2023
1 parent ddc03e0 commit ddd2f71
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
37 changes: 34 additions & 3 deletions models/meshmodel/registry/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"time"
"encoding/json"
"sync"

"github.com/google/uuid"
"github.com/layer5io/meshkit/database"
Expand All @@ -12,6 +14,8 @@ import (
"gorm.io/gorm"
)

var hostCreationLock sync.Mutex //Each entity will perform a check and if the host already doesn't exist, it will create a host. This lock will make sure that there are no race conditions.

type Host struct {
ID uuid.UUID `json:"-"`
Hostname string
Expand All @@ -23,9 +27,36 @@ type Host struct {
}

func createHost(db *database.Handler, h Host) (uuid.UUID, error) {
h.ID = uuid.New()
err := db.Create(&h).Error
return h.ID, err
byt, err := json.Marshal(h)
if err != nil {
return uuid.UUID{}, err
}
hID := uuid.NewSHA1(uuid.UUID{}, byt)
var host Host
hostCreationLock.Lock()
defer hostCreationLock.Unlock()
err = db.First(&host, "id = ?", hID).Error // check if the host already exists
if err != nil && err != gorm.ErrRecordNotFound {
fmt.Println("1. err: ", err)
return uuid.UUID{}, err
}

// if not exists then create a new host and return the id
if err == gorm.ErrRecordNotFound {
fmt.Println("not found")
h.ID = hID
err = db.Create(&h).Error
if err != nil {
fmt.Println("2. err", err)
return uuid.UUID{}, err
}
fmt.Println("created id: ", h.ID)
return h.ID, nil
}

// else return the id of the existing host
fmt.Println("found id: ", host.ID)
return h.ID, nil
}

func (h *Host) AfterFind(tx *gorm.DB) error {
Expand Down
3 changes: 3 additions & 0 deletions models/meshmodel/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ func (rm *RegistryManager) RegisterEntity(h Host, en Entity) error {
}
componentID, err := v1alpha1.CreateComponent(rm.db, entity)
if err != nil {
fmt.Println("err in creating component: ", err)
return err
}
registrantID, err := createHost(rm.db, h)
if err != nil {
fmt.Println("err in creating host: ", err)
return err
}
fmt.Println("registrant id: ", registrantID)
entry := Registry{
ID: uuid.New(),
RegistrantID: registrantID,
Expand Down

0 comments on commit ddd2f71

Please sign in to comment.