...

Source file src/google.golang.org/grpc/channelz/service/service.go

Documentation: google.golang.org/grpc/channelz/service

     1  /*
     2   *
     3   * Copyright 2018 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  // Package service provides an implementation for channelz service server.
    20  package service
    21  
    22  import (
    23  	"context"
    24  
    25  	channelzgrpc "google.golang.org/grpc/channelz/grpc_channelz_v1"
    26  	channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1"
    27  
    28  	"google.golang.org/grpc"
    29  	"google.golang.org/grpc/channelz/internal/protoconv"
    30  	"google.golang.org/grpc/internal/channelz"
    31  )
    32  
    33  func init() {
    34  	channelz.TurnOn()
    35  }
    36  
    37  // RegisterChannelzServiceToServer registers the channelz service to the given server.
    38  //
    39  // Note: it is preferred to use the admin API
    40  // (https://pkg.go.dev/google.golang.org/grpc/admin#Register) instead to
    41  // register Channelz and other administrative services.
    42  func RegisterChannelzServiceToServer(s grpc.ServiceRegistrar) {
    43  	channelzgrpc.RegisterChannelzServer(s, newCZServer())
    44  }
    45  
    46  func newCZServer() channelzgrpc.ChannelzServer {
    47  	return &serverImpl{}
    48  }
    49  
    50  type serverImpl struct {
    51  	channelzgrpc.UnimplementedChannelzServer
    52  }
    53  
    54  func (s *serverImpl) GetChannel(ctx context.Context, req *channelzpb.GetChannelRequest) (*channelzpb.GetChannelResponse, error) {
    55  	ch, err := protoconv.GetChannel(req.GetChannelId())
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	return &channelzpb.GetChannelResponse{Channel: ch}, nil
    60  }
    61  
    62  func (s *serverImpl) GetTopChannels(ctx context.Context, req *channelzpb.GetTopChannelsRequest) (*channelzpb.GetTopChannelsResponse, error) {
    63  	resp := &channelzpb.GetTopChannelsResponse{}
    64  	resp.Channel, resp.End = protoconv.GetTopChannels(req.GetStartChannelId(), int(req.GetMaxResults()))
    65  	return resp, nil
    66  }
    67  
    68  func (s *serverImpl) GetServer(ctx context.Context, req *channelzpb.GetServerRequest) (*channelzpb.GetServerResponse, error) {
    69  	srv, err := protoconv.GetServer(req.GetServerId())
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	return &channelzpb.GetServerResponse{Server: srv}, nil
    74  }
    75  
    76  func (s *serverImpl) GetServers(ctx context.Context, req *channelzpb.GetServersRequest) (*channelzpb.GetServersResponse, error) {
    77  	resp := &channelzpb.GetServersResponse{}
    78  	resp.Server, resp.End = protoconv.GetServers(req.GetStartServerId(), int(req.GetMaxResults()))
    79  	return resp, nil
    80  }
    81  
    82  func (s *serverImpl) GetSubchannel(ctx context.Context, req *channelzpb.GetSubchannelRequest) (*channelzpb.GetSubchannelResponse, error) {
    83  	subChan, err := protoconv.GetSubChannel(req.GetSubchannelId())
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	return &channelzpb.GetSubchannelResponse{Subchannel: subChan}, nil
    88  }
    89  
    90  func (s *serverImpl) GetServerSockets(ctx context.Context, req *channelzpb.GetServerSocketsRequest) (*channelzpb.GetServerSocketsResponse, error) {
    91  	resp := &channelzpb.GetServerSocketsResponse{}
    92  	resp.SocketRef, resp.End = protoconv.GetServerSockets(req.GetServerId(), req.GetStartSocketId(), int(req.GetMaxResults()))
    93  	return resp, nil
    94  }
    95  
    96  func (s *serverImpl) GetSocket(ctx context.Context, req *channelzpb.GetSocketRequest) (*channelzpb.GetSocketResponse, error) {
    97  	socket, err := protoconv.GetSocket(req.GetSocketId())
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  	return &channelzpb.GetSocketResponse{Socket: socket}, nil
   102  }
   103  

View as plain text