dubbo.apache.org/dubbo-go/v3@v3.1.1/registry/event.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package registry
    19  
    20  import (
    21  	"fmt"
    22  	"math/rand"
    23  	"time"
    24  )
    25  
    26  import (
    27  	gxset "github.com/dubbogo/gost/container/set"
    28  	"github.com/dubbogo/gost/gof/observer"
    29  )
    30  
    31  import (
    32  	"dubbo.apache.org/dubbo-go/v3/common"
    33  	"dubbo.apache.org/dubbo-go/v3/remoting"
    34  )
    35  
    36  type KeyFunc func(*common.URL) string
    37  
    38  func init() {
    39  	rand.Seed(time.Now().UnixNano())
    40  }
    41  
    42  // ServiceEvent includes create, update, delete event
    43  type ServiceEvent struct {
    44  	Action  remoting.EventType
    45  	Service *common.URL
    46  	key     string // store the key for Service.Key()
    47  	updated bool   // If the url is updated, such as Merged.
    48  	KeyFunc KeyFunc
    49  }
    50  
    51  // String return the description of event
    52  func (e *ServiceEvent) String() string {
    53  	return fmt.Sprintf("ServiceEvent{Action{%s}, Path{%s}, Key{%s}}", e.Action, e.Service, e.key)
    54  }
    55  
    56  // Update updates the url with the merged URL. Work with Updated() can reduce the process
    57  // of some merging URL.
    58  func (e *ServiceEvent) Update(url *common.URL) {
    59  	e.Service = url
    60  	e.updated = true
    61  }
    62  
    63  // Updated checks if the url is updated. If the serviceEvent is updated, then it don't need
    64  // merge url again.
    65  func (e *ServiceEvent) Updated() bool {
    66  	return e.updated
    67  }
    68  
    69  // Key generates the key for service.Key(). It is cached once.
    70  func (e *ServiceEvent) Key() string {
    71  	if len(e.key) > 0 {
    72  		return e.key
    73  	}
    74  	if e.KeyFunc == nil {
    75  		e.key = e.Service.GetCacheInvokerMapKey()
    76  	} else {
    77  		e.key = e.KeyFunc(e.Service)
    78  	}
    79  	return e.key
    80  }
    81  
    82  // ServiceInstancesChangedEvent represents service instances make some changing
    83  type ServiceInstancesChangedEvent struct {
    84  	observer.BaseEvent
    85  	ServiceName string
    86  	Instances   []ServiceInstance
    87  }
    88  
    89  // String return the description of the event
    90  func (s *ServiceInstancesChangedEvent) String() string {
    91  	return fmt.Sprintf("ServiceInstancesChangedEvent[source=%s]", s.ServiceName)
    92  }
    93  
    94  // NewServiceInstancesChangedEvent will create the ServiceInstanceChangedEvent instance
    95  func NewServiceInstancesChangedEvent(serviceName string, instances []ServiceInstance) *ServiceInstancesChangedEvent {
    96  	return &ServiceInstancesChangedEvent{
    97  		BaseEvent: observer.BaseEvent{
    98  			Source:    serviceName,
    99  			Timestamp: time.Now(),
   100  		},
   101  		ServiceName: serviceName,
   102  		Instances:   instances,
   103  	}
   104  }
   105  
   106  type ServiceMappingChangeEvent struct {
   107  	observer.BaseEvent
   108  	ServiceKey   string
   109  	ServiceNames *gxset.HashSet
   110  }
   111  
   112  // NewServiceMappingChangedEvent will create the ServiceMappingChangeEvent
   113  func NewServiceMappingChangedEvent(serviceKey string, serviceNames *gxset.HashSet) *ServiceMappingChangeEvent {
   114  	return &ServiceMappingChangeEvent{
   115  		BaseEvent: observer.BaseEvent{
   116  			Source:    serviceKey,
   117  			Timestamp: time.Now(),
   118  		},
   119  		ServiceKey:   serviceKey,
   120  		ServiceNames: serviceNames,
   121  	}
   122  }
   123  
   124  func (sm *ServiceMappingChangeEvent) GetServiceNames() *gxset.HashSet {
   125  	return sm.ServiceNames
   126  }
   127  
   128  func (sm *ServiceMappingChangeEvent) GetServiceKey() string {
   129  	return sm.ServiceKey
   130  }