split oauth_tokens.claims column up; improved fonts; allow multiple sse connections per user; make navbar and use friendly
This commit is contained in:
@@ -11,12 +11,16 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
maxNumOpenConnectionsPerUser = 3
|
||||
)
|
||||
|
||||
type (
|
||||
sseRouter struct {
|
||||
log *logging.Logger
|
||||
sse *sse.Queue
|
||||
|
||||
users map[string]context.CancelFunc
|
||||
users map[string][maxNumOpenConnectionsPerUser]context.CancelFunc
|
||||
lock sync.Mutex
|
||||
}
|
||||
)
|
||||
@@ -30,7 +34,7 @@ func Routes(
|
||||
s := &sseRouter{
|
||||
log: logger,
|
||||
sse: sq,
|
||||
users: make(map[string]context.CancelFunc),
|
||||
users: make(map[string][maxNumOpenConnectionsPerUser]context.CancelFunc),
|
||||
}
|
||||
|
||||
r.GET("/", auth.AuthenticateAndAddIdentityGin(), response.Handler(s.serveEvents))
|
||||
@@ -46,10 +50,11 @@ func (r *sseRouter) serveEvents(c *gin.Context) (response.Response, error) {
|
||||
"accountID", acctID,
|
||||
"userID", userID,
|
||||
"email", email,
|
||||
"userAgent", c.Request.UserAgent(),
|
||||
)
|
||||
log.Info("user connected to sse queue")
|
||||
|
||||
ctx := r.closeExistingConnectionsForUserAndStoreCancelFuncForUser(c, userID)
|
||||
ctx := r.closeOutstandingConnectionsForUserAndStoreCancelFuncForUser(c, userID)
|
||||
|
||||
w := c.Writer
|
||||
|
||||
@@ -80,18 +85,34 @@ func (r *sseRouter) serveEvents(c *gin.Context) (response.Response, error) {
|
||||
return response.Status(200), nil
|
||||
}
|
||||
|
||||
func (r *sseRouter) closeExistingConnectionsForUserAndStoreCancelFuncForUser(ctx context.Context, userID string) context.Context {
|
||||
func (r *sseRouter) closeOutstandingConnectionsForUserAndStoreCancelFuncForUser(ctx context.Context, userID string) context.Context {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
|
||||
closeConnFuncs := r.users[userID]
|
||||
defer func() {
|
||||
r.users[userID] = closeConnFuncs
|
||||
}()
|
||||
|
||||
// close existing connection
|
||||
if closeConn, ok := r.users[userID]; ok {
|
||||
closeConn()
|
||||
delete(r.users, userID)
|
||||
|
||||
for i := range maxNumOpenConnectionsPerUser {
|
||||
if fn := closeConnFuncs[i]; fn == nil {
|
||||
// not hit limit on connections.
|
||||
// save the cancellation func and done
|
||||
closeConnFuncs[i] = cancel
|
||||
return ctx
|
||||
}
|
||||
}
|
||||
|
||||
// store reference to cancel func
|
||||
ctx, r.users[userID] = context.WithCancel(ctx)
|
||||
// close the oldest connection, shift all cancellation funcs down, and push the new one in
|
||||
closeConnFuncs[0]()
|
||||
for i := range maxNumOpenConnectionsPerUser - 1 {
|
||||
closeConnFuncs[i] = closeConnFuncs[i+1]
|
||||
}
|
||||
closeConnFuncs[maxNumOpenConnectionsPerUser-1] = cancel
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user