forked from open-telemetry/opentelemetry-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add change handler to register callbacks (open-telemetry#1292)
* config: move onChange callback list methods into a thread-safe wrapper Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de> * config: keep change handler private Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de> * follow naming recommendations Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de> Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>
- Loading branch information
Showing
5 changed files
with
123 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package config contains the operator's runtime configuration. | ||
package config | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/go-logr/logr" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
// changeHandler is implemented by any structure that is able to register callbacks | ||
// and call them using one single method. | ||
type changeHandler interface { | ||
// Do will call every registered callback. | ||
Do() error | ||
// Register this function as a callback that will be executed when Do() is called. | ||
Register(f func() error) | ||
} | ||
|
||
// newOnChange returns a thread-safe ChangeHandler. | ||
func newOnChange() changeHandler { | ||
return &onChange{ | ||
logger: logf.Log.WithName("change-handler"), | ||
} | ||
} | ||
|
||
type onChange struct { | ||
logger logr.Logger | ||
|
||
callbacks []func() error | ||
muCallbacks sync.Mutex | ||
} | ||
|
||
func (o *onChange) Do() error { | ||
o.muCallbacks.Lock() | ||
defer o.muCallbacks.Unlock() | ||
for _, fn := range o.callbacks { | ||
if err := fn(); err != nil { | ||
o.logger.Error(err, "change callback failed") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (o *onChange) Register(f func() error) { | ||
o.muCallbacks.Lock() | ||
defer o.muCallbacks.Unlock() | ||
o.callbacks = append(o.callbacks, f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package config contains the operator's runtime configuration. | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestChangeHandler(t *testing.T) { | ||
// prepare | ||
internal := 0 | ||
callback := func() error { | ||
internal += 1 | ||
return nil | ||
} | ||
h := newOnChange() | ||
|
||
h.Register(callback) | ||
|
||
for i := 0; i < 5; i++ { | ||
assert.Equal(t, i, internal) | ||
require.NoError(t, h.Do()) | ||
assert.Equal(t, i+1, internal) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters