Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/jackc/pgx/v5 v5.7.6
github.com/shirou/gopsutil/v4 v4.25.11
github.com/valyala/fasthttp v1.68.0
go.uber.org/automaxprocs v1.6.0
)

require (
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
github.com/shamaton/msgpack/v2 v2.4.0 h1:O5Z08MRmbo0lA9o2xnQ4TXx6teJbPqEurqcCOQ8Oi/4=
github.com/shamaton/msgpack/v2 v2.4.0/go.mod h1:6khjYnkx73f7VQU7wjcFS9DFjs+59naVWJv1TB7qdOI=
github.com/shirou/gopsutil/v4 v4.25.11 h1:X53gB7muL9Gnwwo2evPSE+SfOrltMoR6V3xJAXZILTY=
Expand Down Expand Up @@ -86,6 +88,8 @@ github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZ
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=
go.uber.org/automaxprocs v1.6.0/go.mod h1:ifeIMSnPZuznNm6jmdzmU3/bfk01Fe2fotchwEFJ8r8=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.44.0 h1:A97SsFvM3AIwEEmTBiaxPPTYpDC47w720rdiiUvgoAU=
Expand Down
1 change: 0 additions & 1 deletion go/db/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func NewDatabase(databaseUrl string) (*Client, error) {
return nil, err
}

config.MaxConns = 200
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
sonicCodec := &pgtype.JSONCodec{
Marshal: sonic.Marshal,
Expand Down
2 changes: 1 addition & 1 deletion go/db/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ select "orders"."id", "orders"."shipped_date", "orders"."ship_name", "orders"."s
select "orders"."id", "orders"."shipped_date", "orders"."ship_name", "orders"."ship_city", "orders"."ship_country", count("order_details"."product_id")::int, sum("order_details"."quantity")::int, sum("order_details"."quantity" * "order_details"."unit_price")::real from "orders" left join "order_details" on "order_details"."order_id" = "orders"."id" where "orders"."id" = $1 group by "orders"."id" order by "orders"."id" asc;

-- name: OrderWithDetailsAndProducts :one
select "d0"."id" as "id", "d0"."order_date" as "orderDate", "d0"."required_date" as "requiredDate", "d0"."shipped_date" as "shippedDate", "d0"."ship_via" as "shipVia", "d0"."freight" as "freight", "d0"."ship_name" as "shipName", "d0"."ship_city" as "shipCity", "d0"."ship_region" as "shipRegion", "d0"."ship_postal_code" as "shipPostalCode", "d0"."ship_country" as "shipCountry", "d0"."customer_id" as "customerId", "d0"."employee_id" as "employeeId", "details"."r" as "details" from "orders" as "d0" left join lateral(select coalesce(json_agg(row_to_json("t".*)), '[]') as "r" from (select "d1"."unit_price" as "unitPrice", "d1"."quantity" as "quantity", "d1"."discount" as "discount", "d1"."order_id" as "orderId", "d1"."product_id" as "productId", "product"."r" as "product" from "order_details" as "d1" left join lateral(select row_to_json("t".*) "r" from (select "d2"."id" as "id", "d2"."name" as "name", "d2"."qt_per_unit" as "quantityPerUnit", "d2"."unit_price" as "unitPrice", "d2"."units_in_stock" as "unitsInStock", "d2"."units_on_order" as "unitsOnOrder", "d2"."reorder_level" as "reorderLevel", "d2"."discontinued" as "discontinued", "d2"."supplier_id" as "supplierId" from "products" as "d2" where "d1"."product_id" = "d2"."id") as "t") as "product" on true where "d0"."id" = "d1"."order_id") as "t") as "details" on true where "d0"."id" = $1;
select "d0"."id" as "id", "d0"."order_date" as "orderDate", "d0"."required_date" as "requiredDate", "d0"."shipped_date" as "shippedDate", "d0"."ship_via" as "shipVia", "d0"."freight" as "freight", "d0"."ship_name" as "shipName", "d0"."ship_city" as "shipCity", "d0"."ship_region" as "shipRegion", "d0"."ship_postal_code" as "shipPostalCode", "d0"."ship_country" as "shipCountry", "d0"."customer_id" as "customerId", "d0"."employee_id" as "employeeId", "details"."r" as "details" from "orders" as "d0" left join lateral(select coalesce(json_agg(row_to_json("t".*)), '[]') as "r" from (select "d1"."unit_price" as "unitPrice", "d1"."quantity" as "quantity", "d1"."discount" as "discount", "d1"."order_id" as "orderId", "d1"."product_id" as "productId", "product"."r" as "product" from "order_details" as "d1" left join lateral(select row_to_json("t".*) "r" from (select "d2"."id" as "id", "d2"."name" as "name", "d2"."qt_per_unit" as "quantityPerUnit", "d2"."unit_price" as "unitPrice", "d2"."units_in_stock" as "unitsInStock", "d2"."units_on_order" as "unitsOnOrder", "d2"."reorder_level" as "reorderLevel", "d2"."discontinued" as "discontinued", "d2"."supplier_id" as "supplierId" from "products" as "d2" where "d1"."product_id" = "d2"."id") as "t") as "product" on true where "d0"."id" = "d1"."order_id") as "t") as "details" on true where "d0"."id" = $1;
54 changes: 39 additions & 15 deletions go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/gofiber/fiber/v3"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/valyala/fasthttp"
_ "go.uber.org/automaxprocs"
)

func b2s(b []byte) string {
Expand All @@ -34,6 +35,13 @@ func getInt32(c fiber.Ctx, key string) int32 {
return int32(n)
}

var jsonEncoder = sonic.ConfigFastest

func streamJSON(c fiber.Ctx, v interface{}) error {
w := c.Response().BodyWriter()
return jsonEncoder.NewEncoder(w).Encode(v)
}

type CPUData struct {
Usage float64
Total float64
Expand All @@ -54,8 +62,11 @@ func main() {
}

app := fiber.New(fiber.Config{
JSONEncoder: sonic.ConfigDefault.Marshal,
JSONDecoder: sonic.ConfigDefault.Unmarshal,
JSONEncoder: sonic.ConfigFastest.Marshal,
JSONDecoder: sonic.ConfigFastest.Unmarshal,
CaseSensitive: true,
StrictRouting: true,
ServerHeader: "",
})

var (
Expand Down Expand Up @@ -116,7 +127,8 @@ func main() {
return err
}

return c.JSON(rows)
c.Set("Content-Type", "application/json")
return streamJSON(c, rows)
})

app.Get("/customer-by-id", func(c fiber.Ctx) error {
Expand All @@ -125,7 +137,8 @@ func main() {
return err
}

return c.JSON(row)
c.Set("Content-Type", "application/json")
return streamJSON(c, row)
})

app.Get("/search-customer", func(c fiber.Ctx) error {
Expand All @@ -136,7 +149,8 @@ func main() {
return err
}

return c.JSON(rows)
c.Set("Content-Type", "application/json")
return streamJSON(c, rows)
})

app.Get("/employees", func(c fiber.Ctx) error {
Expand All @@ -148,7 +162,8 @@ func main() {
return err
}

return c.JSON(rows)
c.Set("Content-Type", "application/json")
return streamJSON(c, rows)
})

app.Get("/employee-with-recipient", func(c fiber.Ctx) error {
Expand All @@ -157,7 +172,8 @@ func main() {
return err
}

return c.JSON(row)
c.Set("Content-Type", "application/json")
return streamJSON(c, row)
})

app.Get("/suppliers", func(c fiber.Ctx) error {
Expand All @@ -169,7 +185,8 @@ func main() {
return err
}

return c.JSON(rows)
c.Set("Content-Type", "application/json")
return streamJSON(c, rows)
})

app.Get("/supplier-by-id", func(c fiber.Ctx) error {
Expand All @@ -178,7 +195,8 @@ func main() {
return err
}

return c.JSON(row)
c.Set("Content-Type", "application/json")
return streamJSON(c, row)
})

app.Get("/products", func(c fiber.Ctx) error {
Expand All @@ -190,7 +208,8 @@ func main() {
return err
}

return c.JSON(rows)
c.Set("Content-Type", "application/json")
return streamJSON(c, rows)
})

app.Get("/product-with-supplier", func(c fiber.Ctx) error {
Expand All @@ -199,7 +218,8 @@ func main() {
return err
}

return c.JSON(row)
c.Set("Content-Type", "application/json")
return streamJSON(c, row)
})

app.Get("/search-product", func(c fiber.Ctx) error {
Expand All @@ -210,7 +230,8 @@ func main() {
return err
}

return c.JSON(rows)
c.Set("Content-Type", "application/json")
return streamJSON(c, rows)
})

app.Get("/orders-with-details", func(c fiber.Ctx) error {
Expand All @@ -222,7 +243,8 @@ func main() {
return err
}

return c.JSON(rows)
c.Set("Content-Type", "application/json")
return streamJSON(c, rows)
})

app.Get("/order-with-details", func(c fiber.Ctx) error {
Expand All @@ -231,7 +253,8 @@ func main() {
return err
}

return c.JSON(row)
c.Set("Content-Type", "application/json")
return streamJSON(c, row)
})

app.Get("/order-with-details-and-products", func(c fiber.Ctx) error {
Expand All @@ -240,7 +263,8 @@ func main() {
return err
}

return c.JSON(row)
c.Set("Content-Type", "application/json")
return streamJSON(c, row)
})

go func() {
Expand Down