fix data race in tagets map
@@ -10,6 +10,7 @@ import (
"github.com/karimra/gnmic/lockers"
"github.com/karimra/gnmic/outputs"
+ "github.com/karimra/gnmic/target"
"github.com/openconfig/gnmi/proto/gnmi"
)
@@ -151,7 +152,14 @@ func (a *App) TargetSubscribePoll(ctx context.Context, name string) {
}
func (a *App) clientSubscribe(ctx context.Context, tName string) error {
- if t, ok := a.Targets[tName]; ok {
+ var t *target.Target
+ var ok bool
+ a.operLock.RLock()
+ if t, ok = a.Targets[tName]; !ok {
+ a.operLock.RUnlock()
+ return fmt.Errorf("unknown target name: %q", tName)
+ }
+ a.operLock.RUnlock()
subscriptionsConfigs := t.Subscriptions
if len(subscriptionsConfigs) == 0 {
subscriptionsConfigs = a.Config.Subscriptions
@@ -193,11 +201,16 @@ func (a *App) clientSubscribe(ctx context.Context, tName string) error {
}
return nil
}
- return fmt.Errorf("unknown target name: %q", tName)
-}
func (a *App) clientSubscribeOnce(ctx context.Context, tName string) error {
- if t, ok := a.Targets[tName]; ok {
+ var t *target.Target
+ var ok bool
+ a.operLock.RLock()
+ if t, ok = a.Targets[tName]; !ok {
+ a.operLock.RUnlock()
+ return fmt.Errorf("unknown target name: %q", tName)
+ }
+ a.operLock.RUnlock()
subscriptionsConfigs := t.Subscriptions
if len(subscriptionsConfigs) == 0 {
subscriptionsConfigs = a.Config.Subscriptions
@@ -256,8 +269,7 @@ func (a *App) clientSubscribeOnce(ctx context.Context, tName string) error {
}
}
return nil
- }
- return fmt.Errorf("unknown target name: %q", tName)
+
}
// clientSubscribePoll sends a gnmi.SubscribeRequest_Poll to targetName and returns the response and an error,
