Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

another improve on dmsghttp handler #283

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions cmd/dmsg-discovery/commands/dmsg-discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ skywire dmsg disc --sk $(tail -n1 dmsgd-config.json)`,
}
}()
if !pk.Null() {
servers := getServers(ctx, a, log)
servers := getServers(ctx, a, log, dmsgServerType)
config := &dmsg.Config{
MinSessions: 0, // listen on all available servers
UpdateInterval: dmsg.DefaultUpdateInterval,
Expand All @@ -174,7 +174,7 @@ skywire dmsg disc --sk $(tail -n1 dmsgd-config.json)`,
}
}()

go updateServers(ctx, a, dClient, dmsgDC, log)
go updateServers(ctx, a, dClient, dmsgDC, log, dmsgServerType)

go func() {
if err = dmsghttp.ListenAndServe(ctx, sk, a, dClient, dmsg.DefaultDmsgHTTPPort, dmsgDC, log); err != nil {
Expand Down Expand Up @@ -210,14 +210,24 @@ func prepareDB(ctx context.Context, log *logging.Logger) store.Storer {
return db
}

func getServers(ctx context.Context, a *api.API, log logrus.FieldLogger) (servers []*disc.Entry) {
func getServers(ctx context.Context, a *api.API, log logrus.FieldLogger, dmsgServerType string) (servers []*disc.Entry) {
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
servers, err := a.AllServers(ctx, log)
if err != nil {
log.WithError(err).Fatal("Error getting dmsg-servers.")
}
// filtered dmsg servers by their type
if dmsgServerType != "" {
var filteredServers []*disc.Entry
for _, server := range servers {
if server.Server.ServerType == dmsgServerType {
filteredServers = append(filteredServers, server)
}
}
servers = filteredServers
}
if len(servers) > 0 {
return servers
}
Expand All @@ -226,12 +236,12 @@ func getServers(ctx context.Context, a *api.API, log logrus.FieldLogger) (server
case <-ctx.Done():
return []*disc.Entry{}
case <-ticker.C:
getServers(ctx, a, log)
getServers(ctx, a, log, dmsgServerType)
}
}
}

func updateServers(ctx context.Context, a *api.API, dClient disc.APIClient, dmsgC *dmsg.Client, log logrus.FieldLogger) {
func updateServers(ctx context.Context, a *api.API, dClient disc.APIClient, dmsgC *dmsg.Client, log logrus.FieldLogger, dmsgServerType string) {
ticker := time.NewTicker(time.Minute * 10)
defer ticker.Stop()
for {
Expand All @@ -244,6 +254,16 @@ func updateServers(ctx context.Context, a *api.API, dClient disc.APIClient, dmsg
log.WithError(err).Error("Error getting dmsg-servers.")
break
}
// filtered dmsg servers by their type
if dmsgServerType != "" {
var filteredServers []*disc.Entry
for _, server := range servers {
if server.Server.ServerType == dmsgServerType {
filteredServers = append(filteredServers, server)
}
}
servers = filteredServers
}
for _, server := range servers {
dClient.PostEntry(ctx, server) //nolint
err := dmsgC.EnsureSession(ctx, server)
Expand Down
Loading