mirror of
				https://github.com/go-micro/go-micro.git
				synced 2025-10-30 23:27:41 +02:00 
			
		
		
		
	Allow setting of timeout for registry
This commit is contained in:
		| @@ -12,6 +12,7 @@ import ( | ||||
| type consulRegistry struct { | ||||
| 	Address string | ||||
| 	Client  *consul.Client | ||||
| 	Options Options | ||||
| } | ||||
|  | ||||
| func encodeEndpoints(en []*Endpoint) []string { | ||||
| @@ -69,7 +70,20 @@ func decodeMetadata(tags []string) map[string]string { | ||||
| } | ||||
|  | ||||
| func newConsulRegistry(addrs []string, opts ...Option) Registry { | ||||
| 	var opt Options | ||||
| 	for _, o := range opts { | ||||
| 		o(&opt) | ||||
| 	} | ||||
|  | ||||
| 	// use default config | ||||
| 	config := consul.DefaultConfig() | ||||
|  | ||||
| 	// set timeout | ||||
| 	if opt.Timeout > 0 { | ||||
| 		config.HttpClient.Timeout = opt.Timeout | ||||
| 	} | ||||
|  | ||||
| 	// check if there are any addrs | ||||
| 	if len(addrs) > 0 { | ||||
| 		addr, port, err := net.SplitHostPort(addrs[0]) | ||||
| 		if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" { | ||||
| @@ -79,11 +93,14 @@ func newConsulRegistry(addrs []string, opts ...Option) Registry { | ||||
| 			config.Address = fmt.Sprintf("%s:%s", addr, port) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// create the client | ||||
| 	client, _ := consul.NewClient(config) | ||||
|  | ||||
| 	cr := &consulRegistry{ | ||||
| 		Address: config.Address, | ||||
| 		Client:  client, | ||||
| 		Options: opt, | ||||
| 	} | ||||
|  | ||||
| 	return cr | ||||
| @@ -178,7 +195,7 @@ func (c *consulRegistry) GetService(name string) ([]*Service, error) { | ||||
| } | ||||
|  | ||||
| func (c *consulRegistry) ListServices() ([]*Service, error) { | ||||
| 	rsp, _, err := c.Client.Catalog().Services(&consul.QueryOptions{}) | ||||
| 	rsp, _, err := c.Client.Catalog().Services(nil) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|   | ||||
							
								
								
									
										15
									
								
								registry/options.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								registry/options.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| package registry | ||||
|  | ||||
| import ( | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| type Options struct { | ||||
| 	Timeout time.Duration | ||||
| } | ||||
|  | ||||
| func Timeout(t time.Duration) Option { | ||||
| 	return func(o *Options) { | ||||
| 		o.Timeout = t | ||||
| 	} | ||||
| } | ||||
| @@ -8,9 +8,7 @@ type Registry interface { | ||||
| 	Watch() (Watcher, error) | ||||
| } | ||||
|  | ||||
| type options struct{} | ||||
|  | ||||
| type Option func(*options) | ||||
| type Option func(*Options) | ||||
|  | ||||
| var ( | ||||
| 	DefaultRegistry = newConsulRegistry([]string{}) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user