]> 127.0.0.1 Git - part/.git/commitdiff
progLock support data v0.5.39
authorqydysky <qydysky@foxmail.com>
Sun, 27 Jun 2021 07:41:25 +0000 (15:41 +0800)
committerqydysky <qydysky@foxmail.com>
Sun, 27 Jun 2021 07:41:25 +0000 (15:41 +0800)
progLock/Lock.go
progLock/Lock_test.go

index 520ba4c3638a15c4762b7ca83a615dffb1485e24..e3fd572fccd3551950745ea8539c4ca7d496c16e 100644 (file)
@@ -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()
index e0f6353197de491b6825b361266fa3bbab4afb96..78e538e8f26a8bea33f09396046520b1deff94c2 100644 (file)
@@ -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())