-
Notifications
You must be signed in to change notification settings - Fork 0
/
abortable.go
114 lines (90 loc) · 2.65 KB
/
abortable.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package quacktors
//The Abortable interface defines the methods a struct has
//to implement so it can be returned by an action that can be canceled.
//It is very similar to context.Context with the key difference that
//an Abortable can only Abort and doesn't carry any further
//details about the underlying action.
type Abortable interface {
//The Abort function aborts the underlying task (e.g. a Monitor) when called.
Abort()
}
type monitorAbortable struct {
pid *Pid
self *Pid
}
func (ma *monitorAbortable) Abort() {
logger.Debug("demonitoring pid",
"monitored_gpid", ma.pid.String(),
"monitor_pid", ma.self.Id)
go func() {
if ma.pid.MachineId != machineId {
//Monitor is not on this machine
logger.Debug("monitor to abort is not on this machine, forwarding to remote machine",
"monitored_gpid", ma.pid.String(),
"monitor_pid", ma.self.Id,
"machine_id", ma.pid.MachineId)
m, ok := getMachine(ma.pid.MachineId)
if ok && m.connected {
//send demonitor request to demonitor channel on the machine connection
m.demonitorChan <- remoteMonitorTuple{From: ma.self, To: ma.pid}
return
}
logger.Warn("remote machine is not registered, couldn't abort monitor",
"monitored_gpid", ma.pid.String(),
"monitor_pid", ma.self.Id,
"machine_id", ma.pid.MachineId)
return
}
defer func() {
if r := recover(); r != nil {
//This happens if we write to the demonitorChan while the actor is being closed
}
}()
if ma.pid.demonitorChan == nil {
logger.Warn("pid to demonitor is already down",
"monitored_gpid", ma.pid.String(),
"monitor_pid", ma.self.Id)
return
}
ma.pid.demonitorChan <- ma.self
}()
}
type machineConnectionMonitorAbortable struct {
machine *Machine
monitor *Pid
}
func (ma *machineConnectionMonitorAbortable) Abort() {
logger.Debug("demonitoring machine connection",
"machine_id", ma.machine.MachineId,
"monitor_pid", ma.monitor.Id)
go func() {
ma.machine.monitorsMu.Lock()
defer ma.machine.monitorsMu.Unlock()
defer func() {
if r := recover(); r != nil {
//This happens if we write to the demonitorChan while the actor is being closed
}
}()
if !ma.machine.connected {
logger.Warn("machine connection to demonitor is already down",
"machine_id", ma.machine.MachineId,
"monitor_pid", ma.monitor.Id)
return
}
ma.machine.monitorQuitChannels[ma.monitor.String()] <- true
}()
}
type sendAfterAbortable struct {
quitChan chan bool
}
func (sa *sendAfterAbortable) Abort() {
defer func() {
//this can happen if the channel is already closed
recover()
}()
sa.quitChan <- true
}
type noopAbortable struct {
}
func (na *noopAbortable) Abort() {
}