Skip to content

Commit 4beb5f9

Browse files
Worked on FCFS and session management
1 parent 27c6067 commit 4beb5f9

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

cmd/laclm/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func run(ctx context.Context) error {
177177
call all the kill switches with context
178178
*/
179179

180-
/* graceful shutdown of http server */
180+
/* graceful shutdown of http server - 5 seconds for allowing completion current API requests */
181181
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
182182
defer shutdownCancel()
183183

config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ authentication:
4141
address: "ldap://openldap:389" # Use the service name from docker-compose
4242
admin_dn: ${LACLM_LDAP_ADMIN_DN}
4343
admin_password: ${LACLM_LDAP_ADMIN_PASSWORD}
44-
search_base: "cn=Princeton Plainsboro Hospital,dc=myorg,dc=local"
44+
search_base: "dc=example,dc=org"
4545

4646
backend_security:
4747
jwt_secret_token: ${JWT_SECRET_TOKEN}

internal/scheduler/fcfs/fcfs.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
package fcfs
22

3-
import "github.com/PythonHacker24/linux-acl-management-backend/internal/session"
3+
import (
4+
"runtime"
45

6+
"github.com/PythonHacker24/linux-acl-management-backend/internal/session"
7+
)
8+
9+
/* spawns a new FCFS scheduler */
510
func NewFCFSScheduler(sm *session.Manager) *FCFSScheduler {
11+
/* calculate max workers */
12+
/* TODO: make it configurable and dynamic */
13+
maxWorkers := runtime.NumCPU()
614
return &FCFSScheduler{
715
sessionManager: sm,
16+
maxWorkers: maxWorkers,
17+
semaphore: make(chan struct{}, maxWorkers),
818
}
919
}

internal/scheduler/fcfs/model.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import (
44
"context"
55

66
"github.com/PythonHacker24/linux-acl-management-backend/internal/session"
7+
"go.uber.org/zap"
78
)
89

9-
/* FCFS Scheduler attached with session.Manager */
10+
/* FCFS Scheduler attached with curSession.Manager */
1011
type FCFSScheduler struct {
11-
sessionManager *session.Manager
12+
curSessionManager *session.Manager
13+
maxWorkers int
14+
15+
/* for limiting spawning of goroutines */
16+
semaphore chan struct{}
1217
}
1318

1419
/* run the fcfs scheduler with context */
@@ -23,7 +28,39 @@ func (f *FCFSScheduler) Run(ctx context.Context) error {
2328
/* in case default is working hard - ctx is passed here so it must attempt to quit */
2429
default:
2530
/* RULE: ctx is propogates all over the coming functions */
26-
31+
32+
curSession := f.curSessionManager.GetNextSession()
33+
if curSession == nil {
34+
/* might need a delay of 10 ms */
35+
continue
36+
}
37+
38+
/* check if transaction queue of the session is empty */
39+
curSession.Mutex.Lock()
40+
if curSession.TransactionQueue.Len() == 0 {
41+
curSession.Mutex.Unlock()
42+
continue
43+
}
44+
45+
/* get a transaction from the session to process */
46+
transaction := curSession.TransactionQueue.Remove(curSession.TransactionQueue.Front())
47+
curSession.Mutex.Unlock()
48+
49+
/* block if all workers are busy */
50+
f.semaphore <- struct{}{}
51+
52+
/* go routine is available to be spawned */
53+
go func(curSession *session.Session, transaction interface{}) {
54+
/* defer clearing the semaphore channel */
55+
defer func() { <-f.semaphore }()
56+
57+
/* process the transaction */
58+
if err := f.processTransaction; err != nil {
59+
zap.L().Error("Faild to process transaction",
60+
zap.Error(err),
61+
)
62+
}
63+
}(curSession, transaction)
2764
}
2865
}
2966
}

0 commit comments

Comments
 (0)