From 029646efba23baf4513da16ee6a1e3112f9349c4 Mon Sep 17 00:00:00 2001 From: qydysky Date: Sun, 27 Jun 2021 15:41:25 +0800 Subject: [PATCH] progLock support data --- progLock/Lock.go | 12 ++++++++++-- progLock/Lock_test.go | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/progLock/Lock.go b/progLock/Lock.go index 520ba4c..e3fd572 100644 --- a/progLock/Lock.go +++ b/progLock/Lock.go @@ -5,13 +5,15 @@ import ( "sync" "time" "errors" + "strings" "encoding/json" part "github.com/qydysky/part" ) type lock struct { - Time int64 `json:"t"` + Time int64 `json:"time"` + Data string `json:"date"` stopsign bool b chan struct{} sync.Mutex @@ -26,7 +28,9 @@ func New() *lock { return &lock{} } -func (l *lock) Start() (err error) { +//start will save current time and Data +//if start func is called in other programe, Data or "still alive" will return as error +func (l *lock) Start(Data ...string) (err error) { l.Lock() defer l.Unlock() @@ -48,10 +52,14 @@ func (l *lock) Start() (err error) { if err != nil {panic(err.Error())} if time.Now().Unix() - l.Time <= lock_timeout { + if l.Data != "" { + return errors.New(l.Data) + } return errors.New("still alive") } } else { l.b = make(chan struct{}) + l.Data = strings.Join(Data, "&") go func(l *lock){ for !l.stopsign { l.Time = time.Now().Unix() diff --git a/progLock/Lock_test.go b/progLock/Lock_test.go index e0f6353..78e538e 100644 --- a/progLock/Lock_test.go +++ b/progLock/Lock_test.go @@ -7,7 +7,7 @@ import ( func Test(t *testing.T) { l := New() - t.Log(l.Start()) + t.Log(l.Start("111","222")) t.Log(New().Start()) time.Sleep(time.Second) t.Log(l.Stop()) -- 2.39.2