mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00

* Return Auth service Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Compose to run with SpiceDB and Auth svc Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update auth gRPC API Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove Users' policies Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Move Groups to internal Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use shared groups in Users Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove unused code Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use pkg Groups in Things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove Things groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Make imports consistent Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Groups networking Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove things groups-specific API Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Move Things Clients to the root Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Move Clients to Users root Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Temporarily remove tracing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix imports Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add buffer config for gRPC Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update auth type for Things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use Auth for login Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add temporary solution for refresh token Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Tokenizer interface Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Updade tokens issuing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix token issuing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update JWT validator and refactor Tokenizer Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Rename access timeout Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Rename login to authenticate Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Identify to use SubjectID Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add Auth to Groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use the Auth service for Groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update auth schema Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix Auth for Groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add auth for addons (#14) Signed-off-by: Arvindh <arvindh91@gmail.com> Speparate Login and Refresh tokens Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Merge authN and authZ requests for things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add connect and disconnect Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update sharing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix policies addition and removal Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update relation with roels Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add gRPC to Things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Assign and Unassign members to group and Listing of Group members (#15) * add auth for addons Signed-off-by: Arvindh <arvindh91@gmail.com> * add assign and unassign to group Signed-off-by: Arvindh <arvindh91@gmail.com> * add group incomplete repo implementation Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Move coap mqtt and ws policies to spicedb (#16) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Remove old policies Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> NOISSUE - Things authorize to return thingID (#18) This commit modifies the authorize endpoint to the grpc endpoint to return thingID. The authorize endpoint allows adapters to get the publisher of the message. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Add Groups to users service (#17) * add assign and unassign to group Signed-off-by: Arvindh <arvindh91@gmail.com> * add group incomplete repo implementation Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users stable 1 Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users stable 2 Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users & things Signed-off-by: Arvindh <arvindh91@gmail.com> * Amend signature Signed-off-by: Arvindh <arvindh91@gmail.com> * fix merge error Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Fix es code (#21) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Fix Bugs (#20) * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Test e2e (#19) * fix: connect method Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * fix: e2e Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * fix changes in sdk and e2e Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(docker): remove unnecessary port mapping Remove the port mapping for MQTT broker in the docker-compose.yml file. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Enable group listing Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(responses): update ChannelsPage struct The ChannelsPage struct in the responses.go file has been updated. The "Channels" field has been renamed to "Groups" to provide more accurate naming. This change ensures consistency and clarity in the codebase. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(things): add UpdateClientSecret method Add the UpdateClientSecret method to the things service. This method allows updating the client secret for a specific client identified by the provided token, id, and key parameters. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Use smaller buffers for gRPC Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Clean up tests (#22) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add Connect Disconnect endpoints (#23) * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> * fix list of things in a channel and Add connect disconnect endpoint Signed-off-by: Arvindh <arvindh91@gmail.com> * fix list of things in a channel and Add connect disconnect endpoint Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add: Things share with users (#25) * fix list of things in a channel and Add connect disconnect endpoint Signed-off-by: Arvindh <arvindh91@gmail.com> * add: things share with other users Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Rename gRPC Services (#24) * Rename things and users auth service Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * docs: add authorization docs for gRPC services Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Rename things and users grpc services Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Remove mainflux.env package Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add: Listing of things, channels, groups, users (#26) * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Clean Up Users (#27) * feat(groups): rename redis package to events - Renamed the `redis` package to `events` in the `internal/groups` directory. - Updated the file paths and names accordingly. - This change reflects the more accurate purpose of the package and improves code organization. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(auth): Modify identity method Change request and response of identity method Add accessToken and refreshToken to Token response Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * clean up users, remove dead code Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(users): add unit tests for user service This commit adds unit tests for the user service in the `users` package. The tests cover various scenarios and ensure the correct behavior of the service. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add: List of user groups & removed repeating code in groups (#29) * removed repeating code in list groups Signed-off-by: Arvindh <arvindh91@gmail.com> * add: list of user group Signed-off-by: Arvindh <arvindh91@gmail.com> * fix: otel handler operator name for endpoints Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Clean Up Things Service (#28) * Rework things service Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * add tests Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Clean Up Auth Service (#30) * clean up auth service Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(auth): remove unused import Remove the unused import of `emptypb` in `auth.pb.go`. This import is not being used in the codebase and can be safely removed. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Update API docs (#31) Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Remove TODO comments and cleanup the code Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Update dependenices Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Co-authored-by: b1ackd0t <28790446+rodneyosodo@users.noreply.github.com> Co-authored-by: Arvindh <30824765+arvindh123@users.noreply.github.com>
302 lines
9.9 KiB
Go
302 lines
9.9 KiB
Go
/*
|
|
*
|
|
* Copyright 2023 gRPC authors.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*
|
|
*/
|
|
|
|
// Package idle contains a component for managing idleness (entering and exiting)
|
|
// based on RPC activity.
|
|
package idle
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"google.golang.org/grpc/grpclog"
|
|
)
|
|
|
|
// For overriding in unit tests.
|
|
var timeAfterFunc = func(d time.Duration, f func()) *time.Timer {
|
|
return time.AfterFunc(d, f)
|
|
}
|
|
|
|
// Enforcer is the functionality provided by grpc.ClientConn to enter
|
|
// and exit from idle mode.
|
|
type Enforcer interface {
|
|
ExitIdleMode() error
|
|
EnterIdleMode() error
|
|
}
|
|
|
|
// Manager defines the functionality required to track RPC activity on a
|
|
// channel.
|
|
type Manager interface {
|
|
OnCallBegin() error
|
|
OnCallEnd()
|
|
Close()
|
|
}
|
|
|
|
type noopManager struct{}
|
|
|
|
func (noopManager) OnCallBegin() error { return nil }
|
|
func (noopManager) OnCallEnd() {}
|
|
func (noopManager) Close() {}
|
|
|
|
// manager implements the Manager interface. It uses atomic operations to
|
|
// synchronize access to shared state and a mutex to guarantee mutual exclusion
|
|
// in a critical section.
|
|
type manager struct {
|
|
// State accessed atomically.
|
|
lastCallEndTime int64 // Unix timestamp in nanos; time when the most recent RPC completed.
|
|
activeCallsCount int32 // Count of active RPCs; -math.MaxInt32 means channel is idle or is trying to get there.
|
|
activeSinceLastTimerCheck int32 // Boolean; True if there was an RPC since the last timer callback.
|
|
closed int32 // Boolean; True when the manager is closed.
|
|
|
|
// Can be accessed without atomics or mutex since these are set at creation
|
|
// time and read-only after that.
|
|
enforcer Enforcer // Functionality provided by grpc.ClientConn.
|
|
timeout int64 // Idle timeout duration nanos stored as an int64.
|
|
logger grpclog.LoggerV2
|
|
|
|
// idleMu is used to guarantee mutual exclusion in two scenarios:
|
|
// - Opposing intentions:
|
|
// - a: Idle timeout has fired and handleIdleTimeout() is trying to put
|
|
// the channel in idle mode because the channel has been inactive.
|
|
// - b: At the same time an RPC is made on the channel, and OnCallBegin()
|
|
// is trying to prevent the channel from going idle.
|
|
// - Competing intentions:
|
|
// - The channel is in idle mode and there are multiple RPCs starting at
|
|
// the same time, all trying to move the channel out of idle. Only one
|
|
// of them should succeed in doing so, while the other RPCs should
|
|
// piggyback on the first one and be successfully handled.
|
|
idleMu sync.RWMutex
|
|
actuallyIdle bool
|
|
timer *time.Timer
|
|
}
|
|
|
|
// ManagerOptions is a collection of options used by
|
|
// NewManager.
|
|
type ManagerOptions struct {
|
|
Enforcer Enforcer
|
|
Timeout time.Duration
|
|
Logger grpclog.LoggerV2
|
|
}
|
|
|
|
// NewManager creates a new idleness manager implementation for the
|
|
// given idle timeout.
|
|
func NewManager(opts ManagerOptions) Manager {
|
|
if opts.Timeout == 0 {
|
|
return noopManager{}
|
|
}
|
|
|
|
m := &manager{
|
|
enforcer: opts.Enforcer,
|
|
timeout: int64(opts.Timeout),
|
|
logger: opts.Logger,
|
|
}
|
|
m.timer = timeAfterFunc(opts.Timeout, m.handleIdleTimeout)
|
|
return m
|
|
}
|
|
|
|
// resetIdleTimer resets the idle timer to the given duration. This method
|
|
// should only be called from the timer callback.
|
|
func (m *manager) resetIdleTimer(d time.Duration) {
|
|
m.idleMu.Lock()
|
|
defer m.idleMu.Unlock()
|
|
|
|
if m.timer == nil {
|
|
// Only close sets timer to nil. We are done.
|
|
return
|
|
}
|
|
|
|
// It is safe to ignore the return value from Reset() because this method is
|
|
// only ever called from the timer callback, which means the timer has
|
|
// already fired.
|
|
m.timer.Reset(d)
|
|
}
|
|
|
|
// handleIdleTimeout is the timer callback that is invoked upon expiry of the
|
|
// configured idle timeout. The channel is considered inactive if there are no
|
|
// ongoing calls and no RPC activity since the last time the timer fired.
|
|
func (m *manager) handleIdleTimeout() {
|
|
if m.isClosed() {
|
|
return
|
|
}
|
|
|
|
if atomic.LoadInt32(&m.activeCallsCount) > 0 {
|
|
m.resetIdleTimer(time.Duration(m.timeout))
|
|
return
|
|
}
|
|
|
|
// There has been activity on the channel since we last got here. Reset the
|
|
// timer and return.
|
|
if atomic.LoadInt32(&m.activeSinceLastTimerCheck) == 1 {
|
|
// Set the timer to fire after a duration of idle timeout, calculated
|
|
// from the time the most recent RPC completed.
|
|
atomic.StoreInt32(&m.activeSinceLastTimerCheck, 0)
|
|
m.resetIdleTimer(time.Duration(atomic.LoadInt64(&m.lastCallEndTime) + m.timeout - time.Now().UnixNano()))
|
|
return
|
|
}
|
|
|
|
// This CAS operation is extremely likely to succeed given that there has
|
|
// been no activity since the last time we were here. Setting the
|
|
// activeCallsCount to -math.MaxInt32 indicates to OnCallBegin() that the
|
|
// channel is either in idle mode or is trying to get there.
|
|
if !atomic.CompareAndSwapInt32(&m.activeCallsCount, 0, -math.MaxInt32) {
|
|
// This CAS operation can fail if an RPC started after we checked for
|
|
// activity at the top of this method, or one was ongoing from before
|
|
// the last time we were here. In both case, reset the timer and return.
|
|
m.resetIdleTimer(time.Duration(m.timeout))
|
|
return
|
|
}
|
|
|
|
// Now that we've set the active calls count to -math.MaxInt32, it's time to
|
|
// actually move to idle mode.
|
|
if m.tryEnterIdleMode() {
|
|
// Successfully entered idle mode. No timer needed until we exit idle.
|
|
return
|
|
}
|
|
|
|
// Failed to enter idle mode due to a concurrent RPC that kept the channel
|
|
// active, or because of an error from the channel. Undo the attempt to
|
|
// enter idle, and reset the timer to try again later.
|
|
atomic.AddInt32(&m.activeCallsCount, math.MaxInt32)
|
|
m.resetIdleTimer(time.Duration(m.timeout))
|
|
}
|
|
|
|
// tryEnterIdleMode instructs the channel to enter idle mode. But before
|
|
// that, it performs a last minute check to ensure that no new RPC has come in,
|
|
// making the channel active.
|
|
//
|
|
// Return value indicates whether or not the channel moved to idle mode.
|
|
//
|
|
// Holds idleMu which ensures mutual exclusion with exitIdleMode.
|
|
func (m *manager) tryEnterIdleMode() bool {
|
|
m.idleMu.Lock()
|
|
defer m.idleMu.Unlock()
|
|
|
|
if atomic.LoadInt32(&m.activeCallsCount) != -math.MaxInt32 {
|
|
// We raced and lost to a new RPC. Very rare, but stop entering idle.
|
|
return false
|
|
}
|
|
if atomic.LoadInt32(&m.activeSinceLastTimerCheck) == 1 {
|
|
// An very short RPC could have come in (and also finished) after we
|
|
// checked for calls count and activity in handleIdleTimeout(), but
|
|
// before the CAS operation. So, we need to check for activity again.
|
|
return false
|
|
}
|
|
|
|
// No new RPCs have come in since we last set the active calls count value
|
|
// -math.MaxInt32 in the timer callback. And since we have the lock, it is
|
|
// safe to enter idle mode now.
|
|
if err := m.enforcer.EnterIdleMode(); err != nil {
|
|
m.logger.Errorf("Failed to enter idle mode: %v", err)
|
|
return false
|
|
}
|
|
|
|
// Successfully entered idle mode.
|
|
m.actuallyIdle = true
|
|
return true
|
|
}
|
|
|
|
// OnCallBegin is invoked at the start of every RPC.
|
|
func (m *manager) OnCallBegin() error {
|
|
if m.isClosed() {
|
|
return nil
|
|
}
|
|
|
|
if atomic.AddInt32(&m.activeCallsCount, 1) > 0 {
|
|
// Channel is not idle now. Set the activity bit and allow the call.
|
|
atomic.StoreInt32(&m.activeSinceLastTimerCheck, 1)
|
|
return nil
|
|
}
|
|
|
|
// Channel is either in idle mode or is in the process of moving to idle
|
|
// mode. Attempt to exit idle mode to allow this RPC.
|
|
if err := m.exitIdleMode(); err != nil {
|
|
// Undo the increment to calls count, and return an error causing the
|
|
// RPC to fail.
|
|
atomic.AddInt32(&m.activeCallsCount, -1)
|
|
return err
|
|
}
|
|
|
|
atomic.StoreInt32(&m.activeSinceLastTimerCheck, 1)
|
|
return nil
|
|
}
|
|
|
|
// exitIdleMode instructs the channel to exit idle mode.
|
|
//
|
|
// Holds idleMu which ensures mutual exclusion with tryEnterIdleMode.
|
|
func (m *manager) exitIdleMode() error {
|
|
m.idleMu.Lock()
|
|
defer m.idleMu.Unlock()
|
|
|
|
if !m.actuallyIdle {
|
|
// This can happen in two scenarios:
|
|
// - handleIdleTimeout() set the calls count to -math.MaxInt32 and called
|
|
// tryEnterIdleMode(). But before the latter could grab the lock, an RPC
|
|
// came in and OnCallBegin() noticed that the calls count is negative.
|
|
// - Channel is in idle mode, and multiple new RPCs come in at the same
|
|
// time, all of them notice a negative calls count in OnCallBegin and get
|
|
// here. The first one to get the lock would got the channel to exit idle.
|
|
//
|
|
// Either way, nothing to do here.
|
|
return nil
|
|
}
|
|
|
|
if err := m.enforcer.ExitIdleMode(); err != nil {
|
|
return fmt.Errorf("channel failed to exit idle mode: %v", err)
|
|
}
|
|
|
|
// Undo the idle entry process. This also respects any new RPC attempts.
|
|
atomic.AddInt32(&m.activeCallsCount, math.MaxInt32)
|
|
m.actuallyIdle = false
|
|
|
|
// Start a new timer to fire after the configured idle timeout.
|
|
m.timer = timeAfterFunc(time.Duration(m.timeout), m.handleIdleTimeout)
|
|
return nil
|
|
}
|
|
|
|
// OnCallEnd is invoked at the end of every RPC.
|
|
func (m *manager) OnCallEnd() {
|
|
if m.isClosed() {
|
|
return
|
|
}
|
|
|
|
// Record the time at which the most recent call finished.
|
|
atomic.StoreInt64(&m.lastCallEndTime, time.Now().UnixNano())
|
|
|
|
// Decrement the active calls count. This count can temporarily go negative
|
|
// when the timer callback is in the process of moving the channel to idle
|
|
// mode, but one or more RPCs come in and complete before the timer callback
|
|
// can get done with the process of moving to idle mode.
|
|
atomic.AddInt32(&m.activeCallsCount, -1)
|
|
}
|
|
|
|
func (m *manager) isClosed() bool {
|
|
return atomic.LoadInt32(&m.closed) == 1
|
|
}
|
|
|
|
func (m *manager) Close() {
|
|
atomic.StoreInt32(&m.closed, 1)
|
|
|
|
m.idleMu.Lock()
|
|
m.timer.Stop()
|
|
m.timer = nil
|
|
m.idleMu.Unlock()
|
|
}
|