File: closer.go

package info (click to toggle)
golang-github-pion-dtls-v3 3.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,124 kB
  • sloc: makefile: 4
file content (50 lines) | stat: -rw-r--r-- 1,108 bytes parent folder | download
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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

// Package closer provides signaling channel for shutdown
package closer

import (
	"context"
)

// Closer allows for each signaling a channel for shutdown.
type Closer struct {
	ctx       context.Context //nolint:containedctx
	closeFunc func()
}

// NewCloser creates a new instance of Closer.
func NewCloser() *Closer {
	ctx, closeFunc := context.WithCancel(context.Background())

	return &Closer{
		ctx:       ctx,
		closeFunc: closeFunc,
	}
}

// NewCloserWithParent creates a new instance of Closer with a parent context.
func NewCloserWithParent(ctx context.Context) *Closer {
	ctx, closeFunc := context.WithCancel(ctx)

	return &Closer{
		ctx:       ctx,
		closeFunc: closeFunc,
	}
}

// Done returns a channel signaling when it is done.
func (c *Closer) Done() <-chan struct{} {
	return c.ctx.Done()
}

// Err returns an error of the context.
func (c *Closer) Err() error {
	return c.ctx.Err()
}

// Close sends a signal to trigger the ctx done channel.
func (c *Closer) Close() {
	c.closeFunc()
}