gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/router/router.go (about)

     1  // Package router provides a network routing control plane
     2  package router
     3  
     4  import (
     5  	"time"
     6  )
     7  
     8  var (
     9  	// DefaultAddress is default router address
    10  	DefaultAddress = ":9093"
    11  	// DefaultName is default router service name
    12  	DefaultName = "go.micro.router"
    13  	// DefaultNetwork is default micro network
    14  	DefaultNetwork = "go.micro"
    15  	// DefaultRouter is default network router
    16  	DefaultRouter = NewRouter()
    17  )
    18  
    19  // Router is an interface for a routing control plane
    20  type Router interface {
    21  	// Init initializes the router with options
    22  	Init(...Option) error
    23  	// Options returns the router options
    24  	Options() Options
    25  	// The routing table
    26  	Table() Table
    27  	// Advertise advertises routes to the network
    28  	Advertise() (<-chan *Advert, error)
    29  	// Process processes incoming adverts
    30  	Process(*Advert) error
    31  	// Solicit advertises the whole routing table to the network
    32  	Solicit() error
    33  	// Lookup queries routes in the routing table
    34  	Lookup(...QueryOption) ([]Route, error)
    35  	// Watch returns a watcher which tracks updates to the routing table
    36  	Watch(opts ...WatchOption) (Watcher, error)
    37  	// Start starts the router
    38  	Start() error
    39  	// Status returns router status
    40  	Status() Status
    41  	// Stop stops the router
    42  	Stop() error
    43  	// Returns the router implementation
    44  	String() string
    45  }
    46  
    47  // Table is an interface for routing table
    48  type Table interface {
    49  	// Create new route in the routing table
    50  	Create(Route) error
    51  	// Delete existing route from the routing table
    52  	Delete(Route) error
    53  	// Update route in the routing table
    54  	Update(Route) error
    55  	// List all routes in the table
    56  	List() ([]Route, error)
    57  	// Query routes in the routing table
    58  	Query(...QueryOption) ([]Route, error)
    59  }
    60  
    61  // Option used by the router
    62  type Option func(*Options)
    63  
    64  // StatusCode defines router status
    65  type StatusCode int
    66  
    67  const (
    68  	// Running means the router is up and running
    69  	Running StatusCode = iota
    70  	// Advertising means the router is advertising
    71  	Advertising
    72  	// Stopped means the router has been stopped
    73  	Stopped
    74  	// Error means the router has encountered error
    75  	Error
    76  )
    77  
    78  func (s StatusCode) String() string {
    79  	switch s {
    80  	case Running:
    81  		return "running"
    82  	case Advertising:
    83  		return "advertising"
    84  	case Stopped:
    85  		return "stopped"
    86  	case Error:
    87  		return "error"
    88  	default:
    89  		return "unknown"
    90  	}
    91  }
    92  
    93  // Status is router status
    94  type Status struct {
    95  	// Code defines router status
    96  	Code StatusCode
    97  	// Error contains error description
    98  	Error error
    99  }
   100  
   101  // String returns human readable status
   102  func (s Status) String() string {
   103  	return s.Code.String()
   104  }
   105  
   106  // AdvertType is route advertisement type
   107  type AdvertType int
   108  
   109  const (
   110  	// Announce is advertised when the router announces itself
   111  	Announce AdvertType = iota
   112  	// RouteUpdate advertises route updates
   113  	RouteUpdate
   114  	// Solicitation indicates routes were solicited
   115  	Solicitation
   116  )
   117  
   118  // String returns human readable advertisement type
   119  func (t AdvertType) String() string {
   120  	switch t {
   121  	case Announce:
   122  		return "announce"
   123  	case RouteUpdate:
   124  		return "update"
   125  	case Solicitation:
   126  		return "solicitation"
   127  	default:
   128  		return "unknown"
   129  	}
   130  }
   131  
   132  // Advert contains a list of events advertised by the router to the network
   133  type Advert struct {
   134  	// Id is the router Id
   135  	Id string
   136  	// Type is type of advert
   137  	Type AdvertType
   138  	// Timestamp marks the time when the update is sent
   139  	Timestamp time.Time
   140  	// TTL is Advert TTL
   141  	TTL time.Duration
   142  	// Events is a list of routing table events to advertise
   143  	Events []*Event
   144  }
   145  
   146  // Strategy is route advertisement strategy
   147  type Strategy int
   148  
   149  const (
   150  	// AdvertiseAll advertises all routes to the network
   151  	AdvertiseAll Strategy = iota
   152  	// AdvertiseBest advertises optimal routes to the network
   153  	AdvertiseBest
   154  	// AdvertiseLocal will only advertise the local routes
   155  	AdvertiseLocal
   156  	// AdvertiseNone will not advertise any routes
   157  	AdvertiseNone
   158  )
   159  
   160  // String returns human readable Strategy
   161  func (s Strategy) String() string {
   162  	switch s {
   163  	case AdvertiseAll:
   164  		return "all"
   165  	case AdvertiseBest:
   166  		return "best"
   167  	case AdvertiseLocal:
   168  		return "local"
   169  	case AdvertiseNone:
   170  		return "none"
   171  	default:
   172  		return "unknown"
   173  	}
   174  }
   175  
   176  // NewRouter creates new Router and returns it
   177  func NewRouter(opts ...Option) Router {
   178  	return newRouter(opts...)
   179  }