Skip to content

Commit

Permalink
fix: better handling of hosts and ports
Browse files Browse the repository at this point in the history
  • Loading branch information
WoodenMaiden authored and nponsard committed May 22, 2024
1 parent 496d9a4 commit d3f97d4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# ROOT_FS_STORAGE_DSN=
LAMBDO_URL="http://localhost:3000"
VM_STATE_URL=redis://localhost:6379
FUNCTION_STATE_STORAGE_DSN='host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable TimeZone=Asia/Shanghai'
JWT_SECRET="I am so secret! Hopefully someone doesn't commit me.."
Expand All @@ -9,3 +8,6 @@ BUILDER_ENDPOINT="http://localhost:8080" # grobuzin's builder service endpoint
MINIO_ENDPOINT="localhost:9000"
MINIO_ACCESS_KEY="access_key"
MINIO_SECRET_KEY="secret_key"

LAMBDO_HOST="localhost"
LAMBDO_PORT="3000"
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (

type Config struct {
// rootFsStorageDSN string `env:"ROOT_FS_STORAGE_DSN,notEmpty"`
LambdoURL string `env:"LAMBDO_URL,notEmpty"`
LambdoHost string `env:"LAMBDO_HOST,notEmpty"`
LambdoPort int `env:"LAMBDO_PORT,notEmpty"`
VMStateURL string `env:"VM_STATE_URL,notEmpty"`
FuntionStateStorageDSN string `env:"FUNCTION_STATE_STORAGE_DSN,notEmpty" envDefault:"host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable TimeZone=Asia/Shanghai"`
JWTSecret string `env:"JWT_SECRET,notEmpty"`
Expand Down Expand Up @@ -51,7 +52,8 @@ func main() {
Redis: redis,
Context: &ctx,
Lambdo: &scheduler.LambdoService{
URL: cfg.LambdoURL,
Host: cfg.LambdoHost,
Port: cfg.LambdoPort,
BucketURL: fmt.Sprint(
bucketPrefix,
cfg.MinioEndpoint,
Expand Down
2 changes: 1 addition & 1 deletion routes/function/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (c *Controller) RunFunction(ctx *gin.Context) {
}

_, err = http.Post(
fmt.Sprint("http://", string(fnState.Address), ":", fnState.Port, "/execute"),
fmt.Sprint(string(fnState.Address), ":", fnState.Port, "/execute"),
"application/json",
ctx.Request.Body,
)
Expand Down
11 changes: 8 additions & 3 deletions scheduler/lambdoService.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const (
)

type LambdoService struct {
URL string
Host string
Port int
BucketURL string
}

Expand All @@ -44,6 +45,10 @@ type LambdoSpawnResponse struct {
Ports [][2]uint16 `json:"port_mapping"`
}

func (service *LambdoService) url() string {
return fmt.Sprint("http://", service.Host, ":", service.Port)
}

func (service *LambdoService) SpawnVM(function database.Function) (data LambdoSpawnResponse, err error) {
var res *http.Response
defer func() {
Expand All @@ -69,7 +74,7 @@ func (service *LambdoService) SpawnVM(function database.Function) (data LambdoSp
}

res, err = http.Post(
fmt.Sprint(service.URL, "/spawn"),
fmt.Sprint(service.url(), "/spawn"),
"application/json",
bytes.NewReader(body),
)
Expand All @@ -90,7 +95,7 @@ func (service *LambdoService) SpawnVM(function database.Function) (data LambdoSp
}

func (service *LambdoService) DeleteVM(VMID string) (err error) {
req, err := http.NewRequest(http.MethodDelete, fmt.Sprint(service.URL, "/destroy/", VMID ), nil)
req, err := http.NewRequest(http.MethodDelete, fmt.Sprint(service.url(), "/destroy/", VMID ), nil)
if err != nil {
return
}
Expand Down
4 changes: 3 additions & 1 deletion scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"time"

"github.com/do4-2022/grobuzin/database"

"github.com/google/uuid"
Expand Down Expand Up @@ -67,6 +68,7 @@ func (s *Scheduler) LookForReadyInstance(functionId uuid.UUID, cursor uint64) (f
}

func (s *Scheduler) SpawnVM(function database.Function) (fnState database.FunctionState, err error) {
url := fmt.Sprint("http://", s.Lambdo.Host)
res, err := s.Lambdo.SpawnVM(function)

if (err != nil) {
Expand All @@ -77,7 +79,7 @@ func (s *Scheduler) SpawnVM(function database.Function) (fnState database.Functi

fnState = database.FunctionState{
ID: res.ID,
Address: s.Lambdo.URL,
Address: url,
Port: res.Ports[0][0],
Status: int(database.FnReady),
LastUsed: "never",
Expand Down

0 comments on commit d3f97d4

Please sign in to comment.