Skip to content

Commit ace56f7

Browse files
authored
Merge pull request #261 from vmarkovtsev/master
Dispose grpc channels
2 parents 4295b79 + f1d558a commit ace56f7

File tree

6 files changed

+40
-8
lines changed

6 files changed

+40
-8
lines changed

internal/core/pipeline.go

+14
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ type FeaturedPipelineItem interface {
128128
Features() []string
129129
}
130130

131+
// DisposablePipelineItem enables resources cleanup after finishing running the pipeline.
132+
type DisposablePipelineItem interface {
133+
PipelineItem
134+
// Dispose frees any previously allocated unmanaged resources. No Consume() calls are possible
135+
// afterwards. The item needs to be Initialize()-d again.
136+
// This method is invoked once for each item in the pipeline, **in a single forked instance**.
137+
// Thus it is the responsibility of the item's programmer to deal with forks and merges, if
138+
// necessary.
139+
Dispose()
140+
}
141+
131142
// LeafPipelineItem corresponds to the top level pipeline items which produce the end results.
132143
type LeafPipelineItem interface {
133144
PipelineItem
@@ -848,6 +859,9 @@ func (pipeline *Pipeline) Run(commits []*object.Commit) (map[LeafPipelineItem]in
848859
result := map[LeafPipelineItem]interface{}{}
849860
if !pipeline.DryRun {
850861
for index, item := range getMasterBranch(branches) {
862+
if casted, ok := item.(DisposablePipelineItem); ok {
863+
casted.Dispose()
864+
}
851865
if casted, ok := item.(LeafPipelineItem); ok {
852866
result[pipeline.items[index].(LeafPipelineItem)] = casted.Finalize()
853867
}

internal/core/pipeline_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
type testPipelineItem struct {
2222
Initialized bool
2323
DepsConsumed bool
24+
Disposed bool
2425
Forked bool
2526
Merged *bool
2627
CommitMatches bool
@@ -118,6 +119,10 @@ func (item *testPipelineItem) Consume(deps map[string]interface{}) (map[string]i
118119
return map[string]interface{}{"test": item}, nil
119120
}
120121

122+
func (item *testPipelineItem) Dispose() {
123+
item.Disposed = true
124+
}
125+
121126
func (item *testPipelineItem) Fork(n int) []PipelineItem {
122127
result := make([]PipelineItem, n)
123128
for i := 0; i < n; i++ {
@@ -304,6 +309,7 @@ func TestPipelineRun(t *testing.T) {
304309
assert.True(t, val >= 0, key)
305310
}
306311
assert.True(t, item.DepsConsumed)
312+
assert.True(t, item.Disposed)
307313
assert.True(t, item.CommitMatches)
308314
assert.True(t, item.IndexMatches)
309315
assert.Equal(t, 1, *item.MergeState)

internal/plumbing/uast/uast.go

+7
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,13 @@ func (exr *Extractor) Consume(deps map[string]interface{}) (map[string]interface
294294
return map[string]interface{}{DependencyUasts: uasts}, nil
295295
}
296296

297+
// Dispose closes the open GRPC channels.
298+
func (exr *Extractor) Dispose() {
299+
for _, client := range exr.clients {
300+
client.Close()
301+
}
302+
}
303+
297304
// Fork clones this PipelineItem.
298305
func (exr *Extractor) Fork(n int) []core.PipelineItem {
299306
return core.ForkSamePipelineItem(exr, n)

internal/plumbing/uast/uast_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func AddHash(t *testing.T, cache map[plumbing.Hash]*items.CachedBlob, hash strin
4343

4444
func TestUASTExtractorMeta(t *testing.T) {
4545
exr := fixtureUASTExtractor()
46+
defer exr.Dispose()
4647
assert.Equal(t, exr.Name(), "UAST")
4748
assert.Equal(t, len(exr.Provides()), 1)
4849
assert.Equal(t, exr.Provides()[0], DependencyUasts)
@@ -63,14 +64,15 @@ func TestUASTExtractorMeta(t *testing.T) {
6364

6465
func TestUASTExtractorConfiguration(t *testing.T) {
6566
exr := fixtureUASTExtractor()
67+
defer exr.Dispose()
6668
facts := map[string]interface{}{}
67-
exr.Configure(facts)
69+
assert.Nil(t, exr.Configure(facts))
6870
facts[ConfigUASTEndpoint] = "localhost:9432"
6971
facts[ConfigUASTTimeout] = 15
7072
facts[ConfigUASTPoolSize] = 7
7173
facts[ConfigUASTFailOnErrors] = true
7274
facts[ConfigUASTIgnoreMissingDrivers] = []string{"test"}
73-
exr.Configure(facts)
75+
assert.Nil(t, exr.Configure(facts))
7476
assert.Equal(t, exr.Endpoint, facts[ConfigUASTEndpoint])
7577
assert.NotNil(t, exr.Context)
7678
assert.Equal(t, exr.PoolSize, facts[ConfigUASTPoolSize])
@@ -95,6 +97,7 @@ func TestUASTExtractorNoBabelfish(t *testing.T) {
9597

9698
func TestUASTExtractorConsume(t *testing.T) {
9799
exr := fixtureUASTExtractor()
100+
defer exr.Dispose()
98101
changes := make(object.Changes, 4)
99102
// 2b1ed978194a94edeabbca6de7ff3b5771d4d665
100103
treeFrom, _ := test.Repository.TreeObject(plumbing.NewHash(
@@ -211,6 +214,7 @@ func TestUASTExtractorConsume(t *testing.T) {
211214

212215
func TestUASTExtractorFork(t *testing.T) {
213216
exr1 := fixtureUASTExtractor()
217+
defer exr1.Dispose()
214218
clones := exr1.Fork(1)
215219
assert.Len(t, clones, 1)
216220
exr2 := clones[0].(*Extractor)

leaves/research/typos.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"gopkg.in/src-d/hercules.v10/internal/pb"
2020
items "gopkg.in/src-d/hercules.v10/internal/plumbing"
2121
uast_items "gopkg.in/src-d/hercules.v10/internal/plumbing/uast"
22+
"gopkg.in/src-d/hercules.v10/internal/yaml"
2223
)
2324

2425
// TyposDatasetBuilder collects pairs of typo-fix in source code identifiers.
@@ -272,10 +273,10 @@ func (tdb *TyposDatasetBuilder) Serialize(result interface{}, binary bool, write
272273

273274
func (tdb *TyposDatasetBuilder) serializeText(result *TyposResult, writer io.Writer) {
274275
for _, t := range result.Typos {
275-
fmt.Fprintf(writer, " - wrong: %s\n", t.Wrong)
276-
fmt.Fprintf(writer, " correct: %s\n", t.Correct)
276+
fmt.Fprintf(writer, " - wrong: %s\n", yaml.SafeString(t.Wrong))
277+
fmt.Fprintf(writer, " correct: %s\n", yaml.SafeString(t.Correct))
277278
fmt.Fprintf(writer, " commit: %s\n", t.Commit.String())
278-
fmt.Fprintf(writer, " file: %s\n", t.File)
279+
fmt.Fprintf(writer, " file: %s\n", yaml.SafeString(t.File))
279280
fmt.Fprintf(writer, " line: %d\n", t.Line)
280281
}
281282
}

leaves/research/typos_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,10 @@ func TestTyposDatasetSerialize(t *testing.T) {
237237
buffer := &bytes.Buffer{}
238238
err := ca.Serialize(res, false, buffer)
239239
assert.Nil(t, err)
240-
assert.Equal(t, ` - wrong: Fo
241-
correct: Foo
240+
assert.Equal(t, ` - wrong: "Fo"
241+
correct: "Foo"
242242
commit: 0000000000000000000000000000000000000000
243-
file: bar.go
243+
file: "bar.go"
244244
line: 7
245245
`, buffer.String())
246246

0 commit comments

Comments
 (0)