github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/router/router.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/tickoalcantara12/micro/v3/router/router.go
    14  
    15  package router
    16  
    17  import (
    18  	"errors"
    19  	"hash/fnv"
    20  )
    21  
    22  var (
    23  	// DefaultRouter implementation
    24  	DefaultRouter Router
    25  )
    26  
    27  var (
    28  	// DefaultLink is default network link
    29  	DefaultLink = "local"
    30  	// DefaultLocalMetric is default route cost for a local route
    31  	DefaultMetric int64 = 1
    32  	// DefaultNetwork is default micro network
    33  	DefaultNetwork = "micro"
    34  	// ErrRouteNotFound is returned when no route was found in the routing table
    35  	ErrRouteNotFound = errors.New("route not found")
    36  	// ErrDuplicateRoute is returned when the route already exists
    37  	ErrDuplicateRoute = errors.New("duplicate route")
    38  )
    39  
    40  // Router is an interface for a routing control plane
    41  type Router interface {
    42  	// Init initializes the router with options
    43  	Init(...Option) error
    44  	// Options returns the router options
    45  	Options() Options
    46  	// The routing table
    47  	Table() Table
    48  	// Lookup queries routes in the routing table
    49  	Lookup(service string, opts ...LookupOption) ([]Route, error)
    50  	// Watch returns a watcher which tracks updates to the routing table
    51  	Watch(opts ...WatchOption) (Watcher, error)
    52  	// Close the router
    53  	Close() error
    54  	// Returns the router implementation
    55  	String() string
    56  }
    57  
    58  // Table is an interface for routing table
    59  type Table interface {
    60  	// Create new route in the routing table
    61  	Create(Route) error
    62  	// Delete existing route from the routing table
    63  	Delete(Route) error
    64  	// Update route in the routing table
    65  	Update(Route) error
    66  	// Read is for querying the table
    67  	Read(...ReadOption) ([]Route, error)
    68  }
    69  
    70  // Option used by the router
    71  type Option func(*Options)
    72  
    73  // StatusCode defines router status
    74  type StatusCode int
    75  
    76  const (
    77  	// Running means the router is up and running
    78  	Running StatusCode = iota
    79  	// Stopped means the router has been stopped
    80  	Stopped
    81  	// Error means the router has encountered error
    82  	Error
    83  )
    84  
    85  // Route is a network route
    86  type Route struct {
    87  	// Service is destination service name
    88  	Service string
    89  	// Address is service node address
    90  	Address string
    91  	// Gateway is route gateway
    92  	Gateway string
    93  	// Network is network address
    94  	Network string
    95  	// Router is router id
    96  	Router string
    97  	// Link is network link
    98  	Link string
    99  	// Metric is the route cost metric
   100  	Metric int64
   101  	// Metadata for the route
   102  	Metadata map[string]string
   103  }
   104  
   105  // Hash returns route hash sum.
   106  func (r *Route) Hash() uint64 {
   107  	h := fnv.New64()
   108  	h.Reset()
   109  	h.Write([]byte(r.Service + r.Address + r.Gateway + r.Network + r.Router + r.Link))
   110  	return h.Sum64()
   111  }