Skip to content

Commit

Permalink
crocochrome: get rid of the context of session
Browse files Browse the repository at this point in the history
  • Loading branch information
nadiamoe committed May 28, 2024
1 parent cf2d684 commit a39de6c
Showing 1 changed file with 17 additions and 48 deletions.
65 changes: 17 additions & 48 deletions crocochrome.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
type Supervisor struct {
opts Options
logger *slog.Logger
sessions map[string]*Session
sessions map[string]context.CancelFunc
sessionsMtx sync.Mutex
}

Expand Down Expand Up @@ -55,7 +55,7 @@ func New(logger *slog.Logger, opts Options) *Supervisor {
return &Supervisor{
opts: opts.withDefaults(),
logger: logger,
sessions: map[string]*Session{},
sessions: map[string]context.CancelFunc{},
}
}

Expand Down Expand Up @@ -83,12 +83,7 @@ func (s *Supervisor) Session() (SessionInfo, error) {
logger := s.logger.With("sessionID", id)

ctx, cancel := context.WithTimeout(context.Background(), s.opts.SessionTimeout)
sess := &Session{
ctx: ctx,
cancelCtx: cancel,
logger: logger,
}
s.sessions[id] = sess
s.sessions[id] = cancel

context.AfterFunc(ctx, func() {
// Ensure that we call supervisor.delete to remove the session from the map its context is cancelled by
Expand All @@ -99,9 +94,18 @@ func (s *Supervisor) Session() (SessionInfo, error) {

go func() {
logger.Debug("starting session")
err := sess.Run(s.opts.ChromiumPath, s.opts.ChromiumPort)
if err != nil {
logger.Error("session terminated with an error", "err", err)
cmd := exec.CommandContext(ctx,
s.opts.ChromiumPath,
"--headless",
"--remote-debugging-address=0.0.0.0",
"--remote-debugging-port="+s.opts.ChromiumPort,
"--no-sandbox", // TODO: Sandbox.
)
cmd.Env = []string{}

err := cmd.Run()
if err != nil && !errors.Is(ctx.Err(), context.Canceled) {
logger.Error("running chromium", "err", err)
}
}()

Expand Down Expand Up @@ -129,8 +133,8 @@ func (s *Supervisor) Delete(sessionID string) bool {
// delete cancels a session's context and removes it from the map, without locking the mutex.
// It must be used only inside functions that already grab the lock.
func (s *Supervisor) delete(sessionID string) bool {
if session, found := s.sessions[sessionID]; found {
session.Cancel()
if cancelSession, found := s.sessions[sessionID]; found {
cancelSession()
delete(s.sessions, sessionID)
return true
}
Expand All @@ -146,41 +150,6 @@ func (s *Supervisor) killExisting() {
}
}

// Session is a running instance of Chromium. It uses a (light, non-chroot) sandbox to run it.
type Session struct {
ctx context.Context
cancelCtx context.CancelFunc
logger *slog.Logger
}

// Cancel cancels the context associated with this session.
func (s *Session) Cancel() {
s.cancelCtx()
}

// Run starts the chromium process, and blocks until it returns. Upon termination, it automatically calls Session.Cancel
// to clean up any pending operations.
// By cancelling this context, the Session is removed from the Supervisor's map.
func (s *Session) Run(chromiumPath, chromiumPort string) error {
defer s.Cancel()

cmd := exec.CommandContext(s.ctx,
chromiumPath,
"--headless",
"--remote-debugging-address=0.0.0.0",
"--remote-debugging-port="+chromiumPort,
"--no-sandbox", // TODO: Sandbox.
)
cmd.Env = []string{}

err := cmd.Run()
if err != nil && !errors.Is(s.ctx.Err(), context.Canceled) {
return fmt.Errorf("running chromium: %w", err)
}

return nil
}

// randString returns 12 random hex characters.
func randString() string {
const IDLen = 12
Expand Down

0 comments on commit a39de6c

Please sign in to comment.