Skip to content

Commit

Permalink
refactor(daemon): rename Start to ListenAndServe and implement Serve
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Dec 6, 2024
1 parent 1c081ab commit db3dfdc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/soft/serve/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (s *Server) Start() error {
errg, _ := errgroup.WithContext(s.ctx)
errg.Go(func() error {
s.logger.Print("Starting Git daemon", "addr", s.Config.Git.ListenAddr)
if err := s.GitDaemon.Start(); !errors.Is(err, daemon.ErrServerClosed) {
if err := s.GitDaemon.ListenAndServe(); !errors.Is(err, daemon.ErrServerClosed) {
return err
}
return nil
Expand Down
45 changes: 26 additions & 19 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ var ErrServerClosed = fmt.Errorf("git: %w", net.ErrClosed)
// GitDaemon represents a Git daemon.
type GitDaemon struct {
ctx context.Context
listener net.Listener
addr string
finished chan struct{}
conns connections
Expand All @@ -52,6 +51,7 @@ type GitDaemon struct {
wg sync.WaitGroup
once sync.Once
logger *log.Logger
done bool // indicates if the server has been closed
}

// NewDaemon returns a new Git daemon.
Expand All @@ -70,26 +70,31 @@ func NewGitDaemon(ctx context.Context) (*GitDaemon, error) {
return d, nil
}

// Start starts the Git TCP daemon.
func (d *GitDaemon) Start() error {
// listen on the socket
{
listener, err := net.Listen("tcp", d.addr)
if err != nil {
return err
}
d.listener = listener
// ListenAndServe starts the Git TCP daemon.
func (d *GitDaemon) ListenAndServe() error {
if d.done {
return ErrServerClosed
}
listener, err := net.Listen("tcp", d.addr)
if err != nil {
return err

Check failure on line 80 in pkg/daemon/daemon.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func net.Listen(network string, address string) (net.Listener, error) (wrapcheck)
}
return d.Serve(listener)
}

// close eventual connections to the socket
defer d.listener.Close() // nolint: errcheck
// Serve listens on the TCP network address and serves Git requests.
func (d *GitDaemon) Serve(listener net.Listener) error {
if d.done {
return ErrServerClosed
}

d.wg.Add(1)
defer d.wg.Done()
defer listener.Close() //nolint:errcheck

var tempDelay time.Duration
for {
conn, err := d.listener.Accept()
conn, err := listener.Accept()
if err != nil {
select {
case <-d.finished:
Expand Down Expand Up @@ -311,19 +316,21 @@ func (d *GitDaemon) Close() error {
}

// closeListener closes the listener and the finished channel.
func (d *GitDaemon) closeListener() (err error) {
func (d *GitDaemon) closeListener() error {
if d.done {
return ErrServerClosed
}
d.once.Do(func() {
close(d.finished)
err = d.listener.Close()
d.done = true
})
return
return nil
}

// Shutdown gracefully shuts down the daemon.
func (d *GitDaemon) Shutdown(ctx context.Context) error {
// in the case when git daemon was never started
if d.listener == nil {
return nil
if d.done {
return ErrServerClosed
}

err := d.closeListener()
Expand Down
2 changes: 1 addition & 1 deletion pkg/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestMain(m *testing.M) {
}
testDaemon = d
go func() {
if err := d.Start(); err != ErrServerClosed {
if err := d.ListenAndServe(); err != ErrServerClosed {
log.Fatal(err)
}
}()
Expand Down

0 comments on commit db3dfdc

Please sign in to comment.