Commit ffafeb48 authored by Vladimir Barsukov's avatar Vladimir Barsukov
Browse files

lint

parent 86d85eca
run:
skip-dirs:
- main
skip-files:
- zgo.go
linters:
enable-all: true
disable:
- gci
- depguard
- goerr113
- varnamelen
- gomnd
- goimports
- gofumpt
- wsl
- wrapcheck
- nonamedreturns
- noctx
- ireturn
- funlen
- containedctx
- maligned
- cyclop
- gocyclo
- exhaustive
- gocognit
- makezero
- errchkjson
- exhaustivestruct
- exhaustruct
- forcetypeassert
- ifshort
- gochecknoglobals
- lll
\ No newline at end of file
...@@ -49,16 +49,17 @@ func (d *Pool) qAny(q *Conn, dst any, sql string, args ...any) error { ...@@ -49,16 +49,17 @@ func (d *Pool) qAny(q *Conn, dst any, sql string, args ...any) error {
v, _ := rows.Values() v, _ := rows.Values()
for i, f := range desc { for i, f := range desc {
if v[i] == nil { switch {
case v[i] == nil:
r[f.Name] = nil r[f.Name] = nil
} else if f.DataTypeOID == pgtype.NumericOID { case f.DataTypeOID == pgtype.NumericOID:
ff, _ := v[i].(pgtype.Numeric).Float64Value() ff, _ := v[i].(pgtype.Numeric).Float64Value()
r[f.Name] = ff.Float64 r[f.Name] = ff.Float64
} else if f.DataTypeOID == pgtype.UUIDOID { case f.DataTypeOID == pgtype.UUIDOID:
bb := v[i].([16]byte) bb := v[i].([16]byte)
u, _ := uuid.FromBytes(bb[:]) u, _ := uuid.FromBytes(bb[:])
r[f.Name] = u.String() r[f.Name] = u.String()
} else { default:
r[f.Name] = v[i] r[f.Name] = v[i]
} }
} }
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"github.com/jackc/pgx/v5/pgxpool" "github.com/jackc/pgx/v5/pgxpool"
"log" "log"
"reflect" "reflect"
"strconv"
"strings" "strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
...@@ -174,7 +175,7 @@ func (d *Pool) newConn(mode connMode, pgConnString string) (q *Conn, err error) ...@@ -174,7 +175,7 @@ func (d *Pool) newConn(mode connMode, pgConnString string) (q *Conn, err error)
func (d *Pool) least(s []*Conn) *Conn { func (d *Pool) least(s []*Conn) *Conn {
var out *Conn var out *Conn
var m float64 = 0 var m float64
for i, conn := range s { for i, conn := range s {
ratio := float64(conn.Stat().AcquiredConns()) / float64(conn.Stat().MaxConns()) ratio := float64(conn.Stat().AcquiredConns()) / float64(conn.Stat().MaxConns())
...@@ -243,24 +244,25 @@ func (d *Pool) execWrapper(pool connMode, dst any, f func(conn *Conn, dst1 any) ...@@ -243,24 +244,25 @@ func (d *Pool) execWrapper(pool connMode, dst any, f func(conn *Conn, dst1 any)
if err := f(q, dst); err != nil { if err := f(q, dst); err != nil {
if q.Mode == ConnModeMaster { if q.Mode == ConnModeMaster {
return err return err
} else { }
if try < d.TryOnError && contains(err.Error(), d.ContinuesTry) { if try < d.TryOnError && contains(err.Error(), d.ContinuesTry) {
try++ try++
d.logger.Printf("ZDB_EXEC_WRAPPER_REPEAT_ERR: SRV: %s TRY: %d; %s", q.ToString(), try, err.Error()) d.logger.Printf("ZDB_EXEC_WRAPPER_REPEAT_ERR: SRV: %s TRY: %d; %s", q.ToString(), try, err.Error())
time.Sleep(d.TryOnSleep) time.Sleep(d.TryOnSleep)
goto repeat
}
if contains(err.Error(), d.Continues) { goto repeat
d.setNotAliveConn(q) }
d.logger.Printf("ZDB_EXEC_WRAPPER_ERR: SRV: %s; %s", q.ToString(), err.Error())
continue if contains(err.Error(), d.Continues) {
} else { d.setNotAliveConn(q)
return err d.logger.Printf("ZDB_EXEC_WRAPPER_ERR: SRV: %s; %s", q.ToString(), err.Error())
}
continue
} }
return err
} }
return nil return nil
...@@ -298,6 +300,7 @@ func (d *Pool) setNotAliveConn(conn *Conn) { ...@@ -298,6 +300,7 @@ func (d *Pool) setNotAliveConn(conn *Conn) {
conn.Alive = false conn.Alive = false
d.notAliveConns = append(d.notAliveConns, conn) d.notAliveConns = append(d.notAliveConns, conn)
d.SrvSync = remove(d.SrvSync, i) d.SrvSync = remove(d.SrvSync, i)
return return
} }
} }
...@@ -334,6 +337,7 @@ func (d *Pool) Start() { ...@@ -334,6 +337,7 @@ func (d *Pool) Start() {
d.SrvAsync = append(d.SrvAsync, q) d.SrvAsync = append(d.SrvAsync, q)
} }
d.mu.Unlock() d.mu.Unlock()
goto rep goto rep
} }
} }
...@@ -390,7 +394,7 @@ func (d *Pool) prepare(sql string, param map[string]any) string { ...@@ -390,7 +394,7 @@ func (d *Pool) prepare(sql string, param map[string]any) string {
if tv == nil { if tv == nil {
sql = strings.ReplaceAll(sql, ":"+n+":", "NULL") sql = strings.ReplaceAll(sql, ":"+n+":", "NULL")
} else { } else {
sql = strings.ReplaceAll(sql, ":"+n+":", fmt.Sprintf("%v", *tv)) sql = strings.ReplaceAll(sql, ":"+n+":", strconv.Itoa(*tv))
} }
case *bool: case *bool:
if tv == nil { if tv == nil {
...@@ -406,7 +410,7 @@ func (d *Pool) prepare(sql string, param map[string]any) string { ...@@ -406,7 +410,7 @@ func (d *Pool) prepare(sql string, param map[string]any) string {
if tv == nil { if tv == nil {
sql = strings.ReplaceAll(sql, ":"+n+":", "NULL") sql = strings.ReplaceAll(sql, ":"+n+":", "NULL")
} else { } else {
sql = strings.ReplaceAll(sql, ":"+n+":", fmt.Sprintf("%v", *tv)) sql = strings.ReplaceAll(sql, ":"+n+":", strconv.FormatInt(*tv, 10))
} }
case *float64: case *float64:
if tv == nil { if tv == nil {
...@@ -420,9 +424,9 @@ func (d *Pool) prepare(sql string, param map[string]any) string { ...@@ -420,9 +424,9 @@ func (d *Pool) prepare(sql string, param map[string]any) string {
switch reflect.TypeOf(tv).Kind() { switch reflect.TypeOf(tv).Kind() {
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
sql = strings.ReplaceAll(sql, ":"+n+":", "'{"+strings.Trim(strings.Join(strings.Split(fmt.Sprint(tv), " "), ","), "[]")+"}'") sql = strings.ReplaceAll(sql, ":"+n+":", "'{"+strings.Trim(strings.Join(strings.Split(fmt.Sprint(tv), " "), ","), "[]")+"}'")
default:
sql = strings.ReplaceAll(sql, ":"+n+":", fmt.Sprintf("'%v'", tv))
} }
sql = strings.ReplaceAll(sql, ":"+n+":", fmt.Sprintf("'%v'", tv))
} }
} }
......
...@@ -2,6 +2,6 @@ ...@@ -2,6 +2,6 @@
package zdb package zdb
func logConnStat(conn *Conn) { func logConnStat(*Conn) {
} }
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
) )
type Stat struct { type Stat struct {
Id string ID string
Mode connMode Mode connMode
Host string Host string
Port uint16 Port uint16
...@@ -42,7 +42,7 @@ func (d *Pool) StatPool() []*Stat { ...@@ -42,7 +42,7 @@ func (d *Pool) StatPool() []*Stat {
} }
func (d *Pool) StatPoolTotal() *Stat { func (d *Pool) StatPoolTotal() *Stat {
m := &Stat{Id: "pool", Mode: connModePool} m := &Stat{ID: "pool", Mode: connModePool}
for _, s := range d.StatPool() { for _, s := range d.StatPool() {
m.AcquireCount += s.AcquireCount m.AcquireCount += s.AcquireCount
...@@ -66,7 +66,7 @@ func (d *Pool) Stat(q *Conn) *Stat { ...@@ -66,7 +66,7 @@ func (d *Pool) Stat(q *Conn) *Stat {
s := q.Stat() s := q.Stat()
return &Stat{ return &Stat{
Id: q.ToString(), ID: q.ToString(),
Mode: q.Mode, Mode: q.Mode,
Host: q.Config().ConnConfig.Host, Host: q.Config().ConnConfig.Host,
Port: q.Config().ConnConfig.Port, Port: q.Config().ConnConfig.Port,
......
...@@ -3,6 +3,7 @@ package zdebug ...@@ -3,6 +3,7 @@ package zdebug
import ( import (
"fmt" "fmt"
"git.barsukov.pro/barsukov/zgo/zutils" "git.barsukov.pro/barsukov/zgo/zutils"
"log"
"runtime" "runtime"
"strings" "strings"
"time" "time"
...@@ -21,7 +22,7 @@ func NewMonitor(secs int) { ...@@ -21,7 +22,7 @@ func NewMonitor(secs int) {
mem := ((rtm.HeapIdle - rtm.HeapReleased) + (rtm.Alloc + rtm.HeapInuse)) / 1e6 mem := ((rtm.HeapIdle - rtm.HeapReleased) + (rtm.Alloc + rtm.HeapInuse)) / 1e6
memMax = max(memMax, mem) memMax = max(memMax, mem)
fmt.Printf("ZDEBUG: MEM: %v; MEM_MAX: %v; OBJ: %v, GO: %v, GC: %v\n", log.Printf("ZDEBUG: MEM: %v; MEM_MAX: %v; OBJ: %v, GO: %v, GC: %v",
mem, mem,
memMax, memMax,
(rtm.Mallocs-rtm.Frees)/1e3, (rtm.Mallocs-rtm.Frees)/1e3,
......
...@@ -15,5 +15,5 @@ import ( ...@@ -15,5 +15,5 @@ import (
) )
func ZGO() { func ZGO() {
println("zgo")
} }
...@@ -31,8 +31,10 @@ func (c *ZClient) Do(r *http.Request, try int, sleepDur time.Duration) (resp *ht ...@@ -31,8 +31,10 @@ func (c *ZClient) Do(r *http.Request, try int, sleepDur time.Duration) (resp *ht
if i+1 < try { if i+1 < try {
time.Sleep(sleepDur) time.Sleep(sleepDur)
} }
continue continue
} }
break break
} }
...@@ -55,7 +57,7 @@ func (c *ZClient) GetEmpty(url string) error { ...@@ -55,7 +57,7 @@ func (c *ZClient) GetEmpty(url string) error {
return fmt.Errorf("status code: %v", resp.StatusCode) return fmt.Errorf("status code: %v", resp.StatusCode)
} }
func (c *ZClient) GetJson(target any, url string) error { func (c *ZClient) GetJSON(target any, url string) error {
var err error var err error
var resp *http.Response var resp *http.Response
......
...@@ -12,23 +12,30 @@ func ToBytes(obj any) ([]byte, error) { ...@@ -12,23 +12,30 @@ func ToBytes(obj any) ([]byte, error) {
} }
func ToString(obj any) (string, error) { func ToString(obj any) (string, error) {
if b, err := json.Marshal(obj); err != nil { var err error
var b []byte
if b, err = json.Marshal(obj); err != nil {
return "", err return "", err
} else {
return *(*string)(unsafe.Pointer(&b)), err
} }
return *(*string)(unsafe.Pointer(&b)), nil
} }
func ToStringSafe(obj any) (string, error) { func ToStringSafe(obj any) (string, error) {
if b, err := json.Marshal(obj); err != nil { var err error
var b []byte
if b, err = json.Marshal(obj); err != nil {
return "", err return "", err
} else {
return string(b), err
} }
return string(b), err
} }
func MustBytes(obj any) []byte { func MustBytes(obj any) []byte {
b, _ := ToBytes(obj) b, _ := ToBytes(obj)
return b return b
} }
...@@ -46,5 +53,6 @@ func MustStringSafe(obj any) string { ...@@ -46,5 +53,6 @@ func MustStringSafe(obj any) string {
func Pretty(i any) string { func Pretty(i any) string {
ss, _ := json.MarshalIndent(i, "", " ") ss, _ := json.MarshalIndent(i, "", " ")
return string(ss) return string(ss)
} }
...@@ -13,7 +13,7 @@ type ZQ struct { ...@@ -13,7 +13,7 @@ type ZQ struct {
ZQuit *zquit.ZQuit ZQuit *zquit.ZQuit
Jobs chan any Jobs chan any
workerId *zatomic.Int workerID *zatomic.Int
complete chan bool complete chan bool
} }
...@@ -24,7 +24,7 @@ func New(qtyWorkers int, buff int, fn func(int, any)) *ZQ { ...@@ -24,7 +24,7 @@ func New(qtyWorkers int, buff int, fn func(int, any)) *ZQ {
ZQuit: zquit.Default(), ZQuit: zquit.Default(),
Jobs: make(chan any, buff), Jobs: make(chan any, buff),
complete: make(chan bool), complete: make(chan bool),
workerId: zatomic.New(), workerID: zatomic.New(),
} }
q.AddWorker(qtyWorkers) q.AddWorker(qtyWorkers)
...@@ -71,6 +71,7 @@ func Default64(fn func(int, any)) *ZQ { ...@@ -71,6 +71,7 @@ func Default64(fn func(int, any)) *ZQ {
func (q *ZQ) WithZQuit(quit *zquit.ZQuit) *ZQ { func (q *ZQ) WithZQuit(quit *zquit.ZQuit) *ZQ {
q.ZQuit = quit q.ZQuit = quit
return q return q
} }
...@@ -88,14 +89,14 @@ func (q *ZQ) AddWorker(delta int) { ...@@ -88,14 +89,14 @@ func (q *ZQ) AddWorker(delta int) {
go func() { go func() {
defer q.WorkerCounter.Dec() defer q.WorkerCounter.Dec()
workerId := q.workerId.Inc() workerID := q.workerID.Inc()
for { for {
select { select {
case <-q.complete: case <-q.complete:
return return
case j := <-q.Jobs: case j := <-q.Jobs:
q.WorkerFn(workerId, j) q.WorkerFn(workerID, j)
q.ZQuit.Done() q.ZQuit.Done()
} }
} }
......
...@@ -86,6 +86,7 @@ func (q *ZQuit) Middleware(c *gin.Context) { ...@@ -86,6 +86,7 @@ func (q *ZQuit) Middleware(c *gin.Context) {
if q.isQuit { if q.isQuit {
c.Data(http.StatusServiceUnavailable, "text/plain", []byte("server shutdown")) c.Data(http.StatusServiceUnavailable, "text/plain", []byte("server shutdown"))
c.Abort() c.Abort()
return return
} }
......
...@@ -36,11 +36,13 @@ func ArrayRemove[T comparable](slice []T, s int) []T { ...@@ -36,11 +36,13 @@ func ArrayRemove[T comparable](slice []T, s int) []T {
func ArrayColumn[T comparable](input []map[string]T, columnKey string) []T { func ArrayColumn[T comparable](input []map[string]T, columnKey string) []T {
columns := make([]T, 0, len(input)) columns := make([]T, 0, len(input))
for _, val := range input { for _, val := range input {
if v, ok := val[columnKey]; ok { if v, ok := val[columnKey]; ok {
columns = append(columns, v) columns = append(columns, v)
} }
} }
return columns return columns
} }
...@@ -48,10 +50,12 @@ func ArrayCombine[K, V comparable](keys []K, values []V) map[K]V { ...@@ -48,10 +50,12 @@ func ArrayCombine[K, V comparable](keys []K, values []V) map[K]V {
if len(keys) != len(values) { if len(keys) != len(values) {
return nil return nil
} }
m := make(map[K]V, len(keys)) m := make(map[K]V, len(keys))
for i, v := range keys { for i, v := range keys {
m[v] = values[i] m[v] = values[i]
} }
return m return m
} }
...@@ -62,6 +66,7 @@ func ArrayDiff[T comparable](array1, array2 []T) []T { ...@@ -62,6 +66,7 @@ func ArrayDiff[T comparable](array1, array2 []T) []T {
res = append(res, v) res = append(res, v)
} }
} }
return res return res
} }
...@@ -72,6 +77,7 @@ func ArrayIntersect[T comparable](array1, array2 []T) []T { ...@@ -72,6 +77,7 @@ func ArrayIntersect[T comparable](array1, array2 []T) []T {
res = append(res, v) res = append(res, v)
} }
} }
return res return res
} }
...@@ -89,13 +95,16 @@ func ArrayFlip(input any) any { ...@@ -89,13 +95,16 @@ func ArrayFlip(input any) any {
for i := 0; i < val.Len(); i++ { for i := 0; i < val.Len(); i++ {
res[val.Index(i).Interface()] = i res[val.Index(i).Interface()] = i
} }
return res return res
case reflect.Map: case reflect.Map:
for _, k := range val.MapKeys() { for _, k := range val.MapKeys() {
res[val.MapIndex(k).Interface()] = k.Interface() res[val.MapIndex(k).Interface()] = k.Interface()
} }
return res return res
} }
return nil return nil
} }
...@@ -107,12 +116,14 @@ func ArrayKeys(input any) any { ...@@ -107,12 +116,14 @@ func ArrayKeys(input any) any {
if val.Len() == 0 { if val.Len() == 0 {
return nil return nil
} }
switch val.Kind() { switch val.Kind() {
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
var res []int var res []int
for i := 0; i < val.Len(); i++ { for i := 0; i < val.Len(); i++ {
res = append(res, i) res = append(res, i)
} }
return res return res
case reflect.Map: case reflect.Map:
var res []string var res []string
...@@ -122,8 +133,10 @@ func ArrayKeys(input any) any { ...@@ -122,8 +133,10 @@ func ArrayKeys(input any) any {
sort.SliceStable(res, func(i, j int) bool { sort.SliceStable(res, func(i, j int) bool {
return res[i] < res[j] return res[i] < res[j]
}) })
return res return res
} }
return nil return nil
} }
...@@ -133,6 +146,7 @@ func ArrayKeyExists[K, V comparable](k K, m map[K]V) bool { ...@@ -133,6 +146,7 @@ func ArrayKeyExists[K, V comparable](k K, m map[K]V) bool {
func KeyExists[K, V comparable](k K, m map[K]V) bool { func KeyExists[K, V comparable](k K, m map[K]V) bool {
_, ok := m[k] _, ok := m[k]
return ok return ok
} }
...@@ -158,6 +172,7 @@ func ArrayFilter(input any, callback func(any) bool) any { ...@@ -158,6 +172,7 @@ func ArrayFilter(input any, callback func(any) bool) any {
res = append(res, v) res = append(res, v)
} }
} }
return res return res
case reflect.Map: case reflect.Map:
res := make(map[any]any) res := make(map[any]any)
...@@ -167,6 +182,7 @@ func ArrayFilter(input any, callback func(any) bool) any { ...@@ -167,6 +182,7 @@ func ArrayFilter(input any, callback func(any) bool) any {
res[k.Interface()] = v res[k.Interface()] = v
} }
} }
return res return res
} }
...@@ -189,6 +205,7 @@ func ArrayPad[T comparable](array []T, size int, value T) []T { ...@@ -189,6 +205,7 @@ func ArrayPad[T comparable](array []T, size int, value T) []T {
if size > 0 { if size > 0 {
return append(array, tmp...) return append(array, tmp...)
} }
return append(tmp, array...) return append(tmp, array...)
} }
...@@ -210,6 +227,7 @@ func ArrayPush[T comparable](s *[]T, elements ...T) int { ...@@ -210,6 +227,7 @@ func ArrayPush[T comparable](s *[]T, elements ...T) int {
return 0 return 0
} }
*s = append(*s, elements...) *s = append(*s, elements...)
return len(*s) return len(*s)
} }
...@@ -230,6 +248,7 @@ func ArrayUnshift[T comparable](s *[]T, elements ...T) int { ...@@ -230,6 +248,7 @@ func ArrayUnshift[T comparable](s *[]T, elements ...T) int {
return 0 return 0
} }
*s = append(elements, *s...) *s = append(elements, *s...)
return len(*s) return len(*s)
} }
...@@ -237,6 +256,7 @@ func ArrayReverse[T comparable](s []T) []T { ...@@ -237,6 +256,7 @@ func ArrayReverse[T comparable](s []T) []T {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i] s[i], s[j] = s[j], s[i]
} }
return s return s
} }
...@@ -248,6 +268,7 @@ func ArraySlice[T comparable](array []T, offset, length uint) []T { ...@@ -248,6 +268,7 @@ func ArraySlice[T comparable](array []T, offset, length uint) []T {
if end < uint(len(array)) { if end < uint(len(array)) {
return array[offset:end] return array[offset:end]
} }
return array[offset:] return array[offset:]
} }
......
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