Skip to content

Commit

Permalink
remove wait times on single binary boot
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <jkoberg@owncloud.com>
  • Loading branch information
kobergj committed Jan 30, 2024
1 parent d73351a commit 606e8bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/speed-up-starttime.md
@@ -0,0 +1,5 @@
Enhancement: Improve ocis single binary start

Removes waiting times when starting the single binary. Improves ocis single binary boot time from 8s to 800ms

https://github.com/owncloud/ocis/pull/8320
24 changes: 15 additions & 9 deletions ocis/pkg/runtime/service/service.go
Expand Up @@ -13,6 +13,7 @@ import (
"syscall"
"time"

"github.com/cs3org/reva/v2/pkg/events/stream"
"github.com/mohae/deepcopy"
"github.com/olekukonko/tablewriter"
"github.com/thejerf/suture/v4"
Expand Down Expand Up @@ -64,10 +65,6 @@ import (
var (
// runset keeps track of which services to start supervised.
runset map[string]struct{}
// time to wait after starting the preliminary services
_preliminaryDelay = 6 * time.Second
// time to wait between starting service groups (preliminary, main, delayed)
_startDelay = 2 * time.Second
)

type serviceFuncMap map[string]func(*ociscfg.Config) suture.Service
Expand Down Expand Up @@ -378,14 +375,14 @@ func Start(o ...Option) error {

if err = rpc.Register(s); err != nil {
if s != nil {
s.Log.Fatal().Err(err)
s.Log.Fatal().Err(err).Msg("could not register rpc service")
}
}
rpc.HandleHTTP()

l, err := net.Listen("tcp", net.JoinHostPort(s.cfg.Runtime.Host, s.cfg.Runtime.Port))
if err != nil {
s.Log.Fatal().Err(err)
s.Log.Fatal().Err(err).Msg("could not start listener")
}

defer func() {
Expand Down Expand Up @@ -415,8 +412,10 @@ func Start(o ...Option) error {
// trap will block on halt channel for interruptions.
go trap(s, halt)

// grace period for preliminary services to get up
time.Sleep(_preliminaryDelay)
// wait for preliminary services (aka nats) to get up
if err = pingNats(s.cfg); err != nil {
s.Log.Fatal().Err(err).Msg("could not connect to nats")
}

// schedule services that we are sure don't have interdependencies.
scheduleServiceTokens(s, s.ServicesRegistry)
Expand All @@ -425,7 +424,6 @@ func Start(o ...Option) error {
scheduleServiceTokens(s, s.Additional)

// add services with delayed execution.
time.Sleep(_startDelay)
scheduleServiceTokens(s, s.Delayed)

return http.Serve(l, nil)
Expand Down Expand Up @@ -516,3 +514,11 @@ func trap(s *Service, halt chan os.Signal) {
s.Log.Debug().Str("service", "runtime service").Msgf("terminating with signal: %v", s)
os.Exit(0)
}

// pingNats will attempt to connect to nats, blocking until a connection is established
func pingNats(cfg *ociscfg.Config) error {
// We need to get a natsconfig from somewhere. We can use any one.
evcfg := cfg.Postprocessing.Postprocessing.Events
_, err := stream.NatsFromConfig("initial", true, stream.NatsConfig(evcfg))
return err
}

0 comments on commit 606e8bc

Please sign in to comment.