Skip to content

Commit e8c86f6

Browse files
committed
Revert "[chore] remove pkg/multicloser (jaegertracing#4291)"
This reverts commit a172bcc. Signed-off-by: Pablo Baeyens <pablo.baeyens@datadoghq.com>
1 parent 8de19fa commit e8c86f6

File tree

3 files changed

+116
-6
lines changed

3 files changed

+116
-6
lines changed

cmd/agent/app/reporter/grpc/collector_proxy.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package grpc
1616

1717
import (
18-
"errors"
1918
"io"
2019

2120
"go.uber.org/zap"
@@ -25,6 +24,7 @@ import (
2524
grpcManager "github.com/jaegertracing/jaeger/cmd/agent/app/configmanager/grpc"
2625
"github.com/jaegertracing/jaeger/cmd/agent/app/reporter"
2726
"github.com/jaegertracing/jaeger/pkg/metrics"
27+
"github.com/jaegertracing/jaeger/pkg/multicloser"
2828
)
2929

3030
// ProxyBuilder holds objects communicating with collector
@@ -74,9 +74,5 @@ func (b ProxyBuilder) GetManager() configmanager.ClientConfigManager {
7474

7575
// Close closes connections used by proxy.
7676
func (b ProxyBuilder) Close() error {
77-
return errors.Join(
78-
b.reporter.Close(),
79-
b.tlsCloser.Close(),
80-
b.GetConn().Close(),
81-
)
77+
return multicloser.Wrap(b.reporter, b.tlsCloser, b.GetConn()).Close()
8278
}

pkg/multicloser/multicloser.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2020 The Jaeger Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package multicloser
16+
17+
import (
18+
"io"
19+
20+
"github.com/jaegertracing/jaeger/pkg/multierror"
21+
)
22+
23+
// MultiCloser wraps multiple io.Closer interfaces
24+
type MultiCloser struct {
25+
closers []io.Closer
26+
}
27+
28+
var _ io.Closer = (*MultiCloser)(nil)
29+
30+
// Close implements io.Closer
31+
func (m MultiCloser) Close() error {
32+
var errs []error
33+
for _, c := range m.closers {
34+
if c == nil {
35+
continue
36+
}
37+
err := c.Close()
38+
if err != nil {
39+
errs = append(errs, err)
40+
}
41+
}
42+
return multierror.Wrap(errs)
43+
}
44+
45+
// Wrap creates io.Closer that encapsulates multiple io.Closer interfaces
46+
func Wrap(closers ...io.Closer) *MultiCloser {
47+
return &MultiCloser{
48+
closers: closers,
49+
}
50+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2020 The Jaeger Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package multicloser
16+
17+
import (
18+
"fmt"
19+
"io"
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestCloser(t *testing.T) {
26+
expectedErr := "some error"
27+
tests := []struct {
28+
closer io.Closer
29+
expectedErr string
30+
}{
31+
{
32+
closer: Wrap(testCloser{}, testCloser{}),
33+
},
34+
{
35+
closer: Wrap(testCloser{}, testCloser{fmt.Errorf(expectedErr)}),
36+
expectedErr: expectedErr,
37+
},
38+
{
39+
closer: Wrap(testCloser{fmt.Errorf(expectedErr)}, testCloser{fmt.Errorf(expectedErr)}),
40+
expectedErr: fmt.Sprintf("[%v, %v]", expectedErr, expectedErr),
41+
},
42+
{
43+
closer: Wrap(nil),
44+
},
45+
}
46+
for _, test := range tests {
47+
err := test.closer.Close()
48+
if test.expectedErr == "" {
49+
assert.Nil(t, err)
50+
} else {
51+
assert.EqualError(t, err, test.expectedErr)
52+
}
53+
}
54+
}
55+
56+
type testCloser struct {
57+
err error
58+
}
59+
60+
var _ io.Closer = (*testCloser)(nil)
61+
62+
func (t testCloser) Close() error {
63+
return t.err
64+
}

0 commit comments

Comments
 (0)