From 7d103c4ee84c23d3519b5087a5d45bcde0b128bd Mon Sep 17 00:00:00 2001 From: John Date: Fri, 18 Jan 2019 22:02:17 +0800 Subject: [PATCH] gcron updates --- g/net/ghttp/ghttp_client_request_client.go | 9 ++++++++ g/os/gcron/gcron.go | 15 ++++++++------ g/os/gcron/gcron_cron.go | 24 +++++++++++----------- g/os/gcron/gcron_unit_1_test.go | 6 +++--- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/g/net/ghttp/ghttp_client_request_client.go b/g/net/ghttp/ghttp_client_request_client.go index 32fe9a2bb..192471acc 100644 --- a/g/net/ghttp/ghttp_client_request_client.go +++ b/g/net/ghttp/ghttp_client_request_client.go @@ -9,6 +9,7 @@ package ghttp import ( + "gitee.com/johng/gf/g/util/gregex" "time" "bytes" "strings" @@ -47,6 +48,14 @@ func (c *Client) SetHeader(key, value string) { c.header[key] = value } +// 通过字符串设置HTTP Header +func (c *Client) SetHeaderRaw(header string) { + for _, line := range strings.Split(strings.TrimSpace(header), "\n") { + array, _ := gregex.MatchString(`^([\w\-]+):\s*(.+)`, line) + c.header[array[1]] = array[2] + } +} + // 设置请求的URL前缀 func (c *Client) SetPrefix(prefix string) { c.prefix = prefix diff --git a/g/os/gcron/gcron.go b/g/os/gcron/gcron.go index a9d3315a2..29c7eded1 100644 --- a/g/os/gcron/gcron.go +++ b/g/os/gcron/gcron.go @@ -9,7 +9,10 @@ // 定时任务. package gcron -import "math" +import ( + "math" + "time" +) const ( STATUS_READY = 0 @@ -45,23 +48,23 @@ func AddTimes(pattern string, times int, job func(), name ... string) (*Entry, e return defaultCron.AddTimes(pattern, times, job, name...) } -// 延迟添加定时任务,delay参数单位为秒 -func DelayAdd(delay int, pattern string, job func(), name ... string) { +// 延迟添加定时任务 +func DelayAdd(delay time.Duration, pattern string, job func(), name ... string) { defaultCron.DelayAdd(delay, pattern, job, name...) } // 延迟添加单例定时任务,delay参数单位为秒 -func DelayAddSingleton(delay int, pattern string, job func(), name ... string) { +func DelayAddSingleton(delay time.Duration, pattern string, job func(), name ... string) { defaultCron.DelayAddSingleton(delay, pattern, job, name...) } // 延迟添加只运行一次的定时任务,delay参数单位为秒 -func DelayAddOnce(delay int, pattern string, job func(), name ... string) { +func DelayAddOnce(delay time.Duration, pattern string, job func(), name ... string) { defaultCron.DelayAddOnce(delay, pattern, job, name...) } // 延迟添加运行指定次数的定时任务,delay参数单位为秒 -func DelayAddTimes(delay int, pattern string, times int, job func(), name ... string) { +func DelayAddTimes(delay time.Duration, pattern string, times int, job func(), name ... string) { defaultCron.DelayAddTimes(delay, pattern, times, job, name...) } diff --git a/g/os/gcron/gcron_cron.go b/g/os/gcron/gcron_cron.go index cc83edb43..49938b1e0 100644 --- a/g/os/gcron/gcron_cron.go +++ b/g/os/gcron/gcron_cron.go @@ -92,36 +92,36 @@ func (c *Cron) AddTimes(pattern string, times int, job func(), name ... string) } } -// 延迟添加定时任务,delay参数单位为秒 -func (c *Cron) DelayAdd(delay int, pattern string, job func(), name ... string) { - gtimer.AddOnce(time.Duration(delay)*time.Second, func() { +// 延迟添加定时任务 +func (c *Cron) DelayAdd(delay time.Duration, pattern string, job func(), name ... string) { + gtimer.AddOnce(delay, func() { if _, err := c.Add(pattern, job, name ...); err != nil { panic(err) } }) } -// 延迟添加单例定时任务,delay参数单位为秒 -func (c *Cron) DelayAddSingleton(delay int, pattern string, job func(), name ... string) { - gtimer.AddOnce(time.Duration(delay)*time.Second, func() { +// 延迟添加单例定时任务 +func (c *Cron) DelayAddSingleton(delay time.Duration, pattern string, job func(), name ... string) { + gtimer.AddOnce(delay, func() { if _, err := c.AddSingleton(pattern, job, name ...); err != nil { panic(err) } }) } -// 延迟添加运行指定次数的定时任务,delay参数单位为秒 -func (c *Cron) DelayAddOnce(delay int, pattern string, job func(), name ... string) { - gtimer.AddOnce(time.Duration(delay)*time.Second, func() { +// 延迟添加运行指定次数的定时任务 +func (c *Cron) DelayAddOnce(delay time.Duration, pattern string, job func(), name ... string) { + gtimer.AddOnce(delay, func() { if _, err := c.AddOnce(pattern, job, name ...); err != nil { panic(err) } }) } -// 延迟添加只运行一次的定时任务,delay参数单位为秒 -func (c *Cron) DelayAddTimes(delay int, pattern string, times int, job func(), name ... string) { - gtimer.AddOnce(time.Duration(delay)*time.Second, func() { +// 延迟添加只运行一次的定时任务 +func (c *Cron) DelayAddTimes(delay time.Duration, pattern string, times int, job func(), name ... string) { + gtimer.AddOnce(delay, func() { if _, err := c.AddTimes(pattern, times, job, name ...); err != nil { panic(err) } diff --git a/g/os/gcron/gcron_unit_1_test.go b/g/os/gcron/gcron_unit_1_test.go index 33a9b5556..582979696 100644 --- a/g/os/gcron/gcron_unit_1_test.go +++ b/g/os/gcron/gcron_unit_1_test.go @@ -122,14 +122,14 @@ func TestCron_DelayAdd(t *testing.T) { gtest.Case(t, func() { cron := gcron.New() array := garray.New(0, 0) - cron.DelayAdd(1, "* * * * * *", func() { + cron.DelayAdd(500*time.Millisecond, "* * * * * *", func() { array.Append(1) }) gtest.Assert(cron.Size(), 0) - time.Sleep(1200*time.Millisecond) + time.Sleep(800*time.Millisecond) gtest.Assert(array.Len(), 0) gtest.Assert(cron.Size(), 1) - time.Sleep(1200*time.Millisecond) + time.Sleep(500*time.Millisecond) gtest.Assert(array.Len(), 1) gtest.Assert(cron.Size(), 1) })