github.com/micro/go-micro/v2@v2.9.1/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 28 Advertise() (<-chan *Advert, error) 29 // Process processes incoming adverts 30 Process(*Advert) error 31 // Lookup queries routes in the routing table 32 Lookup(...QueryOption) ([]Route, error) 33 // Watch returns a watcher which tracks updates to the routing table 34 Watch(opts ...WatchOption) (Watcher, error) 35 // Start starts the router 36 Start() error 37 // Stop stops the router 38 Stop() error 39 // Returns the router implementation 40 String() string 41 } 42 43 // Table is an interface for routing table 44 type Table interface { 45 // Create new route in the routing table 46 Create(Route) error 47 // Delete existing route from the routing table 48 Delete(Route) error 49 // Update route in the routing table 50 Update(Route) error 51 // List all routes in the table 52 List() ([]Route, error) 53 // Query routes in the routing table 54 Query(...QueryOption) ([]Route, error) 55 } 56 57 // Option used by the router 58 type Option func(*Options) 59 60 // StatusCode defines router status 61 type StatusCode int 62 63 const ( 64 // Running means the router is up and running 65 Running StatusCode = iota 66 // Advertising means the router is advertising 67 Advertising 68 // Stopped means the router has been stopped 69 Stopped 70 // Error means the router has encountered error 71 Error 72 ) 73 74 // AdvertType is route advertisement type 75 type AdvertType int 76 77 const ( 78 // Announce is advertised when the router announces itself 79 Announce AdvertType = iota 80 // RouteUpdate advertises route updates 81 RouteUpdate 82 ) 83 84 // String returns human readable advertisement type 85 func (t AdvertType) String() string { 86 switch t { 87 case Announce: 88 return "announce" 89 case RouteUpdate: 90 return "update" 91 default: 92 return "unknown" 93 } 94 } 95 96 // Advert contains a list of events advertised by the router to the network 97 type Advert struct { 98 // Id is the router Id 99 Id string 100 // Type is type of advert 101 Type AdvertType 102 // Timestamp marks the time when the update is sent 103 Timestamp time.Time 104 // TTL is Advert TTL 105 TTL time.Duration 106 // Events is a list of routing table events to advertise 107 Events []*Event 108 } 109 110 // Strategy is route advertisement strategy 111 type Strategy int 112 113 // TODO: remove the "Advertise" prefix from these 114 const ( 115 // AdvertiseAll advertises all routes to the network 116 AdvertiseAll Strategy = iota 117 // AdvertiseBest advertises optimal routes to the network 118 AdvertiseBest 119 // AdvertiseLocal will only advertise the local routes 120 AdvertiseLocal 121 // AdvertiseNone will not advertise any routes 122 AdvertiseNone 123 ) 124 125 // String returns human readable Strategy 126 func (s Strategy) String() string { 127 switch s { 128 case AdvertiseAll: 129 return "all" 130 case AdvertiseBest: 131 return "best" 132 case AdvertiseLocal: 133 return "local" 134 case AdvertiseNone: 135 return "none" 136 default: 137 return "unknown" 138 } 139 } 140 141 // NewRouter creates new Router and returns it 142 func NewRouter(opts ...Option) Router { 143 return newRouter(opts...) 144 }