Skip to content

Commit

Permalink
Merge pull request #32 from gateway-fm/optional_path
Browse files Browse the repository at this point in the history
Optional Path implementation
  • Loading branch information
misnaged authored Aug 15, 2023
2 parents 12540ad + 24e937b commit 449b13d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
30 changes: 26 additions & 4 deletions discovery/consul_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package discovery

import (
"fmt"

consul "github.com/hashicorp/consul/api"

"github.com/gateway-fm/scriptorium/logger"
Expand All @@ -15,16 +14,18 @@ import (
type ConsulDiscovery struct {
client *consul.Client
transport TransportProtocol
opts *DiscoveryOpts
}

// NewConsulDiscovery create new Consul-driven
// service Discovery
func NewConsulDiscovery(transport TransportProtocol, addr ...string) (IServiceDiscovery, error) {
func NewConsulDiscovery(transport TransportProtocol, opts *DiscoveryOpts, addr ...string) (IServiceDiscovery, error) {
if len(addr) != 1 {
return nil, ErrInvalidArgumentsLength{length: len(addr), driver: DriverConsul}
}

config := consul.DefaultConfig()

if addr[0] != "" {
config.Address = addr[0]
}
Expand All @@ -34,7 +35,22 @@ func NewConsulDiscovery(transport TransportProtocol, addr ...string) (IServiceDi
return nil, fmt.Errorf("connect to consul discovery: %w", err)
}

return &ConsulDiscovery{client: c, transport: transport}, nil
if opts == nil {
opts = NilDiscoveryOptions()
}
if opts.isOptional {
if opts.optionalPath == "" {
return nil, ErrEmptyOptionalPath
}
}

consulDiscovery := &ConsulDiscovery{
client: c,
transport: transport,
opts: opts,
}

return consulDiscovery, nil
}

// Discover and return list of the active
Expand Down Expand Up @@ -65,12 +81,18 @@ func (d *ConsulDiscovery) createNodesFromServices(consulServices []*consul.Servi
// instance from consul service
func (d *ConsulDiscovery) createServiceFromConsul(srv *consul.ServiceEntry) service.IService {
addr := d.transport.FormatAddress(srv.Service.Address)
addr = fmt.Sprintf("%s:%d", addr, srv.Service.Port)

if d.opts.isOptional && d.opts.optionalPath != "" {
addr = AddEndOrRemoveFirstSlashIfNeeded(addr) + AddEndOrRemoveFirstSlashIfNeeded(d.opts.optionalPath)
}

logger.Log().Debug(fmt.Sprintf("discovered new service: %s", addr))

tagsMap := make(map[string]struct{})
for _, t := range srv.Service.Tags {
tagsMap[t] = struct{}{}
}

return service.NewService(fmt.Sprintf("%s:%d", addr, srv.Service.Port), srv.Service.ID, tagsMap)
return service.NewService(addr, srv.Service.ID, tagsMap)
}
21 changes: 20 additions & 1 deletion discovery/discovery.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package discovery

import (
"errors"
"github.com/gateway-fm/service-pool/service"
)

Expand All @@ -10,9 +11,13 @@ type IServiceDiscovery interface {
// Discover service by given name
Discover(service string) ([]service.IService, error)
}
type DiscoveryOpts struct {
isOptional bool
optionalPath string
}

// Creator is discovery factory function
type Creator func(TransportProtocol, ...string) (IServiceDiscovery, error)
type Creator func(TransportProtocol, *DiscoveryOpts, ...string) (IServiceDiscovery, error)

// ParseDiscoveryDriver create new addresses discovery
// Creator based on given discovery driver
Expand All @@ -26,3 +31,17 @@ func ParseDiscoveryDriver(driver Driver) (Creator, error) {
return nil, ErrUnsupportedDriver{driver.String()}
}
}

func NewDiscoveryOpts(isPathOptional bool, optionalPath string) *DiscoveryOpts {
return &DiscoveryOpts{
isOptional: isPathOptional,
optionalPath: optionalPath,
}
}

// NilDiscoveryOptions to prevent nil pointers if there are no options
func NilDiscoveryOptions() *DiscoveryOpts {
return &DiscoveryOpts{}
}

var ErrEmptyOptionalPath = errors.New("optional path is empty")
6 changes: 4 additions & 2 deletions discovery/manual_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
type ManualDiscovery struct {
addresses []string
transport TransportProtocol
opts *DiscoveryOpts
}

// NewManualDiscovery create new manual
// NodesDiscovery with given addresses
func NewManualDiscovery(transport TransportProtocol, addrs ...string) (IServiceDiscovery, error) {
return &ManualDiscovery{addresses: addrs, transport: transport}, nil
func NewManualDiscovery(transport TransportProtocol, opts *DiscoveryOpts, addrs ...string) (IServiceDiscovery, error) {
opts = NilDiscoveryOptions()
return &ManualDiscovery{addresses: addrs, opts: opts, transport: transport}, nil
}

// Discover is discover and return list of the active
Expand Down
19 changes: 19 additions & 0 deletions discovery/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package discovery

import "fmt"

func AddEndOrRemoveFirstSlashIfNeeded(addr string) string {
b := []byte(addr)
if len(b) == 0 {
return ""
}
lastByte := b[len(b)-1]
if lastByte != '/' {
addr = fmt.Sprintf("%s/", addr)
}
if b[0] == '/' {
b = append(b[1:])
addr = string(b)
}
return addr
}
2 changes: 1 addition & 1 deletion testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func newHealthyService(addr string) service.IService {
}

func newServicesPool(discoveryInterval time.Duration, hcInterval time.Duration, mutationFunc func(srv service.IService) (service.IService, error)) IServicesPool {
manualDisc, _ := discovery.NewManualDiscovery(discovery.TransportHttp, "localhost")
manualDisc, _ := discovery.NewManualDiscovery(discovery.TransportHttp, nil, "localhost")

opts := &ServicesPoolsOpts{
Name: "TestServicePool",
Expand Down

0 comments on commit 449b13d

Please sign in to comment.