From b4b7f20fc66c85673adb65849050314be515f3f3 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Wed, 28 Aug 2024 18:15:23 -0400 Subject: rename attachHandler to killer, attachable to victim --- kill.go | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) (limited to 'kill.go') diff --git a/kill.go b/kill.go index d6c2b3b..f0f04d0 100644 --- a/kill.go +++ b/kill.go @@ -7,34 +7,36 @@ type Killable interface { Dead() <-chan bool } -// A killer can kill the `victim' that is attached to it. +// A killer can kill the victim that is attached to it. // The victim can attach itself to the killer by sending itself via the killer's attach() channel. // The victim can detach itself by sending a signal via its own detach() channel. // // Only one victim can be attached to the killer at a time. // Further messages sent on the attach() channel will block until the current victim is detached. +// +// If the killer is killed while a victim is attached, it kills the victim. +// When killed, the victim must detach itself before dying. type killer interface { - attach() chan<- attachable -} + attach() chan<- victim -type attachable interface { Killable - // Sending to detach() will detach the object from the killer it is attached to. +} + +type victim interface { + // Sending to detach() will detach the victim from the killer it is attached to. detach() <-chan bool + + Killable } -// attachHandler implements killer. It allows victims to attach themselves via the attach channel. -// There can only be one attached victim at a time. -// If attachHandler is killed while a victim is attached, it kills the victim. -// When killed, the victim must detach itself before dying. -type attachHandler struct { - attachChan chan<- attachable +type _killer struct { + attachChan chan<- victim kill chan<- bool dead <-chan bool } -func newAttachHandler() attachHandler { - attach := make(chan attachable) +func newKiller() killer { + attach := make(chan victim) kill := make(chan bool) dead := make(chan bool) @@ -63,9 +65,17 @@ func newAttachHandler() attachHandler { } }() - return attachHandler{attach, kill, dead} + return _killer{attach, kill, dead} +} + +func (k _killer) attach() chan<- victim { + return k.attachChan +} + +func (k _killer) Kill() chan<- bool { + return k.kill } -func (ah attachHandler) attach() chan<- attachable { - return ah.attachChan +func (k _killer) Dead() <-chan bool { + return k.dead } -- cgit v1.2.3