Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package afrouter |
| 18 | |
| 19 | import ( |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 20 | "fmt" |
| 21 | "google.golang.org/grpc" |
| 22 | ) |
| 23 | |
| 24 | var allRouters = make(map[string]Router) |
| 25 | |
| 26 | // The router interface |
| 27 | type 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 | |
| 42 | func 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 | |
| 50 | func 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 Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 77 | return nil, fmt.Errorf("Internal error, undefined router type: %s", config.Type) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 78 | } |
| 79 | } |