Commit 91ebfb73 authored by Vladimir Barsukov's avatar Vladimir Barsukov
Browse files

first commit

parents
//go:build !go_json
package zjson
import (
"encoding/json"
"unsafe"
)
func ToBytes(obj any) ([]byte, error) {
return json.Marshal(obj)
}
func ToString(obj any) (string, error) {
if b, err := json.Marshal(obj); err != nil {
return "", err
} else {
return *(*string)(unsafe.Pointer(&b)), err
}
}
func ToStringSafe(obj any) (string, error) {
if b, err := json.Marshal(obj); err != nil {
return "", err
} else {
return string(b), err
}
}
func MustBytes(obj any) []byte {
b, _ := ToBytes(obj)
return b
}
func MustString(obj any) string {
b, _ := ToBytes(obj)
return *(*string)(unsafe.Pointer(&b))
}
func MustStringSafe(obj any) string {
b, _ := ToBytes(obj)
return string(b)
}
func Pretty(i any) string {
ss, _ := json.MarshalIndent(i, "", " ")
return string(ss)
}
package zqueue
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com:barsukov-vv/komrad/zgin"
"sync"
)
type ZQueue struct {
queue map[string]chan *ZJob
mu sync.Mutex
}
type ZJob struct {
Id int64 `json:"id"`
Body any `json:"body"`
Result chan any `json:"-"`
Wg *sync.WaitGroup `json:"-"`
}
func New() *ZQueue {
return &ZQueue{}
}
func Default() *ZQueue {
return &ZQueue{}
}
func (q *ZQueue) GetHandler(c *gin.Context) {
queue := c.Param("queue")
if qChan, ok := q.queue[queue]; !ok {
zgin.Err(c, errors.New(fmt.Sprintf("unknown queue: '%s'", queue)))
} else {
job := <-qChan
zgin.Ok(c, job.Body)
return
}
}
func (q *ZQueue) PostHandler(c *gin.Context) {
}
func (q *ZQueue) NewHandler(c *gin.Context) {
}
package zquit
import (
"github.com/gin-gonic/gin"
"net/http"
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"
)
type ZQuit struct {
isQuit bool
delta atomic.Int32
wg sync.WaitGroup
}
func Default() *ZQuit {
return &ZQuit{}
}
func (q *ZQuit) Add(delta int) {
q.delta.Add(int32(delta))
q.wg.Add(delta)
}
func (q *ZQuit) Inc() {
q.Add(1)
}
func (q *ZQuit) Done() {
q.delta.Add(-1)
q.wg.Done()
}
func (q *ZQuit) Wait() {
q.wg.Wait()
}
func (q *ZQuit) IncDefer() func() {
q.Inc()
return func() {
q.Done()
}
}
func (q *ZQuit) IsQuit() bool {
return q.isQuit
}
func (q *ZQuit) Delta() int32 {
return q.delta.Load()
}
func (q *ZQuit) WaitInterruptPrePost(pre func(), post func()) {
pre()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
q.Wait()
post()
}
func (q *ZQuit) WaitInterrupt() {
q.WaitInterruptPrePost(func() {}, func() {})
}
func (q *ZQuit) ZQuitMiddleware(c *gin.Context) {
if q.isQuit {
c.Data(http.StatusServiceUnavailable, "text/plain", []byte("server shutdown"))
return
}
cancel := q.IncDefer()
defer cancel()
c.Next()
}
package zutils
import "strings"
func Uniq[T comparable](s []T) []T {
keys := make(map[T]bool)
var list []T
for _, entry := range s {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func Contains(s string, sl []string) bool {
for _, s2 := range sl {
if strings.Contains(s, s2) {
return true
}
}
return false
}
func Remove[T comparable](slice []T, s int) []T {
return append(slice[:s], slice[s+1:]...)
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment