-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Nguyen Minh Hoang]_Create db_to_gorm.go_Week 2 #27
base: scott/sample-gorm
Are you sure you want to change the base?
[Nguyen Minh Hoang]_Create db_to_gorm.go_Week 2 #27
Conversation
} | ||
func generateSessionID() string { | ||
// tạo ra 1 session ID ngẫu nhiên | ||
return fmt.Sprintf("%d", time.Now().UnixNano()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generate session id like this is not random, and may cause collision a lot, can use a hash function from user_name + timestamp, or uuid
// HSet được sử dụng để lưu trữ một cặp khóa-giá trị trong một hash (bảng băm) của Redis | ||
// khóa là sessionID còn "username"->field, username->value | ||
// err := redisClient.HSet(redisClient.Context(), sessionID, "username", username, "password", password).Err() | ||
err := redisClient.Set(redisClient.Context(), sessionID, username, 0).Err() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should set an expire time for this session data
// lưu | ||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "Login successful", | ||
"session_id": sessionID, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
session id is usually stored on header with field Set-Cookie but not body. Can use c.SetCookie function which provided by gin
} | ||
func pingHandler(c *gin.Context) { | ||
// the sessionID will be saved as session_id | ||
sessionID := c.Query("session_id") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, session id should be get from header
// Check if the session ID is valid | ||
// look up in the redis cached | ||
// Result() sẽ trả về giá trị value với key cung cấp và err | ||
userName, err := redisClient.Get(redisClient.Context(), sessionID).Result() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should generate a new context for every request something like context.WithTimeout(context.Background(), time.MilliSecond*50)
} | ||
|
||
// đặt khóa để chỉ 1 người /ping được 1 lúc mà thôi | ||
mutex.Lock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happen if we have more than 2 instances, then there're 2 people can ping at a same time if you use this mutext. What we suppose to do here is a distributed lock implemented by redis
// giảm số redis query | ||
// lru.Cache.Get() sẽ lấy khóa callCountKey có dạng call_count:<userName | ||
// rồi trả về value của key đó nếu không trả về nil | ||
callCount, _ := lruCache.Get(callCountKey) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, we suppose to do here is using redis to store call count, because if you use this lruCache, it will only store in local memory, if we have more than 2 instances, the total count is wrong
No description provided.