github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/connectors/routing/routing_config.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package routing
    22  
    23  import (
    24  	"strings"
    25  
    26  	"github.com/gobwas/glob"
    27  	"github.com/pkg/errors"
    28  )
    29  
    30  // Rule stores routing rule
    31  // to decide which connector the Connector talks to
    32  type Rule struct {
    33  	Scope      string
    34  	NamePrefix string
    35  	Connector  string
    36  	GlobMatch  glob.Glob
    37  }
    38  
    39  // NewRule initializes Rule
    40  func NewRule(scope, namePrefix, connector string) (*Rule, error) {
    41  	if namePrefix == "" {
    42  		return nil, errors.New("namePrefix could not be empty, should be defined in yaml file")
    43  	}
    44  
    45  	if scope == "" {
    46  		return nil, errors.New("scope could not be empty, should be defined in yaml file")
    47  	}
    48  
    49  	if strings.HasPrefix(namePrefix, "*") && len(namePrefix) != 1 {
    50  		// we don't support name like '*.service.v1'
    51  		return nil, errors.Errorf(
    52  			"namePrefix like %v is not supported, cannot put * at the beginning of namePrefix.", namePrefix)
    53  	}
    54  
    55  	globMatch := glob.MustCompile(namePrefix)
    56  
    57  	return &Rule{NamePrefix: namePrefix, Scope: scope, Connector: connector, GlobMatch: globMatch}, nil
    58  }
    59  
    60  // RouteTo implements the method to choose the matched connector
    61  func (r *Rule) RouteTo(scope string, namePrefix string) bool {
    62  	// scope should be an exact match
    63  	if r.Scope != scope {
    64  		return false
    65  	}
    66  
    67  	// namePrefix could be glob match, but first check if there's an exact match
    68  	if r.NamePrefix == namePrefix {
    69  		return true
    70  	}
    71  
    72  	if r.GlobMatch.Match(namePrefix) {
    73  		return true
    74  	}
    75  
    76  	return false
    77  }