blob: 890a89f5c971fdbcaf182747b2729378310bf39a [file] [log] [blame]
Craig Tiller9d0e0472015-06-23 09:12:48 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#ifndef GRPC_INTERNAL_CORE_CLIENT_CONFIG_LB_POLICY_H
35#define GRPC_INTERNAL_CORE_CLIENT_CONFIG_LB_POLICY_H
36
Craig Tilleraf691802015-06-23 14:57:07 -070037#include "src/core/client_config/subchannel.h"
Craig Tiller9d0e0472015-06-23 09:12:48 -070038
39/** A load balancing policy: specified by a vtable and a struct (which
40 is expected to be extended to contain some parameters) */
41typedef struct grpc_lb_policy grpc_lb_policy;
42typedef struct grpc_lb_policy_vtable grpc_lb_policy_vtable;
43
Craig Tiller7df28a72015-06-24 09:23:52 -070044typedef void (*grpc_lb_completion)(void *cb_arg, grpc_subchannel *subchannel,
Craig Tiller9d0e0472015-06-23 09:12:48 -070045 grpc_status_code status, const char *errmsg);
46
Craig Tiller7c0b4d72015-06-25 14:44:44 -070047struct grpc_lb_policy {
Craig Tillereb3b12e2015-06-26 14:42:49 -070048 const grpc_lb_policy_vtable *vtable;
Craig Tillerd7b68e72015-06-28 11:41:09 -070049 gpr_refcount refs;
Craig Tiller7c0b4d72015-06-25 14:44:44 -070050};
51
Craig Tiller9d0e0472015-06-23 09:12:48 -070052struct grpc_lb_policy_vtable {
Craig Tillerd7b68e72015-06-28 11:41:09 -070053 void (*destroy)(grpc_lb_policy *policy);
Craig Tiller9d0e0472015-06-23 09:12:48 -070054
55 void (*shutdown)(grpc_lb_policy *policy);
56
57 /** implement grpc_lb_policy_pick */
58 void (*pick)(grpc_lb_policy *policy, grpc_pollset *pollset,
Craig Tiller7df28a72015-06-24 09:23:52 -070059 grpc_metadata_batch *initial_metadata, grpc_subchannel **target,
Craig Tiller9d0e0472015-06-23 09:12:48 -070060 grpc_iomgr_closure *on_complete);
Craig Tillerc7b5f762015-06-27 11:48:42 -070061
62 /** broadcast a transport op to all subchannels */
63 void (*broadcast)(grpc_lb_policy *policy, grpc_transport_op *op);
64
65 /** check the current connectivity of the lb_policy */
66 grpc_connectivity_state (*check_connectivity)(grpc_lb_policy *policy);
67
68 /** call notify when the connectivity state of a channel changes from *state.
69 Updates *state with the new state of the policy */
70 void (*notify_on_state_change)(grpc_lb_policy *policy, grpc_connectivity_state *state, grpc_iomgr_closure *closure);
Craig Tiller9d0e0472015-06-23 09:12:48 -070071};
72
Craig Tillerd7b68e72015-06-28 11:41:09 -070073#define GRPC_LB_POLICY_REFCOUNT_DEBUG
74
75#ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG
76#define GRPC_LB_POLICY_REF(p, r) grpc_lb_policy_ref((p), __FILE__, __LINE__, (r))
77#define GRPC_LB_POLICY_UNREF(p, r) grpc_lb_policy_unref((p), __FILE__, __LINE__, (r))
78void grpc_lb_policy_ref(grpc_lb_policy *policy, const char *file, int line, const char *reason);
79void grpc_lb_policy_unref(grpc_lb_policy *policy, const char *file, int line, const char *reason);
80#else
81#define GRPC_LB_POLICY_REF(p, r) grpc_lb_policy_ref((p))
82#define GRPC_LB_POLICY_UNREF(p, r) grpc_lb_policy_unref((p))
Craig Tillerf5f17122015-06-25 08:47:26 -070083void grpc_lb_policy_ref(grpc_lb_policy *policy);
84void grpc_lb_policy_unref(grpc_lb_policy *policy);
Craig Tillerd7b68e72015-06-28 11:41:09 -070085#endif
86
87/** called by concrete implementations to initialize the base struct */
88void grpc_lb_policy_init(grpc_lb_policy *policy, const grpc_lb_policy_vtable *vtable);
Craig Tillerf5f17122015-06-25 08:47:26 -070089
Craig Tiller9d0e0472015-06-23 09:12:48 -070090/** Start shutting down (fail any pending picks) */
91void grpc_lb_policy_shutdown(grpc_lb_policy *policy);
92
93/** Given initial metadata in \a initial_metadata, find an appropriate
94 target for this rpc, and 'return' it by calling \a on_complete after setting
95 \a target.
96 Picking can be asynchronous. Any IO should be done under \a pollset. */
97void grpc_lb_policy_pick(grpc_lb_policy *policy, grpc_pollset *pollset,
98 grpc_metadata_batch *initial_metadata,
Craig Tilleraf691802015-06-23 14:57:07 -070099 grpc_subchannel **target,
Craig Tiller9d0e0472015-06-23 09:12:48 -0700100 grpc_iomgr_closure *on_complete);
101
Craig Tillerca3e9d32015-06-27 18:37:27 -0700102void grpc_lb_policy_broadcast(grpc_lb_policy *policy, grpc_transport_op *op);
103
Craig Tiller9d0e0472015-06-23 09:12:48 -0700104#endif /* GRPC_INTERNAL_CORE_CONFIG_LB_POLICY_H */