blob: 7abbcebe0fedc7c3c4d1751479c6a70ca5181c9f [file] [log] [blame]
sslobodr392ebd52019-01-18 12:41:49 -05001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
sslobodr392ebd52019-01-18 12:41:49 -050016
17package afrouter
18
19import (
sslobodr392ebd52019-01-18 12:41:49 -050020 "errors"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040021 "fmt"
sslobodr392ebd52019-01-18 12:41:49 -050022 "google.golang.org/grpc"
23)
24
Kent Hagerman1e9061e2019-05-21 16:01:21 -040025var allRouters = make(map[string]Router)
sslobodr392ebd52019-01-18 12:41:49 -050026
27// The router interface
28type Router interface {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040029 Name() string
Scott Baker112b0d42019-08-22 08:32:26 -070030
31 // Route() returns a backend and a connection. The connection is optional and if unspecified, then any
32 // connection on the backend may be used.
33 Route(interface{}) (*backend, *connection)
34
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040035 Service() string
Kent Hagerman03b58992019-08-29 17:21:03 -040036 IsStreaming(string) (bool, bool)
Kent Hagerman1e9061e2019-05-21 16:01:21 -040037 BackendCluster(string, string) (*cluster, error)
38 FindBackendCluster(string) *cluster
sslobodr392ebd52019-01-18 12:41:49 -050039 ReplyHandler(interface{}) error
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040040 GetMetaKeyVal(serverStream grpc.ServerStream) (string, string, error)
sslobodr392ebd52019-01-18 12:41:49 -050041}
42
sslobodrcd37bc52019-01-24 11:47:16 -050043func newRouter(config *RouterConfig) (Router, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040044 r, err := newMethodRouter(config)
45 if err == nil {
sslobodr392ebd52019-01-18 12:41:49 -050046 allRouters[r.Name()] = r
47 }
48 return r, err
49}
50
sslobodrcd37bc52019-01-24 11:47:16 -050051func newSubRouter(rconf *RouterConfig, config *RouteConfig) (Router, error) {
Kent Hagerman1e9061e2019-05-21 16:01:21 -040052 switch config.Type {
53 case RouteTypeRpcAffinityMessage:
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040054 r, err := newAffinityRouter(rconf, config)
sslobodr5f0b5a32019-01-24 07:45:19 -050055 if err == nil {
56 allRouters[rconf.Name+config.Name] = r
57 }
58 return r, err
Kent Hagerman1e9061e2019-05-21 16:01:21 -040059 case RouteTypeBinding:
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040060 r, err := newBindingRouter(rconf, config)
sslobodr5f0b5a32019-01-24 07:45:19 -050061 if err == nil {
62 allRouters[rconf.Name+config.Name] = r
63 }
64 return r, err
Kent Hagerman1e9061e2019-05-21 16:01:21 -040065 case RouteTypeRoundRobin:
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040066 r, err := newRoundRobinRouter(rconf, config)
sslobodr5f0b5a32019-01-24 07:45:19 -050067 if err == nil {
68 allRouters[rconf.Name+config.Name] = r
69 }
70 return r, err
Scott Baker112b0d42019-08-22 08:32:26 -070071 case RouteTypeSource:
72 r, err := newSourceRouter(rconf, config)
73 if err == nil {
74 allRouters[rconf.Name+config.Name] = r
75 }
76 return r, err
sslobodr5f0b5a32019-01-24 07:45:19 -050077 default:
78 return nil, errors.New(fmt.Sprintf("Internal error, undefined router type: %s", config.Type))
79 }
sslobodr392ebd52019-01-18 12:41:49 -050080}