Home | History | Annotate | Download | only in client_channel
      1 /*
      2  *
      3  * Copyright 2015 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 #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_FACTORY_H
     20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_FACTORY_H
     21 
     22 #include <grpc/support/port_platform.h>
     23 
     24 #include "src/core/lib/iomgr/resolve_address.h"
     25 
     26 #include "src/core/ext/filters/client_channel/client_channel_factory.h"
     27 #include "src/core/ext/filters/client_channel/lb_policy.h"
     28 #include "src/core/ext/filters/client_channel/uri_parser.h"
     29 
     30 //
     31 // representation of an LB address
     32 //
     33 
     34 // Channel arg key for grpc_lb_addresses.
     35 #define GRPC_ARG_LB_ADDRESSES "grpc.lb_addresses"
     36 
     37 /** A resolved address alongside any LB related information associated with it.
     38  * \a user_data, if not NULL, contains opaque data meant to be consumed by the
     39  * gRPC LB policy. Note that no all LB policies support \a user_data as input.
     40  * Those who don't will simply ignore it and will correspondingly return NULL in
     41  * their namesake pick() output argument. */
     42 // TODO(roth): Once we figure out a better way of handling user_data in
     43 // LB policies, convert these structs to C++ classes.
     44 typedef struct grpc_lb_address {
     45   grpc_resolved_address address;
     46   bool is_balancer;
     47   char* balancer_name; /* For secure naming. */
     48   void* user_data;
     49 } grpc_lb_address;
     50 
     51 typedef struct grpc_lb_user_data_vtable {
     52   void* (*copy)(void*);
     53   void (*destroy)(void*);
     54   int (*cmp)(void*, void*);
     55 } grpc_lb_user_data_vtable;
     56 
     57 typedef struct grpc_lb_addresses {
     58   size_t num_addresses;
     59   grpc_lb_address* addresses;
     60   const grpc_lb_user_data_vtable* user_data_vtable;
     61 } grpc_lb_addresses;
     62 
     63 /** Returns a grpc_addresses struct with enough space for
     64     \a num_addresses addresses.  The \a user_data_vtable argument may be
     65     NULL if no user data will be added. */
     66 grpc_lb_addresses* grpc_lb_addresses_create(
     67     size_t num_addresses, const grpc_lb_user_data_vtable* user_data_vtable);
     68 
     69 /** Creates a copy of \a addresses. */
     70 grpc_lb_addresses* grpc_lb_addresses_copy(const grpc_lb_addresses* addresses);
     71 
     72 /** Sets the value of the address at index \a index of \a addresses.
     73  * \a address is a socket address of length \a address_len.
     74  * Takes ownership of \a balancer_name. */
     75 void grpc_lb_addresses_set_address(grpc_lb_addresses* addresses, size_t index,
     76                                    const void* address, size_t address_len,
     77                                    bool is_balancer, const char* balancer_name,
     78                                    void* user_data);
     79 
     80 /** Sets the value of the address at index \a index of \a addresses from \a uri.
     81  * Returns true upon success, false otherwise. Takes ownership of \a
     82  * balancer_name. */
     83 bool grpc_lb_addresses_set_address_from_uri(grpc_lb_addresses* addresses,
     84                                             size_t index, const grpc_uri* uri,
     85                                             bool is_balancer,
     86                                             const char* balancer_name,
     87                                             void* user_data);
     88 
     89 /** Compares \a addresses1 and \a addresses2. */
     90 int grpc_lb_addresses_cmp(const grpc_lb_addresses* addresses1,
     91                           const grpc_lb_addresses* addresses2);
     92 
     93 /** Destroys \a addresses. */
     94 void grpc_lb_addresses_destroy(grpc_lb_addresses* addresses);
     95 
     96 /** Returns a channel arg containing \a addresses. */
     97 grpc_arg grpc_lb_addresses_create_channel_arg(
     98     const grpc_lb_addresses* addresses);
     99 
    100 /** Returns the \a grpc_lb_addresses instance in \a channel_args or NULL */
    101 grpc_lb_addresses* grpc_lb_addresses_find_channel_arg(
    102     const grpc_channel_args* channel_args);
    103 
    104 // Returns true if addresses contains at least one balancer address.
    105 bool grpc_lb_addresses_contains_balancer_address(
    106     const grpc_lb_addresses& addresses);
    107 
    108 //
    109 // LB policy factory
    110 //
    111 
    112 namespace grpc_core {
    113 
    114 class LoadBalancingPolicyFactory {
    115  public:
    116   /// Returns a new LB policy instance.
    117   virtual OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy(
    118       const LoadBalancingPolicy::Args& args) const GRPC_ABSTRACT;
    119 
    120   /// Returns the LB policy name that this factory provides.
    121   /// Caller does NOT take ownership of result.
    122   virtual const char* name() const GRPC_ABSTRACT;
    123 
    124   virtual ~LoadBalancingPolicyFactory() {}
    125 
    126   GRPC_ABSTRACT_BASE_CLASS
    127 };
    128 
    129 }  // namespace grpc_core
    130 
    131 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_FACTORY_H */
    132