Skip to content

Commit 2264c9d

Browse files
Updated FCFS scheduler and Goroutine spawning
1 parent 4beb5f9 commit 2264c9d

File tree

6 files changed

+46
-5
lines changed

6 files changed

+46
-5
lines changed

cmd/laclm/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/joho/godotenv"
1515
"github.com/spf13/cobra"
1616
"go.uber.org/zap"
17+
"go.uber.org/automaxprocs/maxprocs"
1718

1819
"github.com/PythonHacker24/linux-acl-management-backend/api/routes"
1920
"github.com/PythonHacker24/linux-acl-management-backend/config"
@@ -90,6 +91,13 @@ func exec() error {
9091
/* zap.L() can be used all over the code for global level logging */
9192
zap.L().Info("Logger Initiated ...")
9293

94+
/* calculate max procs accurately (runtime.GOMAXPROCS(0)) */
95+
if _, err := maxprocs.Set(); err != nil {
96+
zap.L().Error("automaxproces: failed to set GOMAXPROCS",
97+
zap.Error(err),
98+
)
99+
}
100+
93101
ctx, cancel := context.WithCancel(context.Background())
94102
defer cancel()
95103

config/app.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type App struct {
1313
DebugMode bool `yaml:"debug_mode,omitempty"`
1414
SessionTimeout int `yaml:"session_timeout,omitempty"`
1515
BasePath string `yaml:"base_path,omitempty"`
16+
MaxWorkers int `yaml:"max_workers,omitempty"`
1617
}
1718

1819
/* normalization function */
@@ -47,5 +48,7 @@ func (a *App) Normalize() error {
4748
`))
4849
}
4950

51+
/* max_workers can be zero - it will be adjusted scheduler */
52+
5053
return nil
5154
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/joho/godotenv v1.5.1 // indirect
1616
github.com/spf13/cobra v1.9.1 // indirect
1717
github.com/spf13/pflag v1.0.6 // indirect
18+
go.uber.org/automaxprocs v1.6.0 // indirect
1819
go.uber.org/multierr v1.11.0 // indirect
1920
go.uber.org/zap v1.27.0 // indirect
2021
golang.org/x/crypto v0.37.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
2424
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
2525
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
2626
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
27+
go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=
28+
go.uber.org/automaxprocs v1.6.0/go.mod h1:ifeIMSnPZuznNm6jmdzmU3/bfk01Fe2fotchwEFJ8r8=
2729
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
2830
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
2931
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=

internal/scheduler/fcfs/fcfs.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,31 @@ package fcfs
33
import (
44
"runtime"
55

6+
"github.com/PythonHacker24/linux-acl-management-backend/config"
67
"github.com/PythonHacker24/linux-acl-management-backend/internal/session"
78
)
89

910
/* spawns a new FCFS scheduler */
1011
func NewFCFSScheduler(sm *session.Manager) *FCFSScheduler {
1112
/* calculate max workers */
12-
/* TODO: make it configurable and dynamic */
13-
maxWorkers := runtime.NumCPU()
13+
maxProcs := runtime.GOMAXPROCS(0)
14+
maxWorkers := config.BackendConfig.AppInfo.MaxWorkers
15+
16+
/*
17+
incase of maxWorkers set less than or equal to 0,
18+
use 75% of GOMAXPROCS to prevent starvation to other processes
19+
*/
20+
if maxWorkers <= 0 {
21+
maxWorkers = int(float64(maxProcs) * 0.75)
22+
}
23+
24+
/* Prevent over-allocation */
25+
if maxWorkers > maxProcs {
26+
maxWorkers = maxProcs
27+
}
28+
1429
return &FCFSScheduler{
15-
sessionManager: sm,
30+
curSessionManager: sm,
1631
maxWorkers: maxWorkers,
1732
semaphore: make(chan struct{}, maxWorkers),
1833
}

internal/scheduler/fcfs/model.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import (
77
"go.uber.org/zap"
88
)
99

10+
/*
11+
Notes: the structure of scheduler is very modular
12+
Docs must be updated for replacing a certain scheduler module with another
13+
This includes installation of prebuilt module or developing a module
14+
*/
15+
1016
/* FCFS Scheduler attached with curSession.Manager */
1117
type FCFSScheduler struct {
1218
curSessionManager *session.Manager
@@ -29,6 +35,7 @@ func (f *FCFSScheduler) Run(ctx context.Context) error {
2935
default:
3036
/* RULE: ctx is propogates all over the coming functions */
3137

38+
/* get next session in the queue (round robin manner) */
3239
curSession := f.curSessionManager.GetNextSession()
3340
if curSession == nil {
3441
/* might need a delay of 10 ms */
@@ -54,8 +61,13 @@ func (f *FCFSScheduler) Run(ctx context.Context) error {
5461
/* defer clearing the semaphore channel */
5562
defer func() { <-f.semaphore }()
5663

57-
/* process the transaction */
58-
if err := f.processTransaction; err != nil {
64+
/*
65+
process the transaction
66+
* processTransaction handles transaction processing completely
67+
* now it is responsible now responsible to execute it
68+
* role of scheduler in handling transactions ends here
69+
*/
70+
if err := f.processTransaction(ctx, curSession, transaction); err != nil {
5971
zap.L().Error("Faild to process transaction",
6072
zap.Error(err),
6173
)

0 commit comments

Comments
 (0)