Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Vladimir Barsukov
zGo
Commits
ffafeb48
Commit
ffafeb48
authored
Nov 19, 2023
by
Vladimir Barsukov
Browse files
lint
parent
86d85eca
Changes
12
Hide whitespace changes
Inline
Side-by-side
.golangci.yml
0 → 100644
View file @
ffafeb48
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
zdb/any.go
View file @
ffafeb48
...
...
@@ -49,16 +49,17 @@ func (d *Pool) qAny(q *Conn, dst any, sql string, args ...any) error {
v
,
_
:=
rows
.
Values
()
for
i
,
f
:=
range
desc
{
if
v
[
i
]
==
nil
{
switch
{
case
v
[
i
]
==
nil
:
r
[
f
.
Name
]
=
nil
}
else
if
f
.
DataTypeOID
==
pgtype
.
NumericOID
{
case
f
.
DataTypeOID
==
pgtype
.
NumericOID
:
ff
,
_
:=
v
[
i
]
.
(
pgtype
.
Numeric
)
.
Float64Value
()
r
[
f
.
Name
]
=
ff
.
Float64
}
else
if
f
.
DataTypeOID
==
pgtype
.
UUIDOID
{
case
f
.
DataTypeOID
==
pgtype
.
UUIDOID
:
bb
:=
v
[
i
]
.
([
16
]
byte
)
u
,
_
:=
uuid
.
FromBytes
(
bb
[
:
])
r
[
f
.
Name
]
=
u
.
String
()
}
else
{
default
:
r
[
f
.
Name
]
=
v
[
i
]
}
}
...
...
zdb/pool.go
View file @
ffafeb48
...
...
@@ -6,6 +6,7 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"log"
"reflect"
"strconv"
"strings"
"sync"
"sync/atomic"
...
...
@@ -174,7 +175,7 @@ func (d *Pool) newConn(mode connMode, pgConnString string) (q *Conn, err error)
func
(
d
*
Pool
)
least
(
s
[]
*
Conn
)
*
Conn
{
var
out
*
Conn
var
m
float64
=
0
var
m
float64
for
i
,
conn
:=
range
s
{
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)
if
err
:=
f
(
q
,
dst
);
err
!=
nil
{
if
q
.
Mode
==
ConnModeMaster
{
return
err
}
else
{
}
if
try
<
d
.
TryOnError
&&
contains
(
err
.
Error
(),
d
.
ContinuesTry
)
{
try
++
d
.
logger
.
Printf
(
"ZDB_EXEC_WRAPPER_REPEAT_ERR: SRV: %s TRY: %d; %s"
,
q
.
ToString
(),
try
,
err
.
Error
())
if
try
<
d
.
TryOnError
&&
contains
(
err
.
Error
(),
d
.
ContinuesTry
)
{
try
++
d
.
logger
.
Printf
(
"ZDB_EXEC_WRAPPER_REPEAT_ERR: SRV: %s TRY: %d; %s"
,
q
.
ToString
(),
try
,
err
.
Error
())
time
.
Sleep
(
d
.
TryOnSleep
)
goto
repeat
}
time
.
Sleep
(
d
.
TryOnSleep
)
if
contains
(
err
.
Error
(),
d
.
Continues
)
{
d
.
setNotAliveConn
(
q
)
d
.
logger
.
Printf
(
"ZDB_EXEC_WRAPPER_ERR: SRV: %s; %s"
,
q
.
ToString
(),
err
.
Error
())
continue
}
else
{
return
err
}
goto
repeat
}
if
contains
(
err
.
Error
(),
d
.
Continues
)
{
d
.
setNotAliveConn
(
q
)
d
.
logger
.
Printf
(
"ZDB_EXEC_WRAPPER_ERR: SRV: %s; %s"
,
q
.
ToString
(),
err
.
Error
())
continue
}
return
err
}
return
nil
...
...
@@ -298,6 +300,7 @@ func (d *Pool) setNotAliveConn(conn *Conn) {
conn
.
Alive
=
false
d
.
notAliveConns
=
append
(
d
.
notAliveConns
,
conn
)
d
.
SrvSync
=
remove
(
d
.
SrvSync
,
i
)
return
}
}
...
...
@@ -334,6 +337,7 @@ func (d *Pool) Start() {
d
.
SrvAsync
=
append
(
d
.
SrvAsync
,
q
)
}
d
.
mu
.
Unlock
()
goto
rep
}
}
...
...
@@ -390,7 +394,7 @@ func (d *Pool) prepare(sql string, param map[string]any) string {
if
tv
==
nil
{
sql
=
strings
.
ReplaceAll
(
sql
,
":"
+
n
+
":"
,
"NULL"
)
}
else
{
sql
=
strings
.
ReplaceAll
(
sql
,
":"
+
n
+
":"
,
fmt
.
Sprintf
(
"%v"
,
*
tv
))
sql
=
strings
.
ReplaceAll
(
sql
,
":"
+
n
+
":"
,
strconv
.
Itoa
(
*
tv
))
}
case
*
bool
:
if
tv
==
nil
{
...
...
@@ -406,7 +410,7 @@ func (d *Pool) prepare(sql string, param map[string]any) string {
if
tv
==
nil
{
sql
=
strings
.
ReplaceAll
(
sql
,
":"
+
n
+
":"
,
"NULL"
)
}
else
{
sql
=
strings
.
ReplaceAll
(
sql
,
":"
+
n
+
":"
,
fmt
.
Sprintf
(
"%v"
,
*
tv
))
sql
=
strings
.
ReplaceAll
(
sql
,
":"
+
n
+
":"
,
strconv
.
FormatInt
(
*
tv
,
10
))
}
case
*
float64
:
if
tv
==
nil
{
...
...
@@ -420,9 +424,9 @@ func (d *Pool) prepare(sql string, param map[string]any) string {
switch
reflect
.
TypeOf
(
tv
)
.
Kind
()
{
case
reflect
.
Slice
,
reflect
.
Array
:
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
))
}
}
...
...
zdb/prod.go
View file @
ffafeb48
...
...
@@ -2,6 +2,6 @@
package
zdb
func
logConnStat
(
conn
*
Conn
)
{
func
logConnStat
(
*
Conn
)
{
}
zdb/stats.go
View file @
ffafeb48
...
...
@@ -5,7 +5,7 @@ import (
)
type
Stat
struct
{
I
d
string
I
D
string
Mode
connMode
Host
string
Port
uint16
...
...
@@ -42,7 +42,7 @@ func (d *Pool) StatPool() []*Stat {
}
func
(
d
*
Pool
)
StatPoolTotal
()
*
Stat
{
m
:=
&
Stat
{
I
d
:
"pool"
,
Mode
:
connModePool
}
m
:=
&
Stat
{
I
D
:
"pool"
,
Mode
:
connModePool
}
for
_
,
s
:=
range
d
.
StatPool
()
{
m
.
AcquireCount
+=
s
.
AcquireCount
...
...
@@ -66,7 +66,7 @@ func (d *Pool) Stat(q *Conn) *Stat {
s
:=
q
.
Stat
()
return
&
Stat
{
I
d
:
q
.
ToString
(),
I
D
:
q
.
ToString
(),
Mode
:
q
.
Mode
,
Host
:
q
.
Config
()
.
ConnConfig
.
Host
,
Port
:
q
.
Config
()
.
ConnConfig
.
Port
,
...
...
zdebug/zdebug.go
View file @
ffafeb48
...
...
@@ -3,6 +3,7 @@ package zdebug
import
(
"fmt"
"git.barsukov.pro/barsukov/zgo/zutils"
"log"
"runtime"
"strings"
"time"
...
...
@@ -21,7 +22,7 @@ func NewMonitor(secs int) {
mem
:=
((
rtm
.
HeapIdle
-
rtm
.
HeapReleased
)
+
(
rtm
.
Alloc
+
rtm
.
HeapInuse
))
/
1e6
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
,
memMax
,
(
rtm
.
Mallocs
-
rtm
.
Frees
)
/
1e3
,
...
...
zgo.go
View file @
ffafeb48
...
...
@@ -15,5 +15,5 @@ import (
)
func
ZGO
()
{
println
(
"zgo"
)
}
zhttp/zhttp.go
View file @
ffafeb48
...
...
@@ -31,8 +31,10 @@ func (c *ZClient) Do(r *http.Request, try int, sleepDur time.Duration) (resp *ht
if
i
+
1
<
try
{
time
.
Sleep
(
sleepDur
)
}
continue
}
break
}
...
...
@@ -55,7 +57,7 @@ func (c *ZClient) GetEmpty(url string) error {
return
fmt
.
Errorf
(
"status code: %v"
,
resp
.
StatusCode
)
}
func
(
c
*
ZClient
)
GetJ
son
(
target
any
,
url
string
)
error
{
func
(
c
*
ZClient
)
GetJ
SON
(
target
any
,
url
string
)
error
{
var
err
error
var
resp
*
http
.
Response
...
...
zjson/json.go
View file @
ffafeb48
...
...
@@ -12,23 +12,30 @@ func ToBytes(obj any) ([]byte, 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
}
else
{
return
*
(
*
string
)(
unsafe
.
Pointer
(
&
b
)),
err
}
return
*
(
*
string
)(
unsafe
.
Pointer
(
&
b
)),
nil
}
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
}
else
{
return
string
(
b
),
err
}
return
string
(
b
),
err
}
func
MustBytes
(
obj
any
)
[]
byte
{
b
,
_
:=
ToBytes
(
obj
)
return
b
}
...
...
@@ -46,5 +53,6 @@ func MustStringSafe(obj any) string {
func
Pretty
(
i
any
)
string
{
ss
,
_
:=
json
.
MarshalIndent
(
i
,
""
,
" "
)
return
string
(
ss
)
}
zq/zq.go
View file @
ffafeb48
...
...
@@ -13,7 +13,7 @@ type ZQ struct {
ZQuit
*
zquit
.
ZQuit
Jobs
chan
any
workerI
d
*
zatomic
.
Int
workerI
D
*
zatomic
.
Int
complete
chan
bool
}
...
...
@@ -24,7 +24,7 @@ func New(qtyWorkers int, buff int, fn func(int, any)) *ZQ {
ZQuit
:
zquit
.
Default
(),
Jobs
:
make
(
chan
any
,
buff
),
complete
:
make
(
chan
bool
),
workerI
d
:
zatomic
.
New
(),
workerI
D
:
zatomic
.
New
(),
}
q
.
AddWorker
(
qtyWorkers
)
...
...
@@ -71,6 +71,7 @@ func Default64(fn func(int, any)) *ZQ {
func
(
q
*
ZQ
)
WithZQuit
(
quit
*
zquit
.
ZQuit
)
*
ZQ
{
q
.
ZQuit
=
quit
return
q
}
...
...
@@ -88,14 +89,14 @@ func (q *ZQ) AddWorker(delta int) {
go
func
()
{
defer
q
.
WorkerCounter
.
Dec
()
workerI
d
:=
q
.
workerI
d
.
Inc
()
workerI
D
:=
q
.
workerI
D
.
Inc
()
for
{
select
{
case
<-
q
.
complete
:
return
case
j
:=
<-
q
.
Jobs
:
q
.
WorkerFn
(
workerI
d
,
j
)
q
.
WorkerFn
(
workerI
D
,
j
)
q
.
ZQuit
.
Done
()
}
}
...
...
zquit/zquit.go
View file @
ffafeb48
...
...
@@ -86,6 +86,7 @@ func (q *ZQuit) Middleware(c *gin.Context) {
if
q
.
isQuit
{
c
.
Data
(
http
.
StatusServiceUnavailable
,
"text/plain"
,
[]
byte
(
"server shutdown"
))
c
.
Abort
()
return
}
...
...
zutils/array.go
View file @
ffafeb48
...
...
@@ -36,11 +36,13 @@ func ArrayRemove[T comparable](slice []T, s int) []T {
func
ArrayColumn
[
T
comparable
](
input
[]
map
[
string
]
T
,
columnKey
string
)
[]
T
{
columns
:=
make
([]
T
,
0
,
len
(
input
))
for
_
,
val
:=
range
input
{
if
v
,
ok
:=
val
[
columnKey
];
ok
{
columns
=
append
(
columns
,
v
)
}
}
return
columns
}
...
...
@@ -48,10 +50,12 @@ func ArrayCombine[K, V comparable](keys []K, values []V) map[K]V {
if
len
(
keys
)
!=
len
(
values
)
{
return
nil
}
m
:=
make
(
map
[
K
]
V
,
len
(
keys
))
for
i
,
v
:=
range
keys
{
m
[
v
]
=
values
[
i
]
}
return
m
}
...
...
@@ -62,6 +66,7 @@ func ArrayDiff[T comparable](array1, array2 []T) []T {
res
=
append
(
res
,
v
)
}
}
return
res
}
...
...
@@ -72,6 +77,7 @@ func ArrayIntersect[T comparable](array1, array2 []T) []T {
res
=
append
(
res
,
v
)
}
}
return
res
}
...
...
@@ -89,13 +95,16 @@ func ArrayFlip(input any) any {
for
i
:=
0
;
i
<
val
.
Len
();
i
++
{
res
[
val
.
Index
(
i
)
.
Interface
()]
=
i
}
return
res
case
reflect
.
Map
:
for
_
,
k
:=
range
val
.
MapKeys
()
{
res
[
val
.
MapIndex
(
k
)
.
Interface
()]
=
k
.
Interface
()
}
return
res
}
return
nil
}
...
...
@@ -107,12 +116,14 @@ func ArrayKeys(input any) any {
if
val
.
Len
()
==
0
{
return
nil
}
switch
val
.
Kind
()
{
case
reflect
.
Slice
,
reflect
.
Array
:
var
res
[]
int
for
i
:=
0
;
i
<
val
.
Len
();
i
++
{
res
=
append
(
res
,
i
)
}
return
res
case
reflect
.
Map
:
var
res
[]
string
...
...
@@ -122,8 +133,10 @@ func ArrayKeys(input any) any {
sort
.
SliceStable
(
res
,
func
(
i
,
j
int
)
bool
{
return
res
[
i
]
<
res
[
j
]
})
return
res
}
return
nil
}
...
...
@@ -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
{
_
,
ok
:=
m
[
k
]
return
ok
}
...
...
@@ -158,6 +172,7 @@ func ArrayFilter(input any, callback func(any) bool) any {
res
=
append
(
res
,
v
)
}
}
return
res
case
reflect
.
Map
:
res
:=
make
(
map
[
any
]
any
)
...
...
@@ -167,6 +182,7 @@ func ArrayFilter(input any, callback func(any) bool) any {
res
[
k
.
Interface
()]
=
v
}
}
return
res
}
...
...
@@ -189,6 +205,7 @@ func ArrayPad[T comparable](array []T, size int, value T) []T {
if
size
>
0
{
return
append
(
array
,
tmp
...
)
}
return
append
(
tmp
,
array
...
)
}
...
...
@@ -210,6 +227,7 @@ func ArrayPush[T comparable](s *[]T, elements ...T) int {
return
0
}
*
s
=
append
(
*
s
,
elements
...
)
return
len
(
*
s
)
}
...
...
@@ -230,6 +248,7 @@ func ArrayUnshift[T comparable](s *[]T, elements ...T) int {
return
0
}
*
s
=
append
(
elements
,
*
s
...
)
return
len
(
*
s
)
}
...
...
@@ -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
{
s
[
i
],
s
[
j
]
=
s
[
j
],
s
[
i
]
}
return
s
}
...
...
@@ -248,6 +268,7 @@ func ArraySlice[T comparable](array []T, offset, length uint) []T {
if
end
<
uint
(
len
(
array
))
{
return
array
[
offset
:
end
]
}
return
array
[
offset
:
]
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment