-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support use porter without consul
- Loading branch information
Showing
13 changed files
with
219 additions
and
89 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/tuihub/librarian/internal/conf" | ||
|
||
"github.com/go-kratos/kratos/v2/registry" | ||
) | ||
|
||
type staticDiscovery struct { | ||
serviceInstances []*registry.ServiceInstance | ||
watcherCount int | ||
watcherCh chan struct{} | ||
} | ||
|
||
func newStaticDiscovery(c *conf.Porter) (*staticDiscovery, error) { | ||
if c == nil { | ||
c = new(conf.Porter) | ||
} | ||
var serviceInstances []*registry.ServiceInstance | ||
for i, addr := range c.GetAddress() { | ||
parsed, err := url.Parse(addr) | ||
if err != nil { | ||
return nil, fmt.Errorf("porter address %s invalid: %w", addr, err) | ||
} | ||
if parsed.Scheme != "grpc" && parsed.Scheme != "grpcs" { | ||
return nil, fmt.Errorf("porter address %s is not a valid gRPC address", addr) | ||
} | ||
serviceInstances = append(serviceInstances, ®istry.ServiceInstance{ | ||
ID: fmt.Sprintf("porter-%d", i), | ||
Name: "porter", | ||
Version: "", | ||
Metadata: nil, | ||
Endpoints: []string{addr}, | ||
}) | ||
} | ||
return &staticDiscovery{ | ||
serviceInstances: serviceInstances, | ||
watcherCount: 0, | ||
watcherCh: make(chan struct{}), | ||
}, nil | ||
} | ||
|
||
func (s *staticDiscovery) GetService(ctx context.Context, serviceName string) ([]*registry.ServiceInstance, error) { | ||
return s.serviceInstances, nil | ||
} | ||
|
||
func (s *staticDiscovery) Watch(ctx context.Context, serviceName string) (registry.Watcher, error) { | ||
return s, nil | ||
} | ||
|
||
func (s *staticDiscovery) Next() ([]*registry.ServiceInstance, error) { | ||
if s.watcherCount > 0 { | ||
<-s.watcherCh | ||
} | ||
s.watcherCount++ | ||
return s.serviceInstances, nil | ||
} | ||
|
||
func (s *staticDiscovery) Stop() error { | ||
s.watcherCh <- struct{}{} | ||
return nil | ||
} | ||
|
||
func (s *staticDiscovery) GetAliveInstances() ([]string, error) { | ||
res := make([]string, 0, len(s.serviceInstances)) | ||
for _, instance := range s.serviceInstances { | ||
parsed, err := url.Parse(instance.Endpoints[0]) | ||
if err != nil { | ||
continue | ||
} | ||
res = append(res, fmt.Sprintf("%s:%s", parsed.Hostname(), parsed.Port())) | ||
} | ||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.