update code solution#16
Conversation
| } | ||
|
|
||
| func generateSessionID() string { | ||
| timestamp := time.Now().UnixNano() |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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
| } | ||
|
|
||
| func pingHandler(c *gin.Context) { | ||
| sessionID := c.Query("session_id") |
There was a problem hiding this comment.
people usually store session id on cookie
| } | ||
|
|
||
| // Acquire lock to ensure only one person can call /ping at a time | ||
| mutex.Lock() |
There was a problem hiding this comment.
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
|
|
||
| // Check if the user has exceeded the rate limit | ||
| callCountKey := fmt.Sprintf("call_count:%s", userName) | ||
| callCount, _ := lruCache.Get(callCountKey) |
There was a problem hiding this comment.
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, )
No description provided.