sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [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 | // gRPC affinity router with active/active backends |
| 17 | |
| 18 | package afrouter |
| 19 | |
| 20 | import ( |
| 21 | "fmt" |
| 22 | "errors" |
| 23 | "google.golang.org/grpc" |
| 24 | ) |
| 25 | |
| 26 | const ( |
| 27 | RT_RPC_AFFINITY_MESSAGE = iota+1 |
| 28 | RT_RPC_AFFINITY_HEADER = iota+1 |
| 29 | RT_BINDING = iota+1 |
| 30 | RT_ROUND_ROBIN = iota+1 |
| 31 | ) |
| 32 | |
| 33 | // String names for display in error messages. |
| 34 | var rTypeNames = []string{"","rpc_affinity_message","rpc_affinity_header","binding", "round_robin"} |
| 35 | var rAssnNames = []string{"","round_robin"} |
| 36 | |
| 37 | var allRouters map[string]Router = make(map[string]Router) |
| 38 | |
| 39 | // The router interface |
| 40 | type Router interface { |
| 41 | Name() (string) |
| 42 | Route(interface{}) *backend |
| 43 | Service() (string) |
| 44 | BackendCluster(string, string) (*backendCluster, error) |
| 45 | FindBackendCluster(string) (*backendCluster) |
| 46 | ReplyHandler(interface{}) error |
| 47 | GetMetaKeyVal(serverStream grpc.ServerStream) (string,string,error) |
| 48 | } |
| 49 | |
| 50 | func NewRouter(config *RouterConfig) (Router, error) { |
| 51 | r,err := newMethodRouter(config) |
| 52 | if err == nil { |
| 53 | allRouters[r.Name()] = r |
| 54 | } |
| 55 | return r, err |
| 56 | } |
| 57 | |
| 58 | func newRouter(rconf *RouterConfig, config *RouteConfig) (Router, error) { |
| 59 | idx := strIndex(rTypeNames, config.Type) |
| 60 | //for idx := range(rTypeNames) { |
| 61 | //if config.Type == rTypeNames[idx] { |
| 62 | switch idx { |
| 63 | case RT_RPC_AFFINITY_MESSAGE: |
| 64 | r,err := NewAffinityRouter(rconf, config) |
| 65 | if err == nil { |
| 66 | allRouters[rconf.Name+config.Name] = r |
| 67 | } |
| 68 | return r, err |
| 69 | case RT_BINDING: |
| 70 | r,err := NewBindingRouter(rconf, config) |
| 71 | if err == nil { |
| 72 | allRouters[rconf.Name+config.Name] = r |
| 73 | } |
| 74 | return r, err |
| 75 | case RT_ROUND_ROBIN: |
| 76 | r,err := NewRoundRobinRouter(rconf, config) |
| 77 | if err == nil { |
| 78 | allRouters[rconf.Name+config.Name] = r |
| 79 | } |
| 80 | return r, err |
| 81 | default: |
| 82 | return nil, errors.New(fmt.Sprintf("Internal error, undefined router type: %s", config.Type)) |
| 83 | } |
| 84 | |
| 85 | //} |
| 86 | //} |
| 87 | return nil, errors.New(fmt.Sprintf("Unrecognized router type '%s'",config.Type)) |
| 88 | } |