Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 0ef587a

Browse files
committed
Add Close() method to Project to release resources
Problem Observed Goroutine leaks because there is no way to "close" projects Problem Detail NewProject() creates a defaultListener in it. The listener starts a new goroutine in NewDefaultListener(). We currently don't have a method to stop this goroutine. Suggested Resolution This comimt adds a new method Close() to release resources tied to a Project.
1 parent 8e4221d commit 0ef587a

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

project/listener.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ type defaultListener struct {
3939
}
4040

4141
// NewDefaultListener create a default listener for the specified project.
42-
func NewDefaultListener(p *Project) chan<- events.Event {
43-
l := defaultListener{
42+
func NewDefaultListener(p *Project) *defaultListener {
43+
l := &defaultListener{
4444
listenChan: make(chan events.Event),
4545
project: p,
4646
}
4747
go l.start()
48-
return l.listenChan
48+
return l
4949
}
5050

5151
func (d *defaultListener) start() {
@@ -79,3 +79,7 @@ func (d *defaultListener) start() {
7979
}
8080
}
8181
}
82+
83+
func (d *defaultListener) close() {
84+
close(d.listenChan)
85+
}

project/project.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ type Project struct {
3535
ReloadCallback func() error
3636
ParseOptions *config.ParseOptions
3737

38-
runtime RuntimeProject
39-
networks Networks
40-
volumes Volumes
41-
configVersion string
42-
context *Context
43-
reload []string
44-
upCount int
45-
listeners []chan<- events.Event
46-
hasListeners bool
38+
runtime RuntimeProject
39+
networks Networks
40+
volumes Volumes
41+
configVersion string
42+
context *Context
43+
reload []string
44+
upCount int
45+
listeners []chan<- events.Event
46+
hasListeners bool
47+
defaultListener *defaultListener
4748
}
4849

4950
// NewProject creates a new project with the specified context.
@@ -93,11 +94,18 @@ func NewProject(context *Context, runtime RuntimeProject, parseOptions *config.P
9394

9495
context.Project = p
9596

96-
p.listeners = []chan<- events.Event{NewDefaultListener(p)}
97+
p.defaultListener = NewDefaultListener(p)
98+
p.listeners = []chan<- events.Event{p.defaultListener.listenChan}
9799

98100
return p
99101
}
100102

103+
// Parse releases resources attached to the project
104+
func (p *Project) Close() error {
105+
p.defaultListener.close()
106+
return nil
107+
}
108+
101109
// Parse populates project information based on its context. It sets up the name,
102110
// the composefile and the composebytes (the composefile content).
103111
func (p *Project) Parse() error {

0 commit comments

Comments
 (0)