-
Notifications
You must be signed in to change notification settings - Fork 8
/
map.go
75 lines (62 loc) · 1.61 KB
/
map.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Copyright © 2019 Hamed Yousefi <hdyousefi@gmail.com>.
*/
package gowl
import (
"context"
"sync"
"github.com/hamed-yousefi/gowl/status/worker"
)
type (
// controlPanelMap is a thread safe map for controlling processes. It also
// provides type safety.
// Key: PID
// Value: processContext
controlPanelMap struct {
internal sync.Map
}
// workerStatsMap is a thread safe map for controlling processes. It also
// provides type safety.
// Key: WorkerName
// Value: worker.Status
workerStatsMap struct {
internal sync.Map
}
// processStatusMap is a thread safe map for controlling processes. It also
// provides type safety.
// Key: PID
// Value: ProcessStats
processStatusMap struct {
internal sync.Map
}
// processContext represents a cancellation context by holding a context and
// a cancel function.
processContext struct {
ctx context.Context
cancel context.CancelFunc
}
)
func (c *controlPanelMap) put(pid PID, pc *processContext) {
c.internal.Store(pid, pc)
}
func (c *controlPanelMap) get(pid PID) *processContext {
in, _ := c.internal.Load(pid)
cancel, _ := in.(*processContext)
return cancel
}
func (c *workerStatsMap) put(name WorkerName, status worker.Status) {
c.internal.Store(name, status)
}
func (c *workerStatsMap) get(name WorkerName) worker.Status {
in, _ := c.internal.Load(name)
status, _ := in.(worker.Status)
return status
}
func (c *processStatusMap) put(pid PID, stats ProcessStats) {
c.internal.Store(pid, stats)
}
func (c *processStatusMap) get(pid PID) ProcessStats {
in, _ := c.internal.Load(pid)
stats, _ := in.(ProcessStats)
return stats
}