mirror of
https://github.com/nabbar/golib.git
synced 2025-10-07 08:40:51 +08:00
Package Socket/Server:
- fix UDP as kernel SOCKGRAM - add unixgram as kernel SOCKGRAM for local socket file - optimize parallel statment - optimize code / minor bugs fix - add IsRunning function - add timeout shutdown based on periodicly check of IsRunning - fix close channel before send empty struct to stop process - optimize clean shutdown - optimize connection process to use less resources and lock pipeline less time
This commit is contained in:
@@ -30,10 +30,12 @@
|
||||
package unix
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
libptc "github.com/nabbar/golib/network/protocol"
|
||||
|
||||
@@ -56,6 +58,7 @@ type srv struct {
|
||||
h *atomic.Value // handler
|
||||
c *atomic.Value // chan []byte
|
||||
s *atomic.Value // chan struct{}
|
||||
r *atomic.Bool // is Running
|
||||
|
||||
fe *atomic.Value // function error
|
||||
fi *atomic.Value // function info
|
||||
@@ -67,6 +70,10 @@ type srv struct {
|
||||
sg *atomic.Int32 // file unix group perm
|
||||
}
|
||||
|
||||
func (o *srv) IsRunning() bool {
|
||||
return o.r.Load()
|
||||
}
|
||||
|
||||
func (o *srv) Done() <-chan struct{} {
|
||||
s := o.s.Load()
|
||||
if s != nil {
|
||||
@@ -76,14 +83,47 @@ func (o *srv) Done() <-chan struct{} {
|
||||
return closedChanStruct
|
||||
}
|
||||
|
||||
func (o *srv) Shutdown() {
|
||||
func (o *srv) Close() error {
|
||||
return o.Shutdown()
|
||||
}
|
||||
|
||||
func (o *srv) Shutdown() error {
|
||||
if o == nil {
|
||||
return
|
||||
return ErrInvalidInstance
|
||||
}
|
||||
|
||||
s := o.s.Load()
|
||||
if s != nil {
|
||||
o.s.Store(nil)
|
||||
if c, k := s.(chan struct{}); k {
|
||||
c <- struct{}{}
|
||||
o.s.Store(c)
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
tck = time.NewTicker(100 * time.Millisecond)
|
||||
ctx, cnl = context.WithTimeout(context.Background(), 25*time.Second)
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if s != nil {
|
||||
o.s.Store(closedChanStruct)
|
||||
}
|
||||
|
||||
tck.Stop()
|
||||
cnl()
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ErrShutdownTimeout
|
||||
case <-tck.C:
|
||||
if o.IsRunning() {
|
||||
continue
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user