Skip to content

Commit

Permalink
🕜 perf: process daemon is executed by default by a non-root user (Win…
Browse files Browse the repository at this point in the history
…dows does not support this)
  • Loading branch information
sohaha committed Jan 2, 2024
1 parent 4cab429 commit 8acbf86
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 94 deletions.
17 changes: 7 additions & 10 deletions zcli/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,18 @@ func LaunchServiceRun(name string, description string, fn func(), config ...*dae

// LaunchService Launch Service
func LaunchService(name string, description string, fn func(), config ...*daemon.Config) (daemon.ServiceIface, error) {

once.Do(func() {
var daemonConfig *daemon.Config
daemonConfig := &daemon.Config{
Name: name,
Description: description,
Options: map[string]interface{}{
"UserService": true,
},
}
if len(config) > 0 {
daemonConfig = config[0]
daemonConfig.Name = name
daemonConfig.Description = description
} else {
daemonConfig = &daemon.Config{
Name: name,
Description: description,
Options: map[string]interface{}{
// "UserService": true,
},
}
}

// The file path is redirected to the current execution file path
Expand Down
2 changes: 1 addition & 1 deletion zcli/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type testCmd struct {
flag1 *bool
flag2 *int
flag3 *string
run bool
tt *zls.TestUtil
run bool
}

func (cmd *testCmd) Flags(sub *Subcommand) {
Expand Down
4 changes: 3 additions & 1 deletion zhttp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,14 +524,16 @@ func (m *multipartHelper) upload(req *http.Request, upload func(io.Writer, io.Re

func (m *multipartHelper) Upload(req *http.Request) {
bodyBuf := zutil.GetBuff(1048576)
defer zutil.PutBuff(bodyBuf)
bodyWriter := multipart.NewWriter(bodyBuf)

m.upload(req, nil, bodyWriter)
_ = bodyWriter.Close()

req.Header.Set(textContentType, bodyWriter.FormDataContentType())
b := bytes.NewReader(bodyBuf.Bytes())

zutil.PutBuff(bodyBuf)

req.Body = ioutil.NopCloser(b)
req.ContentLength = int64(b.Len())
}
Expand Down
2 changes: 1 addition & 1 deletion znet/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ func requestLog(c *Context) {
var status string
end := time.Now()
statusCode := zutil.GetBuff()
defer zutil.PutBuff(statusCode)
latency := end.Sub(c.startTime)
code := c.prevData.Code.Load()
statusCode.WriteString(" ")
statusCode.WriteString(strconv.FormatInt(int64(code), 10))
statusCode.WriteString(" ")
s := statusCode.String()
zutil.PutBuff(statusCode)
switch {
case code >= 200 && code <= 299:
status = c.Log.ColorBackgroundWrap(zlog.ColorBlack, zlog.ColorGreen, s)
Expand Down
2 changes: 1 addition & 1 deletion znet/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var Utils = utils{
}

const (
defaultPattern = `[^\/.]+`
defaultPattern = `[^\/]+`
idPattern = `[\d]+`
idKey = `id`
allPattern = `.*`
Expand Down
11 changes: 11 additions & 0 deletions znet/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ func TestURLMatchAndParse(t *testing.T) {
tt.EqualTrue(ok)
tt.Equal(1, len(match))

match, ok = Utils.URLMatchAndParse("/hi/1.1/123", "/:name/:code/:id")
t.Log(match)
tt.EqualTrue(ok)
tt.Equal(3, len(match))

match, ok = Utils.URLMatchAndParse("/aaa/hi", "/:name/:*")
t.Log(match)
tt.EqualTrue(ok)
tt.Equal(2, len(match))

}

func Test_parsPattern(t *testing.T) {
Expand All @@ -58,6 +64,11 @@ func Test_parsPattern(t *testing.T) {
tt.EqualExit("([\\w\\d-]+)", p)
tt.EqualExit([]string{"name"}, s)

p, s = parsePattern([]string{":name", ":id"}, "/")
tt.Log(p, s)
tt.EqualExit("/([^\\/]+)/([\\d]+)", p)
tt.EqualExit([]string{"name", "id"}, s)

p, s = parsePattern([]string{"{p:[\\w\\d-]+}.pth"}, "")
tt.Log(p, s)
tt.EqualExit("([\\w\\d-]+).pth", p)
Expand Down
29 changes: 11 additions & 18 deletions zpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func New(size int, max ...int) *WorkPool {
}
maxIdle := minIdle
if len(max) > 0 && max[0] > 0 {
max := uint(max[0])
if max > maxIdle {
maxIdle = max
m := uint(max[0])
if m > maxIdle {
maxIdle = m
}
}

Expand Down Expand Up @@ -103,10 +103,10 @@ func (wp *WorkPool) PanicFunc(handler PanicFunc) {
}

func (wp *WorkPool) do(cxt context.Context, fn taskfn, param []interface{}) error {
wp.activeNum.Add(1)
if wp.IsClosed() {
return ErrPoolClosed
}
wp.activeNum.Add(1)
wp.mu.Lock()
run := func(w *worker) {
if fn != nil {
Expand All @@ -131,29 +131,22 @@ func (wp *WorkPool) do(cxt context.Context, fn taskfn, param []interface{}) erro
default:
switch {
case uint(wp.usedNum.Load()) >= wp.minIdle:
wp.mu.Unlock()
// todo 超时处理
// 需要启动队列功能了
select {
case <-cxt.Done():
wp.mu.Lock()
if uint(wp.usedNum.Load()) >= wp.maxIdle {
wp.mu.Unlock()
return ErrWaitTimeout
}
if uint(wp.usedNum.Load()) < wp.maxIdle {
w := add()
run(w)
return nil
// 尝试扩大容量?
}
wp.mu.Unlock()
select {
case <-cxt.Done():
wp.activeNum.Sub(1)
return ErrWaitTimeout
case w := <-wp.queue:
if w != nil {
run(w)
} else {
return ErrPoolClosed
}
// default:
// todo 进入队列
// return nil
}
case uint(wp.usedNum.Load()) < wp.minIdle:
w := add()
Expand Down
17 changes: 7 additions & 10 deletions zpool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,10 @@ func TestPoolCap(t *testing.T) {
tt.EqualExit(uint(0), p.Cap())

newSize := 5
go func() {
tt.EqualExit(uint(0), p.Cap())
time.Sleep(time.Second)
p.Continue(newSize)
tt.EqualExit(uint(newSize), p.Cap())
}()
p.Continue(newSize)
tt.EqualExit(uint(newSize), p.Cap())

restarSum := 6
restarSum := 7
g.Add(restarSum)

for i := 0; i < restarSum; i++ {
Expand All @@ -149,13 +145,13 @@ func TestPoolCap(t *testing.T) {
}()
}
g.Wait()
tt.EqualExit(uint(newSize), p.Cap())
tt.EqualExit(uint(restarSum), p.Cap())

p.Continue(1000)
tt.EqualExit(uint(maxsize), p.Cap())

p.Close()
p.Continue(1000)
tt.EqualExit(uint(0), p.Cap())
}

func TestPoolPanicFunc(t *testing.T) {
Expand Down Expand Up @@ -223,12 +219,13 @@ func TestPoolTimeout(t *testing.T) {
}, time.Second/3)
t.Log(err)
if v > 0 {
tt.EqualTrue(err == zpool.ErrWaitTimeout)
tt.Equal(err, zpool.ErrWaitTimeout)
}
if err == zpool.ErrWaitTimeout {
t.Log(v)
}
}
p.Wait()
}

func TestPoolAuto(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions zreflect/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ type (

DemoSt struct {
Date2 time.Time
Any interface{}
any interface{}
Child3 *DemoChildSt
Remark string `json:"remark"`
note string
Any interface{}
any interface{}
Name string `json:"username"`
Slice [][]string
pri string
Hobby []string
Slice [][]string
Child struct {
Title string `json:"child_user_title"`
DemoChild2 Child2 `json:"demo_child_2"`
Expand All @@ -37,7 +38,6 @@ type (
child4 DemoChildSt
Age uint
Lovely bool
pri string
}
TestSt struct {
Name string
Expand Down
2 changes: 1 addition & 1 deletion ztype/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (

type Conver struct {
MatchName func(mapKey, fieldName string) bool
ConvHook func(i reflect.Value, o reflect.Type) (reflect.Value, error)
TagName string
ZeroFields bool
Squash bool
Deep bool
Merge bool
ConvHook func(i reflect.Value, o reflect.Type) (reflect.Value, error)
}

var conv = Conver{TagName: tagName, Squash: true, MatchName: strings.EqualFold}
Expand Down
14 changes: 7 additions & 7 deletions ztype/to_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ type (
}
JsonTime time.Time
type2 struct {
E *uint
G map[string]int `z:"gg"`
S2 *type1
F []string `json:"fs"`
type1
S1 type1
D bool
Date time.Time `z:"date_time"`
JDate JsonTime `z:"j_date"`
E *uint
G map[string]int `z:"gg"`
S2 *type1
F []string `json:"fs"`
type1
S1 type1
D bool
}
)

Expand Down
3 changes: 2 additions & 1 deletion zutil/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ func (args *Args) CompileString(format string, initialValue ...interface{}) stri
// Compile compiles builder's format to standard sql and returns associated args
func (args *Args) Compile(format string, initialValue ...interface{}) (query string, values []interface{}) {
buf := GetBuff(256)
defer PutBuff(buf)
idx := strings.IndexRune(format, '$')
offset := 0
values = initialValue
Expand Down Expand Up @@ -175,6 +174,8 @@ func (args *Args) Compile(format string, initialValue ...interface{}) (query str

query = buf.String()

PutBuff(buf)

if len(args.sqlNamedArgs) > 0 {
ints := make([]int, 0, len(args.sqlNamedArgs))
for _, p := range args.sqlNamedArgs {
Expand Down
5 changes: 1 addition & 4 deletions zutil/daemon/daemon_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ func (s *darwinLaunchdService) Install() error {
if v, ok := s.Options[optionKeepAlive]; ok {
keepAlive, _ = v.(bool)
}
load := optionRunAtLoadDefault
if v, ok := s.Options[optionRunAtLoad]; ok {
load, _ = v.(bool)
}
load := isServiceRestart(s.Config)
sessionCreate := optionSessionCreateDefault
if v, ok := s.Options[optionSessionCreate]; ok {
sessionCreate, _ = v.(bool)
Expand Down
5 changes: 1 addition & 4 deletions zutil/daemon/daemon_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ func (s *freebsdRcdService) Install() error {
if v, ok := s.Options[optionKeepAlive]; ok {
keepAlive, _ = v.(bool)
}
load := optionRunAtLoadDefault
if v, ok := s.Options[optionRunAtLoad]; ok {
load, _ = v.(bool)
}
load := isServiceRestart(s.Config)
sessionCreate := optionSessionCreateDefault
if v, ok := s.Options[optionSessionCreate]; ok {
sessionCreate, _ = v.(bool)
Expand Down
Loading

0 comments on commit 8acbf86

Please sign in to comment.