|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package connection |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "time" |
| 23 | + |
| 24 | + "dubbo.apache.org/dubbo-go/v3/common" |
| 25 | +) |
| 26 | + |
| 27 | +// ConnectionState represents the state of a connection |
| 28 | +type ConnectionState int32 |
| 29 | + |
| 30 | +const ( |
| 31 | + // StateIdle indicates the connection is idle |
| 32 | + StateIdle ConnectionState = iota |
| 33 | + // StateConnecting indicates the connection is being established |
| 34 | + StateConnecting |
| 35 | + // StateReady indicates the connection is ready for use |
| 36 | + StateReady |
| 37 | + // StateTransientFailure indicates the connection has failed temporarily |
| 38 | + StateTransientFailure |
| 39 | + // StateShutdown indicates the connection has been shutdown |
| 40 | + StateShutdown |
| 41 | +) |
| 42 | + |
| 43 | +// String returns the string representation of ConnectionState |
| 44 | +func (cs ConnectionState) String() string { |
| 45 | + switch cs { |
| 46 | + case StateIdle: |
| 47 | + return "IDLE" |
| 48 | + case StateConnecting: |
| 49 | + return "CONNECTING" |
| 50 | + case StateReady: |
| 51 | + return "READY" |
| 52 | + case StateTransientFailure: |
| 53 | + return "TRANSIENT_FAILURE" |
| 54 | + case StateShutdown: |
| 55 | + return "SHUTDOWN" |
| 56 | + default: |
| 57 | + return "UNKNOWN" |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// Connection represents a unified connection interface across all protocols |
| 62 | +type Connection interface { |
| 63 | + // GetState returns the current state of the connection |
| 64 | + GetState() ConnectionState |
| 65 | + |
| 66 | + // GetURL returns the URL associated with this connection |
| 67 | + GetURL() *common.URL |
| 68 | + |
| 69 | + // IsAvailable checks if the connection is available for use |
| 70 | + IsAvailable() bool |
| 71 | + |
| 72 | + // Close closes the connection |
| 73 | + Close() error |
| 74 | + |
| 75 | + // GetLastActive returns the last active time of the connection |
| 76 | + GetLastActive() time.Time |
| 77 | + |
| 78 | + // GetProtocol returns the protocol name (dubbo, triple, grpc, etc.) |
| 79 | + GetProtocol() string |
| 80 | +} |
| 81 | + |
| 82 | +// ConnectionStats provides statistics about connections |
| 83 | +type ConnectionStats struct { |
| 84 | + TotalConnections int |
| 85 | + ActiveConnections int |
| 86 | + IdleConnections int |
| 87 | + FailedConnections int |
| 88 | + ReconnectAttempts int |
| 89 | + LastReconnectTime time.Time |
| 90 | +} |
| 91 | + |
| 92 | +// HealthCheckResult represents the result of a health check |
| 93 | +type HealthCheckResult struct { |
| 94 | + Healthy bool |
| 95 | + Reason string |
| 96 | + CheckTime time.Time |
| 97 | + Duration time.Duration |
| 98 | +} |
| 99 | + |
| 100 | +// HealthChecker defines the interface for connection health checking |
| 101 | +type HealthChecker interface { |
| 102 | + // CheckConnection performs health check on a specific connection |
| 103 | + CheckConnection(ctx context.Context, conn Connection) *HealthCheckResult |
| 104 | + |
| 105 | + // GetProtocol returns the protocol this health checker supports |
| 106 | + GetProtocol() string |
| 107 | + |
| 108 | + // GetCheckInterval returns the recommended check interval |
| 109 | + GetCheckInterval() time.Duration |
| 110 | +} |
| 111 | + |
| 112 | +// ConnectionPool manages connections for a specific protocol |
| 113 | +type ConnectionPool interface { |
| 114 | + // GetConnection retrieves a healthy connection for the given URL |
| 115 | + GetConnection(url *common.URL) (Connection, error) |
| 116 | + |
| 117 | + // RemoveConnection removes a connection from the pool |
| 118 | + RemoveConnection(conn Connection) error |
| 119 | + |
| 120 | + // RemoveStaleConnections removes all stale connections for the given URL |
| 121 | + RemoveStaleConnections(url *common.URL) int |
| 122 | + |
| 123 | + // GetStats returns connection pool statistics |
| 124 | + GetStats() *ConnectionStats |
| 125 | + |
| 126 | + // Close closes all connections in the pool |
| 127 | + Close() error |
| 128 | +} |
| 129 | + |
| 130 | +// ConnectionManager provides unified connection management across all protocols |
| 131 | +type ConnectionManager interface { |
| 132 | + // RegisterProtocol registers a connection pool and health checker for a protocol |
| 133 | + RegisterProtocol(protocol string, pool ConnectionPool, checker HealthChecker) error |
| 134 | + |
| 135 | + // GetConnection gets a healthy connection for the specified URL |
| 136 | + GetConnection(url *common.URL) (Connection, error) |
| 137 | + |
| 138 | + // IsConnectionHealthy checks if the connection is healthy |
| 139 | + IsConnectionHealthy(conn Connection) bool |
| 140 | + |
| 141 | + // RemoveStaleConnections removes stale connections for the given URL |
| 142 | + RemoveStaleConnections(url *common.URL) int |
| 143 | + |
| 144 | + // GetGlobalStats returns global connection statistics across all protocols |
| 145 | + GetGlobalStats() map[string]*ConnectionStats |
| 146 | + |
| 147 | + // StartHealthCheckLoop starts the background health check loop |
| 148 | + StartHealthCheckLoop(ctx context.Context) |
| 149 | + |
| 150 | + // Close closes all connections across all protocols |
| 151 | + Close() error |
| 152 | +} |
| 153 | + |
| 154 | +// StateChangeCallback is called when connection state changes |
| 155 | +type StateChangeCallback func(conn Connection, oldState, newState ConnectionState) |
| 156 | + |
| 157 | +// ConnectionEventListener listens to connection events |
| 158 | +type ConnectionEventListener interface { |
| 159 | + // OnStateChange is called when connection state changes |
| 160 | + OnStateChange(conn Connection, oldState, newState ConnectionState) |
| 161 | + |
| 162 | + // OnHealthCheckFailed is called when health check fails |
| 163 | + OnHealthCheckFailed(conn Connection, result *HealthCheckResult) |
| 164 | + |
| 165 | + // OnConnectionRemoved is called when a connection is removed |
| 166 | + OnConnectionRemoved(conn Connection, reason string) |
| 167 | +} |
0 commit comments