summaryrefslogtreecommitdiff
path: root/plugins/temporal/workflow/canceller.go
blob: 962c527f3e54d5820b20da2668cf58556847e6e6 (plain)
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
package workflow

import (
	"sync"
)

type cancellable func() error

type canceller struct {
	ids sync.Map
}

func (c *canceller) register(id uint64, cancel cancellable) {
	c.ids.Store(id, cancel)
}

func (c *canceller) discard(id uint64) {
	c.ids.Delete(id)
}

func (c *canceller) cancel(ids ...uint64) error {
	var err error
	for _, id := range ids {
		cancel, ok := c.ids.Load(id)
		if ok == false {
			continue
		}

		// TODO return when minimum supported version will be go 1.15
		// go1.14 don't have LoadAndDelete method
		// It was introduced only in go1.15
		c.ids.Delete(id)

		err = cancel.(cancellable)()
		if err != nil {
			return err
		}
	}

	return nil
}