blob: f1ac3880b5c3aae6111c34e4d812fb9b066f26a0 [file] [log] [blame]
Scott Bakere7144bc2019-10-01 14:16:47 -07001/*
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 */
16
17package afrouter
18
19import (
Scott Bakere7144bc2019-10-01 14:16:47 -070020 "fmt"
21 "google.golang.org/grpc"
22)
23
24var allRouters = make(map[string]Router)
25
26// The router interface
27type Router interface {
28 Name() string
29
30 // Route() returns a backend and a connection. The connection is optional and if unspecified, then any
31 // connection on the backend may be used.
32 Route(interface{}) (*backend, *connection)
33
34 Service() string
35 IsStreaming(string) (bool, bool)
36 BackendCluster(string, string) (*cluster, error)
37 FindBackendCluster(string) *cluster
38 ReplyHandler(interface{}) error
39 GetMetaKeyVal(serverStream grpc.ServerStream) (string, string, error)
40}
41
42func newRouter(config *RouterConfig) (Router, error) {
43 r, err := newMethodRouter(config)
44 if err == nil {
45 allRouters[r.Name()] = r
46 }
47 return r, err
48}
49
50func newSubRouter(rconf *RouterConfig, config *RouteConfig) (Router, error) {
51 switch config.Type {
52 case RouteTypeRpcAffinityMessage:
53 r, err := newAffinityRouter(rconf, config)
54 if err == nil {
55 allRouters[rconf.Name+config.Name] = r
56 }
57 return r, err
58 case RouteTypeBinding:
59 r, err := newBindingRouter(rconf, config)
60 if err == nil {
61 allRouters[rconf.Name+config.Name] = r
62 }
63 return r, err
64 case RouteTypeRoundRobin:
65 r, err := newRoundRobinRouter(rconf, config)
66 if err == nil {
67 allRouters[rconf.Name+config.Name] = r
68 }
69 return r, err
70 case RouteTypeSource:
71 r, err := newSourceRouter(rconf, config)
72 if err == nil {
73 allRouters[rconf.Name+config.Name] = r
74 }
75 return r, err
76 default:
Scott Baker4989fe92019-10-09 17:03:06 -070077 return nil, fmt.Errorf("Internal error, undefined router type: %s", config.Type)
Scott Bakere7144bc2019-10-01 14:16:47 -070078 }
79}