blob: 1bd2d6e05c4bf8d715c016e2b932d04c00bbb0c3 [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
sslobodr392ebd52019-01-18 12:41:49 -050030 Route(interface{}) *backend
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040031 Service() string
Kent Hagerman03b58992019-08-29 17:21:03 -040032 IsStreaming(string) (bool, bool)
Kent Hagerman1e9061e2019-05-21 16:01:21 -040033 BackendCluster(string, string) (*cluster, error)
34 FindBackendCluster(string) *cluster
sslobodr392ebd52019-01-18 12:41:49 -050035 ReplyHandler(interface{}) error
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040036 GetMetaKeyVal(serverStream grpc.ServerStream) (string, string, error)
sslobodr392ebd52019-01-18 12:41:49 -050037}
38
sslobodrcd37bc52019-01-24 11:47:16 -050039func newRouter(config *RouterConfig) (Router, error) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040040 r, err := newMethodRouter(config)
41 if err == nil {
sslobodr392ebd52019-01-18 12:41:49 -050042 allRouters[r.Name()] = r
43 }
44 return r, err
45}
46
sslobodrcd37bc52019-01-24 11:47:16 -050047func newSubRouter(rconf *RouterConfig, config *RouteConfig) (Router, error) {
Kent Hagerman1e9061e2019-05-21 16:01:21 -040048 switch config.Type {
49 case RouteTypeRpcAffinityMessage:
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040050 r, err := newAffinityRouter(rconf, config)
sslobodr5f0b5a32019-01-24 07:45:19 -050051 if err == nil {
52 allRouters[rconf.Name+config.Name] = r
53 }
54 return r, err
Kent Hagerman1e9061e2019-05-21 16:01:21 -040055 case RouteTypeBinding:
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040056 r, err := newBindingRouter(rconf, config)
sslobodr5f0b5a32019-01-24 07:45:19 -050057 if err == nil {
58 allRouters[rconf.Name+config.Name] = r
59 }
60 return r, err
Kent Hagerman1e9061e2019-05-21 16:01:21 -040061 case RouteTypeRoundRobin:
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040062 r, err := newRoundRobinRouter(rconf, config)
sslobodr5f0b5a32019-01-24 07:45:19 -050063 if err == nil {
64 allRouters[rconf.Name+config.Name] = r
65 }
66 return r, err
67 default:
68 return nil, errors.New(fmt.Sprintf("Internal error, undefined router type: %s", config.Type))
69 }
sslobodr392ebd52019-01-18 12:41:49 -050070}