Skip to content

Commit 0c60b91

Browse files
committed
fix: OCM client configurability
Instead of creating a client each time a function is called, create client once Signed-off-by: Mahdi Baghbani <mahdi-baghbani@azadehafzar.io>
1 parent 18e5b6f commit 0c60b91

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

internal/http/services/sciencemesh/sciencemesh.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ func (s *svc) Close() error {
6060
}
6161

6262
type config struct {
63-
Prefix string `mapstructure:"prefix"`
64-
GatewaySvc string `mapstructure:"gatewaysvc" validate:"required"`
65-
ProviderDomain string `mapstructure:"provider_domain" validate:"required"`
66-
MeshDirectoryURL string `mapstructure:"mesh_directory_url"`
67-
OCMMountPoint string `mapstructure:"ocm_mount_point"`
68-
FederationsFile string `mapstructure:"federations_file"`
69-
Events EventOptions `mapstructure:"events"`
63+
Prefix string `mapstructure:"prefix"`
64+
GatewaySvc string `mapstructure:"gatewaysvc" validate:"required"`
65+
ProviderDomain string `mapstructure:"provider_domain" validate:"required"`
66+
MeshDirectoryURL string `mapstructure:"mesh_directory_url"`
67+
OCMMountPoint string `mapstructure:"ocm_mount_point"`
68+
FederationsFile string `mapstructure:"federations_file"`
69+
OCMClientTimeout int `mapstructure:"ocm_client_timeout"`
70+
OCMClientInsecure bool `mapstructure:"ocm_client_insecure"`
71+
Events EventOptions `mapstructure:"events"`
7072
}
7173

7274
// EventOptions are the configurable options for events
@@ -90,6 +92,9 @@ func (c *config) ApplyDefaults() {
9092
if c.FederationsFile == "" {
9193
c.FederationsFile = "/etc/revad/federations.json"
9294
}
95+
if c.OCMClientTimeout == 0 {
96+
c.OCMClientTimeout = 10
97+
}
9398

9499
c.GatewaySvc = sharedconf.GetGatewaySVC(c.GatewaySvc)
95100
}

internal/http/services/sciencemesh/wayf.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535

3636
type wayfHandler struct {
3737
federations []Federation
38+
ocmClient *ocmd.OCMClient
3839
}
3940

4041
type Federation struct {
@@ -71,6 +72,13 @@ type DiscoverResponse struct {
7172
func (h *wayfHandler) init(c *config) error {
7273
log := appctx.GetLogger(context.Background())
7374

75+
// Create OCM client for discovery from config
76+
h.ocmClient = ocmd.NewClient(time.Duration(c.OCMClientTimeout)*time.Second, c.OCMClientInsecure)
77+
log.Debug().
78+
Int("timeout_seconds", c.OCMClientTimeout).
79+
Bool("insecure", c.OCMClientInsecure).
80+
Msg("Created OCM client for discovery")
81+
7482
log.Debug().Str("file", c.FederationsFile).Msg("Initializing WAYF handler with federations file")
7583

7684
data, err := os.ReadFile(c.FederationsFile)
@@ -92,10 +100,6 @@ func (h *wayfHandler) init(c *config) error {
92100

93101
log.Debug().Int("federations_count", len(fileData)).Msg("Loaded federations from file")
94102

95-
// Create OCM client for discovery
96-
ocmClient := ocmd.NewClient(10*time.Second, false)
97-
log.Debug().Msg("Created OCM client for discovery")
98-
99103
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
100104
defer cancel()
101105

@@ -120,7 +124,7 @@ func (h *wayfHandler) init(c *config) error {
120124
log.Debug().Str("federation", fed.Federation).Str("server", srv.DisplayName).Str("url", srv.URL).Msg("Discovering server")
121125

122126
// Discover inviteAcceptDialog from OCM endpoint
123-
disco, err := ocmClient.Discover(ctx, srv.URL)
127+
disco, err := h.ocmClient.Discover(ctx, srv.URL)
124128
if err != nil {
125129
log.Warn().Err(err).
126130
Str("federation", fed.Federation).
@@ -219,11 +223,8 @@ func (h *wayfHandler) DiscoverProvider(w http.ResponseWriter, r *http.Request) {
219223
return
220224
}
221225

222-
// Create OCM client with timeout
223-
ocmClient := ocmd.NewClient(10*time.Second, false)
224-
225226
log.Debug().Str("domain", domain).Msg("Attempting OCM discovery")
226-
disco, err := ocmClient.Discover(ctx, domain)
227+
disco, err := h.ocmClient.Discover(ctx, domain)
227228
if err != nil {
228229
log.Warn().Err(err).Str("domain", domain).Msg("Discovery failed")
229230
reqres.WriteError(w, r, reqres.APIErrorNotFound,

0 commit comments

Comments
 (0)