Skip to content

Commit

Permalink
SelectEndpoint returns error
Browse files Browse the repository at this point in the history
  • Loading branch information
NovaFell committed Dec 3, 2024
1 parent 2c61e33 commit 26ed22d
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,36 +85,36 @@ func GetEndpoints(ctx context.Context, endpoint string, opts ...Option) ([]*ua.E
// security policy and security mode. policy and mode can be omitted so that
// only one of them has to match.
// todo(fs): should this function return an error?
func SelectEndpoint(endpoints []*ua.EndpointDescription, policy string, mode ua.MessageSecurityMode) *ua.EndpointDescription {
func SelectEndpoint(endpoints []*ua.EndpointDescription, policy string, mode ua.MessageSecurityMode) (*ua.EndpointDescription, error) {
if len(endpoints) == 0 {
return nil
return nil, errors.Errorf("no endpoints available")
}

sort.Sort(sort.Reverse(bySecurityLevel(endpoints)))
policy = ua.FormatSecurityPolicyURI(policy)

// don't care -> return highest security level
if policy == "" && mode == ua.MessageSecurityModeInvalid {
return endpoints[0]
return endpoints[0], nil
}

for _, p := range endpoints {
// match only security mode
if policy == "" && p.SecurityMode == mode {
return p
return p, nil
}

// match only security policy
if p.SecurityPolicyURI == policy && mode == ua.MessageSecurityModeInvalid {
return p
return p, nil
}

// match both
if p.SecurityPolicyURI == policy && p.SecurityMode == mode {
return p
return p, nil
}
}
return nil
return nil, errors.Errorf("no matching endpoint found for policy %s and mode %s", policy, mode)
}

type bySecurityLevel []*ua.EndpointDescription
Expand Down

0 comments on commit 26ed22d

Please sign in to comment.