You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.1 KiB
65 lines
2.1 KiB
// Copyright © 2023 OpenIM. All rights reserved.
|
|
//
|
|
// 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 zookeeper
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
|
|
|
"google.golang.org/grpc/resolver"
|
|
)
|
|
|
|
type Resolver struct {
|
|
target resolver.Target
|
|
cc resolver.ClientConn
|
|
addrs []resolver.Address
|
|
|
|
getConnsRemote func(serviceName string) (conns []resolver.Address, err error)
|
|
}
|
|
|
|
func (r *Resolver) ResolveNow(o resolver.ResolveNowOptions) {
|
|
log.ZDebug(context.Background(), "start resolve now", "target", r.target)
|
|
newConns, err := r.getConnsRemote(strings.TrimLeft(r.target.URL.Path, "/"))
|
|
if err != nil {
|
|
log.ZError(context.Background(), "resolve now error", err, "target", r.target)
|
|
return
|
|
}
|
|
r.addrs = newConns
|
|
if err := r.cc.UpdateState(resolver.State{Addresses: newConns}); err != nil {
|
|
log.ZError(context.Background(), "UpdateState error", err, "conns", newConns)
|
|
}
|
|
log.ZDebug(context.Background(), "resolve now finished", "target", r.target, "conns", r.addrs)
|
|
}
|
|
|
|
func (s *Resolver) Close() {}
|
|
|
|
func (s *ZkClient) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
|
|
s.logger.Printf("build resolver: %+v, cc: %+v", target, cc)
|
|
r := &Resolver{}
|
|
r.target = target
|
|
r.cc = cc
|
|
r.getConnsRemote = s.GetConnsRemote
|
|
r.ResolveNow(resolver.ResolveNowOptions{})
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
s.resolvers[strings.TrimLeft(target.URL.Path, "/")] = r
|
|
s.logger.Printf("build resolver finished: %+v, cc: %+v", target, cc)
|
|
return r, nil
|
|
}
|
|
|
|
func (s *ZkClient) Scheme() string { return s.scheme }
|