You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ngs/service/connection.go

42 lines
918 B
Go

package service
import "sync/atomic"
// Connections is a global variable which is used by session. thread-safe
var Connections = newConnectionService()
type connectionService struct {
count int64
sid int64
}
func newConnectionService() *connectionService {
return &connectionService{sid: 0}
}
// Increment the connection count
func (c *connectionService) Increment() {
atomic.AddInt64(&c.count, 1)
}
// Decrement the connection count
func (c *connectionService) Decrement() {
atomic.AddInt64(&c.count, -1)
}
// Count returns the connection numbers in current
func (c *connectionService) Count() int64 {
return atomic.LoadInt64(&c.count)
}
// Reset the connection service status
func (c *connectionService) Reset() {
atomic.StoreInt64(&c.count, 0)
atomic.StoreInt64(&c.sid, 0)
}
// SessionID returns the session id
func (c *connectionService) SessionID() int64 {
return atomic.AddInt64(&c.sid, 1)
}