syncpool

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2025 License: MIT Imports: 2 Imported by: 1

README

anacpe9/sync-pool

Usage

go get github.com/anacpe9/sync-pool
Basic
package main

import (
	syncpool "github.com/anacpe9/sync-pool"
)

func main() {
	type Foo struct{ x int }
	type Bar struct{ y int }

	fooPool_01 := syncpool.GetPool[Foo]()
	fooPool_02 := syncpool.GetPool[Foo]()
	barPool_01 := syncpool.GetPool[Bar]()

	// ...

	foo_01 := fooPool_01.Get()
	foo_02 := fooPool_02.Get()
	bar_01 := barPool_01.Get()

	fooPool_01.Put(foo_01)
	fooPool_01.Put(foo_02)

	barPool_01.Put(bar_01)
}
Fiber example
func NewDTOMiddleware[T interface{ Reset() }]() *DTOMiddleware[T] {
	pool := syncpool.GetPool[T]() // when call GetPoo[T], it's call new (T) every time
	return &DTOMiddleware[T]{
		dtoPool: pool,
	}
}
func (mid *DTOMiddleware[T]) DTOValidate() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		resMsg := responseHTTPPool.Get()
		dtoBody := mid.dtoPool.Get()
		defer func() {
			dto := *dtoBody
			dto.Reset()
			mid.dtoPool.Put(&dto)

			resMsg.Reset()
			responseHTTPPool.Put(resMsg)
		}()

		// // for body
		// ctx.BodyParser(dtoBody);
		// // ...
		//
		// validate DTO
		// errors := ValidateStruct(*dtoBody)
		// // ...
		// //

		// share dto to next chain
		ctx.Locals("body", *dtoBody)
		return ctx.Next()
	}
}
Test invalid payload
curl -i -s --location 'http://localhost:3000/otp' \
  --header 'Content-Type: application/json' \
  --data '{}'
HTTP/1.1 400 Bad Request
Date: Thu, 16 May 2024 14:29:50 GMT
Content-Type: application/json
Content-Length: 358

{"error":"'TargetId' has a value of '' which does not satisfy 'required'.","errors":["'TargetId' has a value of '' which does not satisfy 'required'.","'CommandId' has a value of '' which does not satisfy 'required'.","'Timestamp' has a value of '' which does not satisfy 'required'.","'OTP' has a value of '' which does not satisfy 'required'."],"code":400}
Test success
curl -i -s --location 'http://localhost:3000/otp' \
  --header 'Content-Type: application/json' \
  --data '{
      "targetId": "123",
      "commandId": "456",
      "timestamp": "202405151730",
      "otp": "123"
  }'
HTTP/1.1 201 Created
Date: Thu, 16 May 2024 14:33:48 GMT
Content-Type: application/json
Content-Length: 42

{"message":"Success","code":201,"ok":true}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool[T any] struct {
	// contains filtered or unexported fields
}

Pool is a strongly typed version of sync.Pool from go standard library

func GetPool

func GetPool[T any]() *Pool[T]

func (*Pool[T]) Get

func (p *Pool[T]) Get() *T

Get returns an arbitrary item from the pool.

func (*Pool[T]) Put

func (p *Pool[T]) Put(x *T)

Put places an item in the pool

Directories

Path Synopsis
example
basic command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL