github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/router/watcher.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/watcher.go
    14  
    15  package router
    16  
    17  import (
    18  	"errors"
    19  	"time"
    20  )
    21  
    22  var (
    23  	// ErrWatcherStopped is returned when routing table watcher has been stopped
    24  	ErrWatcherStopped = errors.New("watcher stopped")
    25  )
    26  
    27  // EventType defines routing table event
    28  type EventType int
    29  
    30  const (
    31  	// Create is emitted when a new route has been created
    32  	Create EventType = iota
    33  	// Delete is emitted when an existing route has been deleted
    34  	Delete
    35  	// Update is emitted when an existing route has been updated
    36  	Update
    37  )
    38  
    39  // String returns human readable event type
    40  func (t EventType) String() string {
    41  	switch t {
    42  	case Create:
    43  		return "create"
    44  	case Delete:
    45  		return "delete"
    46  	case Update:
    47  		return "update"
    48  	default:
    49  		return "unknown"
    50  	}
    51  }
    52  
    53  // Event is returned by a call to Next on the watcher.
    54  type Event struct {
    55  	// Unique id of the event
    56  	Id string
    57  	// Type defines type of event
    58  	Type EventType
    59  	// Timestamp is event timestamp
    60  	Timestamp time.Time
    61  	// Route is table route
    62  	Route Route
    63  }
    64  
    65  // Watcher defines routing table watcher interface
    66  // Watcher returns updates to the routing table
    67  type Watcher interface {
    68  	// Next is a blocking call that returns watch result
    69  	Next() (*Event, error)
    70  	// Chan returns event channel
    71  	Chan() (<-chan *Event, error)
    72  	// Stop stops watcher
    73  	Stop()
    74  }
    75  
    76  // WatchOption is used to define what routes to watch in the table
    77  type WatchOption func(*WatchOptions)
    78  
    79  // WatchOptions are table watcher options
    80  // TODO: expand the options to watch based on other criteria
    81  type WatchOptions struct {
    82  	// Service allows to watch specific service routes
    83  	Service string
    84  }
    85  
    86  // WatchService sets what service routes to watch
    87  // Service is the microservice name
    88  func WatchService(s string) WatchOption {
    89  	return func(o *WatchOptions) {
    90  		o.Service = s
    91  	}
    92  }