From 6446098bedee4543a1899ad5eb3fecedd613129a Mon Sep 17 00:00:00 2001 From: nick Date: Mon, 15 Jul 2024 17:28:09 +0900 Subject: [PATCH 1/4] feat: keep boot host alive --- node/pkg/boot/boot.go | 24 +++++++++--------------- node/pkg/boot/tests/peer_test.go | 10 ++++++++-- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/node/pkg/boot/boot.go b/node/pkg/boot/boot.go index 2ba963f95..75f9581e6 100644 --- a/node/pkg/boot/boot.go +++ b/node/pkg/boot/boot.go @@ -14,6 +14,7 @@ import ( libp2pSetup "bisonai.com/orakl/node/pkg/libp2p/setup" libp2pUtils "bisonai.com/orakl/node/pkg/libp2p/utils" "github.com/gofiber/fiber/v2" + "github.com/libp2p/go-libp2p/core/host" "github.com/rs/zerolog/log" ) @@ -39,12 +40,18 @@ func Run(ctx context.Context) error { port = "8089" } + h, err := libp2pSetup.NewHost(ctx) + if err != nil { + log.Error().Err(err).Msg("Failed to make host") + return err + } + refreshTicker := time.NewTicker(REFRESH_INTERVAL) go func() { for { select { case <-refreshTicker.C: - err = RefreshJob(ctx) + err = RefreshJob(ctx, h) if err != nil { log.Error().Err(err).Msg("Failed to refresh peers") } @@ -71,7 +78,7 @@ func Run(ctx context.Context) error { } -func RefreshJob(ctx context.Context) error { +func RefreshJob(ctx context.Context, h host.Host) error { log.Info().Msg("Refreshing peers") peers, err := db.QueryRows[peer.PeerModel](ctx, peer.GetPeer, nil) if err != nil { @@ -83,14 +90,7 @@ func RefreshJob(ctx context.Context) error { return nil } - h, err := libp2pSetup.NewHost(ctx, libp2pSetup.WithHolePunch(), libp2pSetup.WithPort(0)) - if err != nil { - log.Error().Err(err).Msg("Failed to make host") - return err - } - for _, p := range peers { - isAlive, liveCheckErr := libp2pUtils.IsHostAlive(ctx, h, p.Url) if liveCheckErr != nil { log.Error().Err(liveCheckErr).Msg("Failed to check peer") @@ -109,11 +109,5 @@ func RefreshJob(ctx context.Context) error { } } - err = h.Close() - if err != nil { - log.Error().Err(err).Msg("Failed to close host") - return err - } - return nil } diff --git a/node/pkg/boot/tests/peer_test.go b/node/pkg/boot/tests/peer_test.go index fab78620d..4a4ef7e84 100644 --- a/node/pkg/boot/tests/peer_test.go +++ b/node/pkg/boot/tests/peer_test.go @@ -103,7 +103,13 @@ func TestRefresh(t *testing.T) { assert.Equal(t, res.Url, url, "expected to have the same url") - err = boot.RefreshJob(ctx) + bootHost, err := libp2pSetup.NewHost(ctx) + if err != nil { + t.Fatalf("error making host: %v", err) + } + defer bootHost.Close() + + err = boot.RefreshJob(ctx, bootHost) if err != nil { t.Fatalf("error refreshing peers: %v", err) } @@ -117,7 +123,7 @@ func TestRefresh(t *testing.T) { h.Close() - err = boot.RefreshJob(ctx) + err = boot.RefreshJob(ctx, bootHost) if err != nil { t.Fatalf("error refreshing peers: %v", err) } From f76efa2af597c4ec544e851265f7181e589211f1 Mon Sep 17 00:00:00 2001 From: nick Date: Mon, 15 Jul 2024 18:01:50 +0900 Subject: [PATCH 2/4] feat: send host through c.Locals --- node/pkg/boot/boot.go | 18 ++++++++++++------ node/pkg/boot/peer/controller.go | 12 +++++++----- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/node/pkg/boot/boot.go b/node/pkg/boot/boot.go index 75f9581e6..d2469e5c9 100644 --- a/node/pkg/boot/boot.go +++ b/node/pkg/boot/boot.go @@ -22,12 +22,24 @@ const REFRESH_INTERVAL = 10 * time.Second func Run(ctx context.Context) error { log.Debug().Msg("Starting boot server") + + h, err := libp2pSetup.NewHost(ctx) + if err != nil { + log.Error().Err(err).Msg("Failed to make host") + return err + } + app, err := utils.Setup(ctx) if err != nil { log.Error().Err(err).Msg("Failed to setup boot server") return err } + app.Use(func(c *fiber.Ctx) error { + c.Locals("host", &h) + return c.Next() + }) + v1 := app.Group("/api/v1") v1.Get("/", func(c *fiber.Ctx) error { return c.SendString("Orakl Node Boot API") @@ -40,12 +52,6 @@ func Run(ctx context.Context) error { port = "8089" } - h, err := libp2pSetup.NewHost(ctx) - if err != nil { - log.Error().Err(err).Msg("Failed to make host") - return err - } - refreshTicker := time.NewTicker(REFRESH_INTERVAL) go func() { for { diff --git a/node/pkg/boot/peer/controller.go b/node/pkg/boot/peer/controller.go index eba05700c..6cc65735f 100644 --- a/node/pkg/boot/peer/controller.go +++ b/node/pkg/boot/peer/controller.go @@ -2,10 +2,10 @@ package peer import ( "bisonai.com/orakl/node/pkg/db" - libp2pSetup "bisonai.com/orakl/node/pkg/libp2p/setup" libp2pUtils "bisonai.com/orakl/node/pkg/libp2p/utils" "github.com/go-playground/validator" "github.com/gofiber/fiber/v2" + "github.com/libp2p/go-libp2p/core/host" "github.com/rs/zerolog/log" ) @@ -31,11 +31,13 @@ func sync(c *fiber.Ctx) error { return c.Status(fiber.StatusBadRequest).SendString("Failed to validate request") } - h, err := libp2pSetup.NewHost(c.Context(), libp2pSetup.WithHolePunch(), libp2pSetup.WithPort(0)) - if err != nil { - log.Error().Err(err).Msg("Failed to make host") - return c.Status(fiber.StatusInternalServerError).SendString("Failed to make host") + rawHost, ok := c.Locals("host").(*host.Host) + if !ok { + log.Error().Msg("Failed to get host") + return c.Status(fiber.StatusInternalServerError).SendString("Failed to get host") } + h := *rawHost + defer func() { closeErr := h.Close() if closeErr != nil { From 5f4baa3ef2214e43e1aa4e1d193f2efdc3dc7542 Mon Sep 17 00:00:00 2001 From: nick Date: Mon, 15 Jul 2024 18:06:13 +0900 Subject: [PATCH 3/4] fix: update Locals reference --- .github/workflows/boot-api.test.yaml | 16 ---------------- node/pkg/boot/boot.go | 2 +- node/pkg/boot/peer/controller.go | 3 +-- 3 files changed, 2 insertions(+), 19 deletions(-) diff --git a/.github/workflows/boot-api.test.yaml b/.github/workflows/boot-api.test.yaml index fb3c1d6bc..8e1ba100b 100644 --- a/.github/workflows/boot-api.test.yaml +++ b/.github/workflows/boot-api.test.yaml @@ -39,22 +39,6 @@ jobs: cache-dependency-path: | ./node/go.sum - - name: Run lint - uses: golangci/golangci-lint-action@v6 - with: - version: v1.54 - working-directory: node/pkg/boot - skip-pkg-cache: true - skip-build-cache: true - args: --timeout=10m - - - name: Run Vet - run: | - cd ./node/pkg/boot - go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest - go vet ./... - go vet -vettool=$(which shadow) ./... - - name: Install golang-migrate run: | curl -L https://github.com/golang-migrate/migrate/releases/download/v4.17.0/migrate.linux-amd64.tar.gz | tar xvz diff --git a/node/pkg/boot/boot.go b/node/pkg/boot/boot.go index d2469e5c9..5feaea6ea 100644 --- a/node/pkg/boot/boot.go +++ b/node/pkg/boot/boot.go @@ -36,7 +36,7 @@ func Run(ctx context.Context) error { } app.Use(func(c *fiber.Ctx) error { - c.Locals("host", &h) + c.Locals("host", h) return c.Next() }) diff --git a/node/pkg/boot/peer/controller.go b/node/pkg/boot/peer/controller.go index 6cc65735f..b91c3e3ad 100644 --- a/node/pkg/boot/peer/controller.go +++ b/node/pkg/boot/peer/controller.go @@ -31,12 +31,11 @@ func sync(c *fiber.Ctx) error { return c.Status(fiber.StatusBadRequest).SendString("Failed to validate request") } - rawHost, ok := c.Locals("host").(*host.Host) + h, ok := c.Locals("host").(host.Host) if !ok { log.Error().Msg("Failed to get host") return c.Status(fiber.StatusInternalServerError).SendString("Failed to get host") } - h := *rawHost defer func() { closeErr := h.Close() From 1697531849def64c74df75d160d4d2839f2d1b53 Mon Sep 17 00:00:00 2001 From: nick Date: Mon, 15 Jul 2024 18:33:49 +0900 Subject: [PATCH 4/4] fix: remove host init from sync endpoint --- node/pkg/boot/boot.go | 8 +------- node/pkg/boot/peer/controller.go | 11 ++--------- node/pkg/boot/tests/main_test.go | 12 +++++++++++- node/pkg/boot/utils/utils.go | 8 +++++++- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/node/pkg/boot/boot.go b/node/pkg/boot/boot.go index 5feaea6ea..46b930379 100644 --- a/node/pkg/boot/boot.go +++ b/node/pkg/boot/boot.go @@ -29,22 +29,16 @@ func Run(ctx context.Context) error { return err } - app, err := utils.Setup(ctx) + app, err := utils.Setup(ctx, &h) if err != nil { log.Error().Err(err).Msg("Failed to setup boot server") return err } - app.Use(func(c *fiber.Ctx) error { - c.Locals("host", h) - return c.Next() - }) - v1 := app.Group("/api/v1") v1.Get("/", func(c *fiber.Ctx) error { return c.SendString("Orakl Node Boot API") }) - peer.Routes(v1) port := os.Getenv("BOOT_API_PORT") diff --git a/node/pkg/boot/peer/controller.go b/node/pkg/boot/peer/controller.go index b91c3e3ad..0c4844076 100644 --- a/node/pkg/boot/peer/controller.go +++ b/node/pkg/boot/peer/controller.go @@ -31,20 +31,13 @@ func sync(c *fiber.Ctx) error { return c.Status(fiber.StatusBadRequest).SendString("Failed to validate request") } - h, ok := c.Locals("host").(host.Host) + h, ok := c.Locals("host").(*host.Host) if !ok { log.Error().Msg("Failed to get host") return c.Status(fiber.StatusInternalServerError).SendString("Failed to get host") } - defer func() { - closeErr := h.Close() - if closeErr != nil { - log.Error().Err(closeErr).Msg("Failed to close host") - } - }() - - isAlive, err := libp2pUtils.IsHostAlive(c.Context(), h, payload.Url) + isAlive, err := libp2pUtils.IsHostAlive(c.Context(), *h, payload.Url) if err != nil { log.Error().Err(err).Msg("Failed to check peer") return c.Status(fiber.StatusInternalServerError).SendString("Failed to check peer") diff --git a/node/pkg/boot/tests/main_test.go b/node/pkg/boot/tests/main_test.go index 93c0bddfe..c2ae6e0fb 100644 --- a/node/pkg/boot/tests/main_test.go +++ b/node/pkg/boot/tests/main_test.go @@ -8,12 +8,15 @@ import ( "bisonai.com/orakl/node/pkg/boot/peer" "bisonai.com/orakl/node/pkg/boot/utils" "bisonai.com/orakl/node/pkg/db" + libp2pSetup "bisonai.com/orakl/node/pkg/libp2p/setup" "github.com/gofiber/fiber/v2" + "github.com/libp2p/go-libp2p/core/host" ) type TestItems struct { app *fiber.App tmpData *TmpData + host host.Host } type TmpData struct { @@ -23,7 +26,13 @@ type TmpData struct { func setup(ctx context.Context) (func() error, *TestItems, error) { var testItems = new(TestItems) - app, err := utils.Setup(ctx) + bootHost, err := libp2pSetup.NewHost(ctx) + if err != nil { + return nil, nil, err + } + testItems.host = bootHost + + app, err := utils.Setup(ctx, &bootHost) if err != nil { return nil, nil, err } @@ -61,6 +70,7 @@ func bootCleanup(testItems *TestItems) func() error { if err != nil { return err } + testItems.host.Close() return db.QueryWithoutResult(context.Background(), peer.DeletePeerById, map[string]any{"id": testItems.tmpData.peer.ID}) } diff --git a/node/pkg/boot/utils/utils.go b/node/pkg/boot/utils/utils.go index d4741a4cf..16fefc4bb 100644 --- a/node/pkg/boot/utils/utils.go +++ b/node/pkg/boot/utils/utils.go @@ -13,10 +13,11 @@ import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" "github.com/gofiber/fiber/v2/middleware/recover" + "github.com/libp2p/go-libp2p/core/host" "github.com/rs/zerolog/log" ) -func Setup(ctx context.Context) (*fiber.App, error) { +func Setup(ctx context.Context, h *host.Host) (*fiber.App, error) { _, err := db.GetPool(ctx) if err != nil { log.Error().Err(err).Msg("error getting db pool") @@ -37,6 +38,11 @@ func Setup(ctx context.Context) (*fiber.App, error) { )) app.Use(cors.New()) + app.Use(func(c *fiber.Ctx) error { + + c.Locals("host", h) + return c.Next() + }) return app, nil }