Skip to content

Commit

Permalink
style: move functions to private.go
Browse files Browse the repository at this point in the history
Signed-off-by: black-desk <[email protected]>
  • Loading branch information
black-desk committed Oct 20, 2023
1 parent abda2b0 commit ad5f56e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 75 deletions.
74 changes: 74 additions & 0 deletions pkg/cgfsmon/private.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,86 @@ package cgfsmon
import (
"context"
"errors"
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/black-desk/cgtproxy/pkg/types"
fsevents "github.com/tywkeene/go-fsevents"
)

func (m *CGroupFSMonitor) walkFn(ctx context.Context) func(path string, d fs.DirEntry, err error) error {
return func(path string, d fs.DirEntry, err error) error {
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
m.log.Debug(
"Cgroup had been removed.",
"path", path,
)
err = nil
}
m.log.Errorw(
"Errors occurred while first time going through cgroupfs.",
"path", path,
"error", err,
)
err = nil
}

if !d.IsDir() {
return nil
}

cgEvent := &types.CGroupEvent{
Path: path,
EventType: types.CgroupEventTypeNew,
}

err = m.send(ctx, cgEvent)
if err != nil {
return err
}

return nil
}
}

func (m *CGroupFSMonitor) walk(ctx context.Context, path string) {
err := filepath.WalkDir(path, m.walkFn(ctx))
if err == nil {
return
}

return
}

func (m *CGroupFSMonitor) send(ctx context.Context, cgEvent *types.CGroupEvent) (err error) {
path := strings.TrimRight(cgEvent.Path, "/")
cgEvent.Path = path

if cgEvent.Path == string(m.root) {
// NOTE: Ignore cgroup root.
return nil
}

m.log.Debugw("New cgroup envent.",
"event", cgEvent,
)

select {
case <-ctx.Done():
err = ctx.Err()
return
case m.events <- *cgEvent:
m.log.Debugw("Cgroup event sent.",
"path", path,
)
}

return
}

type handle struct {
ctx context.Context
mon *CGroupFSMonitor
Expand Down
75 changes: 0 additions & 75 deletions pkg/cgfsmon/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package cgfsmon

import (
"context"
"errors"
"io/fs"
"path/filepath"
"strings"

"github.com/black-desk/cgtproxy/pkg/types"
. "github.com/black-desk/lib/go/errwrap"
Expand All @@ -16,77 +12,6 @@ func (w *CGroupFSMonitor) Events() <-chan types.CGroupEvent {
return w.events
}

func (m *CGroupFSMonitor) walkFn(ctx context.Context) func(path string, d fs.DirEntry, err error) error {
return func(path string, d fs.DirEntry, err error) error {
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
m.log.Debug(
"Cgroup had been removed.",
"path", path,
)
err = nil
}
m.log.Errorw(
"Errors occurred while first time going through cgroupfs.",
"path", path,
"error", err,
)
err = nil
}

if !d.IsDir() {
return nil
}

cgEvent := &types.CGroupEvent{
Path: path,
EventType: types.CgroupEventTypeNew,
}

err = m.send(ctx, cgEvent)
if err != nil {
return err
}

return nil
}
}

func (m *CGroupFSMonitor) walk(ctx context.Context, path string) {
err := filepath.WalkDir(path, m.walkFn(ctx))
if err == nil {
return
}

return
}

func (m *CGroupFSMonitor) send(ctx context.Context, cgEvent *types.CGroupEvent) (err error) {
path := strings.TrimRight(cgEvent.Path, "/")
cgEvent.Path = path

if cgEvent.Path == string(m.root) {
// NOTE: Ignore cgroup root.
return nil
}

m.log.Debugw("New cgroup envent.",
"event", cgEvent,
)

select {
case <-ctx.Done():
err = ctx.Err()
return
case m.events <- *cgEvent:
m.log.Debugw("Cgroup event sent.",
"path", path,
)
}

return
}

func (w *CGroupFSMonitor) Run(ctx context.Context) (err error) {
defer Wrap(&err, "running filesystem watcher")
defer close(w.events)
Expand Down

0 comments on commit ad5f56e

Please sign in to comment.