Skip to content

Commit

Permalink
💥 move pooling detection to unique ids per-lambda-instnace
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Oct 27, 2022
1 parent b1b274a commit fa6f02c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
7 changes: 0 additions & 7 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

var Db *gorm.DB
var Os *opensearch.Client
var Pooled = true

func DSN() string {
return fmt.Sprintf(
Expand Down Expand Up @@ -54,12 +53,6 @@ func Setup() {
}
}

if Os != nil {
Pooled = true
return
}

Pooled = false
if setting.Opensearch.Connect == "true" {
Os, err = opensearch.NewClient(opensearch.Config{
Transport: &http.Transport{
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func Init(config *fiber.Config) *fiber.App {
return app
}

func ShowPooled(app *fiber.App) {
render.ExposePooled = true
app.Use(route.Pooled)
func ShowUUID(app *fiber.App) {
render.ShowUUID = true
route.GenerateUUID()
app.Use(route.UUIDMiddleware)
}
15 changes: 7 additions & 8 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (

type H map[string]any

var ExposePooled bool = false
var ShowUUID bool = false

func Success(c *fiber.Ctx, message string, a ...interface{}) error {
json := H{"_benchmark": bench(c), "type": "success", "success": true, "message": message}
if ExposePooled {
json["_pooled"] = pooled(c)
if ShowUUID {
json["_uuid"] = uuid(c)
}
if len(a) > 0 {
json["data"] = a[0]
Expand Down Expand Up @@ -53,8 +53,8 @@ func HTML(c *fiber.Ctx, html string) error {

func Render(c *fiber.Ctx, data any, a ...interface{}) error {
json := H{"_benchmark": bench(c), "data": data}
if ExposePooled {
json["_pooled"] = pooled(c)
if ShowUUID {
json["_uuid"] = uuid(c)
}
if len(a) > 0 {
json["_meta"] = a[0]
Expand All @@ -68,7 +68,6 @@ func bench(c *fiber.Ctx) float64 {
return diff
}

func pooled(c *fiber.Ctx) bool {
pooled := c.Locals("tonicPooled")
return pooled.(bool)
func uuid(c *fiber.Ctx) string {
return c.Locals("tonicUUID").(string)
}
14 changes: 10 additions & 4 deletions route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package route

import (
"errors"
"github.com/google/uuid"
"net/http"
"strconv"
"time"
Expand All @@ -21,6 +22,7 @@ type ApiResourceStruct struct {
var (
model any
resources ApiResourceStruct
UUID uuid.UUID
)

type binder func(c *fiber.Ctx, value any) error
Expand All @@ -30,15 +32,19 @@ func Benchmark(c *fiber.Ctx) error {
return c.Next()
}

func Pooled(c *fiber.Ctx) error {
c.Locals("tonicPooled", database.Pooled)
func GenerateUUID() {
UUID = uuid.New()
}

func UUIDMiddleware(c *fiber.Ctx) error {
c.Locals("tonicUUID", UUID.String())
return c.Next()
}

func bind(c *fiber.Ctx, callback binder) error {
if isNumeric(c) {
value, error := retrieve(c)
if error != nil {
value, err := retrieve(c)
if err != nil {
return invalid(c)
} else {
return callback(c, value)
Expand Down

0 comments on commit fa6f02c

Please sign in to comment.