github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/infrastructure/infrastructure_AddServiceServer.go (about)

     1  package infrastructure
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  )
     7  
     8  // ErrServiceNotExists occurs when you trying to add a server to service that
     9  // not exists
    10  var ErrServiceNotExists = errors.New("service is not exists")
    11  
    12  // AddServiceServer registers server to a service in the infrastructure
    13  func (i *Infrastructure) AddServiceServer(serviceName string, addr Address) error {
    14  	i.Lock.RLock()
    15  	if _, ok := i.Services[serviceName]; !ok {
    16  		i.Lock.RUnlock()
    17  		return ErrServiceNotExists
    18  	}
    19  	i.Lock.RUnlock()
    20  	i.Lock.Lock()
    21  	if i.Services[serviceName].Addresses == nil {
    22  		i.Services[serviceName].Addresses = make([]Address, 0)
    23  	}
    24  	i.UpdateTimestamp = time.Now().UnixNano()
    25  	i.Services[serviceName].Addresses = append(i.Services[serviceName].Addresses, addr)
    26  	i.Lock.Unlock()
    27  	return nil
    28  }