Skip to content

Commit 0518f67

Browse files
committed
Removed uuid package dependency & not it's a 0 deps package
1 parent d8d5544 commit 0518f67

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
module github.com/kisshan13/taskout
22

33
go 1.23.3
4-
5-
require github.com/google/uuid v1.6.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
2-
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

taskout.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"fmt"
66
"sync"
77
"time"
8-
9-
"github.com/google/uuid"
108
)
119

1210
// TaskManager is responsible for managing tasks.
@@ -122,7 +120,7 @@ func (tm *TaskManager) SetTimeout(task func(ctx context.Context), duration time.
122120
oneShot: true,
123121
}
124122

125-
taskId := uuid.NewString()
123+
taskId, _ := generateId()
126124

127125
tm.addTask(TaskID(taskId), t)
128126

@@ -146,7 +144,7 @@ func (tm *TaskManager) SetInterval(task func(context.Context), interval time.Dur
146144
oneShot: false,
147145
}
148146

149-
taskId := uuid.NewString()
147+
taskId, _ := generateId()
150148

151149
tm.addTask(TaskID(taskId), t)
152150

utils.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package taskout
2+
3+
import (
4+
"crypto/rand"
5+
"encoding/binary"
6+
"fmt"
7+
"time"
8+
)
9+
10+
// GenerateID creates a lightweight unique ID based on the current timestamp and a random component.
11+
func generateId() (string, error) {
12+
now := time.Now().UnixNano()
13+
randBytes := make([]byte, 4)
14+
_, err := rand.Read(randBytes)
15+
if err != nil {
16+
return "", fmt.Errorf("failed to generate id for task")
17+
}
18+
19+
randPart := binary.BigEndian.Uint32(randBytes)
20+
21+
return fmt.Sprintf("%x-%x", now, randPart), nil
22+
}

0 commit comments

Comments
 (0)