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
92afcbbd
Commit
92afcbbd
authored
Nov 18, 2023
by
Vladimir Barsukov
Browse files
add quit pool
parent
b9d319f8
Changes
2
Show whitespace changes
Inline
Side-by-side
zquit/pool.go
0 → 100644
View file @
92afcbbd
package
zquit
import
(
"os"
"os/signal"
"syscall"
"time"
)
type
Pool
struct
{
PostWaitDur
time
.
Duration
items
[]
*
ZQuit
}
func
NewPool
(
postWaitDur
time
.
Duration
)
*
Pool
{
return
&
Pool
{
PostWaitDur
:
postWaitDur
,
items
:
make
([]
*
ZQuit
,
0
),
}
}
func
DefaultPool
()
*
Pool
{
return
NewPool
(
time
.
Second
)
}
func
(
p
*
Pool
)
Add
(
z
*
ZQuit
)
{
p
.
items
=
append
(
p
.
items
,
z
)
}
func
(
p
*
Pool
)
PrintStat
()
{
for
_
,
i
:=
range
p
.
items
{
i
.
PrintStat
()
}
}
func
(
p
*
Pool
)
Wait
()
{
for
_
,
i
:=
range
p
.
items
{
i
.
Wait
()
}
}
func
(
p
*
Pool
)
WaitInterruptPrePost
(
pre
func
(),
post
func
())
{
c
:=
make
(
chan
os
.
Signal
,
1
)
signal
.
Notify
(
c
,
os
.
Interrupt
,
syscall
.
SIGTERM
)
<-
c
if
pre
!=
nil
{
pre
()
}
p
.
Wait
()
time
.
Sleep
(
p
.
PostWaitDur
)
if
post
!=
nil
{
post
()
}
}
func
(
p
*
Pool
)
WaitInterrupt
()
{
p
.
WaitInterruptPrePost
(
nil
,
nil
)
}
zquit/zquit.go
View file @
92afcbbd
...
@@ -3,23 +3,38 @@ package zquit
...
@@ -3,23 +3,38 @@ package zquit
import
(
import
(
"git.barsukov.pro/barsukov/zgo/zwg"
"git.barsukov.pro/barsukov/zgo/zwg"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"log"
"net/http"
"net/http"
"os"
"os"
"os/signal"
"os/signal"
"syscall"
"syscall"
"time"
)
)
type
ZQuit
struct
{
type
ZQuit
struct
{
Name
string
PostWaitDur
time
.
Duration
isQuit
bool
isQuit
bool
wg
*
zwg
.
Zwg
wg
*
zwg
.
Zwg
}
}
func
New
()
*
ZQuit
{
func
New
(
name
string
,
postWaitDur
time
.
Duration
)
*
ZQuit
{
return
&
ZQuit
{
return
&
ZQuit
{
Name
:
name
,
PostWaitDur
:
postWaitDur
,
wg
:
zwg
.
New
(),
wg
:
zwg
.
New
(),
}
}
}
}
func
Default
(
name
...
string
)
*
ZQuit
{
if
len
(
name
)
==
0
{
name
=
[]
string
{
"MAIN"
}
}
return
New
(
name
[
0
],
time
.
Second
)
}
func
(
q
*
ZQuit
)
Inc
()
{
func
(
q
*
ZQuit
)
Inc
()
{
q
.
wg
.
Inc
()
q
.
wg
.
Inc
()
}
}
...
@@ -47,16 +62,16 @@ func (q *ZQuit) Val() int {
...
@@ -47,16 +62,16 @@ func (q *ZQuit) Val() int {
}
}
func
(
q
*
ZQuit
)
WaitInterruptPrePost
(
pre
func
(),
post
func
())
{
func
(
q
*
ZQuit
)
WaitInterruptPrePost
(
pre
func
(),
post
func
())
{
if
pre
!=
nil
{
pre
()
}
c
:=
make
(
chan
os
.
Signal
,
1
)
c
:=
make
(
chan
os
.
Signal
,
1
)
signal
.
Notify
(
c
,
os
.
Interrupt
,
syscall
.
SIGTERM
)
signal
.
Notify
(
c
,
os
.
Interrupt
,
syscall
.
SIGTERM
)
<-
c
<-
c
if
pre
!=
nil
{
pre
()
}
q
.
Wait
()
q
.
Wait
()
time
.
Sleep
(
q
.
PostWaitDur
)
if
post
!=
nil
{
if
post
!=
nil
{
post
()
post
()
...
@@ -70,6 +85,7 @@ func (q *ZQuit) WaitInterrupt() {
...
@@ -70,6 +85,7 @@ func (q *ZQuit) WaitInterrupt() {
func
(
q
*
ZQuit
)
Middleware
(
c
*
gin
.
Context
)
{
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
()
return
return
}
}
...
@@ -79,6 +95,15 @@ func (q *ZQuit) Middleware(c *gin.Context) {
...
@@ -79,6 +95,15 @@ func (q *ZQuit) Middleware(c *gin.Context) {
c
.
Next
()
c
.
Next
()
}
}
func
(
q
*
ZQuit
)
PrintStat
()
{
go
func
()
{
for
{
<-
time
.
After
(
time
.
Second
)
log
.
Printf
(
"Z_QUIT: LOCKS[%s]: %v"
,
q
.
Name
,
q
.
Val
())
}
}()
}
func
(
q
*
ZQuit
)
Shutdown
()
{
func
(
q
*
ZQuit
)
Shutdown
()
{
q
.
isQuit
=
true
q
.
isQuit
=
true
}
}
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