Skip to content

update code solution#16

Open
batman-do wants to merge 2 commits intoEngineerProOrg:scott/sample-gormfrom
batman-do:scott/sample-gorm
Open

update code solution#16
batman-do wants to merge 2 commits intoEngineerProOrg:scott/sample-gormfrom
batman-do:scott/sample-gorm

Conversation

@batman-do
Copy link

No description provided.

main.go Outdated
}

func generateSessionID() string {
timestamp := time.Now().UnixNano()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without seeding first, this one can be duplicate if 2 guys call in 2 different machines at the same time. Can use uuid to generate this id instead

}

func loginHandler(c *gin.Context) {
username := c.PostForm("username")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should check if this req have session id or not first, if already have then its mean he already logged in, no need to do anything

main.go Outdated
}

func pingHandler(c *gin.Context) {
sessionID := c.Query("session_id")
Copy link
Collaborator

@tpdongcs tpdongcs Jun 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

people usually store session id on cookie

main.go Outdated
}

// Acquire lock to ensure only one person can call /ping at a time
mutex.Lock()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is process level lock, not distributed lock (lock for multiple servers ) . Let read again distributed lock implementation by redis and implement it instead of using this lib

main.go Outdated

// Check if the user has exceeded the rate limit
callCountKey := fmt.Sprintf("call_count:%s", userName)
callCount, _ := lruCache.Get(callCountKey)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use rate limiter implementation by redis instead, if you use a local cache like this, if we have more than 2 instances, and user logged in, then the count number will be wrong (ex: call to instance A 2 time, to instance B 3 times. which mean total 5 times, )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants