go-micro.dev/v5@v5.12.0/server/rpc_helper.go (about)

     1  package server
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  
     7  	"go-micro.dev/v5/codec"
     8  	"go-micro.dev/v5/registry"
     9  )
    10  
    11  // setRegistered will set the service as registered safely.
    12  func (s *rpcServer) setRegistered(b bool) {
    13  	s.Lock()
    14  	defer s.Unlock()
    15  
    16  	s.registered = b
    17  }
    18  
    19  // isRegistered will check if the service has already been registered.
    20  func (s *rpcServer) isRegistered() bool {
    21  	s.RLock()
    22  	defer s.RUnlock()
    23  
    24  	return s.registered
    25  }
    26  
    27  // setStarted will set started state safely.
    28  func (s *rpcServer) setStarted(b bool) {
    29  	s.Lock()
    30  	defer s.Unlock()
    31  
    32  	s.started = b
    33  }
    34  
    35  // isStarted will check if the service has already been started.
    36  func (s *rpcServer) isStarted() bool {
    37  	s.RLock()
    38  	defer s.RUnlock()
    39  
    40  	return s.started
    41  }
    42  
    43  // setWg will set the waitgroup safely.
    44  func (s *rpcServer) setWg(wg *sync.WaitGroup) {
    45  	s.Lock()
    46  	defer s.Unlock()
    47  
    48  	s.wg = wg
    49  }
    50  
    51  // getWaitgroup returns the global waitgroup safely.
    52  func (s *rpcServer) getWg() *sync.WaitGroup {
    53  	s.RLock()
    54  	defer s.RUnlock()
    55  
    56  	return s.wg
    57  }
    58  
    59  // setOptsAddr will set the address in the service options safely.
    60  func (s *rpcServer) setOptsAddr(addr string) {
    61  	s.Lock()
    62  	defer s.Unlock()
    63  
    64  	s.opts.Address = addr
    65  }
    66  
    67  func (s *rpcServer) getCachedService() *registry.Service {
    68  	s.RLock()
    69  	defer s.RUnlock()
    70  
    71  	return s.rsvc
    72  }
    73  
    74  func (s *rpcServer) Options() Options {
    75  	s.RLock()
    76  	defer s.RUnlock()
    77  
    78  	return s.opts
    79  }
    80  
    81  // swapAddr swaps the address found in the config and the transport address.
    82  func (s *rpcServer) swapAddr(config Options, addr string) string {
    83  	s.Lock()
    84  	defer s.Unlock()
    85  
    86  	a := config.Address
    87  	s.opts.Address = addr
    88  	return a
    89  }
    90  
    91  func (s *rpcServer) newCodec(contentType string) (codec.NewCodec, error) {
    92  	if cf, ok := s.opts.Codecs[contentType]; ok {
    93  		return cf, nil
    94  	}
    95  
    96  	if cf, ok := DefaultCodecs[contentType]; ok {
    97  		return cf, nil
    98  	}
    99  
   100  	return nil, fmt.Errorf("unsupported Content-Type: %s", contentType)
   101  }