2023-05-29 21:03:29 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright 2017 gRPC 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 manual defines a resolver that can be used to manually send resolved
|
|
|
|
// addresses to ClientConn.
|
|
|
|
package manual
|
|
|
|
|
|
|
|
import (
|
2023-06-19 21:01:00 +00:00
|
|
|
"sync"
|
|
|
|
|
2023-05-29 21:03:29 +00:00
|
|
|
"google.golang.org/grpc/resolver"
|
|
|
|
)
|
|
|
|
|
2023-10-25 10:07:57 +00:00
|
|
|
// NewBuilderWithScheme creates a new manual resolver builder with the given
|
|
|
|
// scheme. Every instance of the manual resolver may only ever be used with a
|
|
|
|
// single grpc.ClientConn. Otherwise, bad things will happen.
|
2023-05-29 21:03:29 +00:00
|
|
|
func NewBuilderWithScheme(scheme string) *Resolver {
|
|
|
|
return &Resolver{
|
2023-09-11 20:46:57 +00:00
|
|
|
BuildCallback: func(resolver.Target, resolver.ClientConn, resolver.BuildOptions) {},
|
|
|
|
UpdateStateCallback: func(error) {},
|
|
|
|
ResolveNowCallback: func(resolver.ResolveNowOptions) {},
|
|
|
|
CloseCallback: func() {},
|
|
|
|
scheme: scheme,
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolver is also a resolver builder.
|
|
|
|
// It's build() function always returns itself.
|
|
|
|
type Resolver struct {
|
|
|
|
// BuildCallback is called when the Build method is called. Must not be
|
|
|
|
// nil. Must not be changed after the resolver may be built.
|
|
|
|
BuildCallback func(resolver.Target, resolver.ClientConn, resolver.BuildOptions)
|
2023-09-11 20:46:57 +00:00
|
|
|
// UpdateStateCallback is called when the UpdateState method is called on
|
|
|
|
// the resolver. The value passed as argument to this callback is the value
|
|
|
|
// returned by the resolver.ClientConn. Must not be nil. Must not be
|
|
|
|
// changed after the resolver may be built.
|
|
|
|
UpdateStateCallback func(err error)
|
2023-05-29 21:03:29 +00:00
|
|
|
// ResolveNowCallback is called when the ResolveNow method is called on the
|
|
|
|
// resolver. Must not be nil. Must not be changed after the resolver may
|
|
|
|
// be built.
|
|
|
|
ResolveNowCallback func(resolver.ResolveNowOptions)
|
|
|
|
// CloseCallback is called when the Close method is called. Must not be
|
|
|
|
// nil. Must not be changed after the resolver may be built.
|
|
|
|
CloseCallback func()
|
|
|
|
scheme string
|
|
|
|
|
|
|
|
// Fields actually belong to the resolver.
|
2023-10-25 10:07:57 +00:00
|
|
|
// Guards access to below fields.
|
|
|
|
mu sync.Mutex
|
|
|
|
CC resolver.ClientConn
|
|
|
|
// Storing the most recent state update makes this resolver resilient to
|
|
|
|
// restarts, which is possible with channel idleness.
|
|
|
|
lastSeenState *resolver.State
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitialState adds initial state to the resolver so that UpdateState doesn't
|
|
|
|
// need to be explicitly called after Dial.
|
|
|
|
func (r *Resolver) InitialState(s resolver.State) {
|
2023-10-25 10:07:57 +00:00
|
|
|
r.lastSeenState = &s
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build returns itself for Resolver, because it's both a builder and a resolver.
|
|
|
|
func (r *Resolver) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
|
2023-10-25 10:07:57 +00:00
|
|
|
r.BuildCallback(target, cc, opts)
|
2023-06-19 21:01:00 +00:00
|
|
|
r.mu.Lock()
|
2023-05-29 21:03:29 +00:00
|
|
|
r.CC = cc
|
2023-10-25 10:07:57 +00:00
|
|
|
if r.lastSeenState != nil {
|
|
|
|
err := r.CC.UpdateState(*r.lastSeenState)
|
|
|
|
go r.UpdateStateCallback(err)
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
2023-10-25 10:07:57 +00:00
|
|
|
r.mu.Unlock()
|
2023-05-29 21:03:29 +00:00
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2023-10-25 10:07:57 +00:00
|
|
|
// Scheme returns the manual resolver's scheme.
|
2023-05-29 21:03:29 +00:00
|
|
|
func (r *Resolver) Scheme() string {
|
|
|
|
return r.scheme
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveNow is a noop for Resolver.
|
|
|
|
func (r *Resolver) ResolveNow(o resolver.ResolveNowOptions) {
|
|
|
|
r.ResolveNowCallback(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close is a noop for Resolver.
|
|
|
|
func (r *Resolver) Close() {
|
|
|
|
r.CloseCallback()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateState calls CC.UpdateState.
|
|
|
|
func (r *Resolver) UpdateState(s resolver.State) {
|
2023-06-19 21:01:00 +00:00
|
|
|
r.mu.Lock()
|
2023-09-11 20:46:57 +00:00
|
|
|
err := r.CC.UpdateState(s)
|
2023-10-25 10:07:57 +00:00
|
|
|
r.lastSeenState = &s
|
2023-06-19 21:01:00 +00:00
|
|
|
r.mu.Unlock()
|
2023-09-11 20:46:57 +00:00
|
|
|
r.UpdateStateCallback(err)
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReportError calls CC.ReportError.
|
|
|
|
func (r *Resolver) ReportError(err error) {
|
2023-06-19 21:01:00 +00:00
|
|
|
r.mu.Lock()
|
2023-05-29 21:03:29 +00:00
|
|
|
r.CC.ReportError(err)
|
2023-06-19 21:01:00 +00:00
|
|
|
r.mu.Unlock()
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|