Skip to content

Commit 9e0e614

Browse files
committed
docs added
1 parent b0a312b commit 9e0e614

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

pkg/pool/dbpool.go

+107
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ var _ DBPool = &InstancePoolImpl{}
3939
//
4040
// Returns:
4141
// - shard.Shard: The shard that satisfies the callback condition, or nil if no shard satisfies the condition.
42+
//
43+
// traverseHostsMatchCB traverses the list of hosts and invokes the provided callback function
44+
// for each host until the callback returns true. It returns the shard that satisfies the callback
45+
// condition. If no shard satisfies the condition, it returns nil.
46+
//
47+
// Parameters:
48+
// - clid: The client ID.
49+
// - key: The shard key.
50+
// - hosts: The list of hosts to traverse.
51+
// - cb: The callback function that takes a shard and returns a boolean value.
52+
//
53+
// Returns:
54+
// - shard.Shard: The shard that satisfies the callback condition, or nil if no shard satisfies the condition.
4255
func (s *InstancePoolImpl) traverseHostsMatchCB(
4356
clid uint,
4457
key kr.ShardKey, hosts []string, cb func(shard.Shard) bool) shard.Shard {
@@ -79,6 +92,20 @@ func (s *InstancePoolImpl) traverseHostsMatchCB(
7992
// Returns:
8093
// - shard.Shard: The selected read-only shard host.
8194
// - error: An error if no suitable shard host is found.
95+
//
96+
// SelectReadOnlyShardHost selects a read-only shard host from the given list of hosts based on the provided client ID and shard key.
97+
// It traverses the hosts and performs checks to ensure the selected shard host is suitable for read-only operations.
98+
// If a suitable shard host is found, it is returned along with a nil error.
99+
// If no suitable shard host is found, an error is returned with a message indicating the reason for failure.
100+
//
101+
// Parameters:
102+
// - clid: The client ID.
103+
// - key: The shard key.
104+
// - hosts: The list of hosts to traverse.
105+
//
106+
// Returns:
107+
// - shard.Shard: The selected read-only shard host.
108+
// - error: An error if no suitable shard host is found.
82109
func (s *InstancePoolImpl) SelectReadOnlyShardHost(
83110
clid uint,
84111
key kr.ShardKey, hosts []string) (shard.Shard, error) {
@@ -116,6 +143,20 @@ func (s *InstancePoolImpl) SelectReadOnlyShardHost(
116143
// Returns:
117144
// - shard.Shard: The selected read-write shard host.
118145
// - error: An error if no suitable shard host is found.
146+
//
147+
// SelectReadWriteShardHost selects a read-write shard host from the given list of hosts based on the provided client ID and shard key.
148+
// It traverses the hosts and checks if each shard is available and suitable for read-write operations.
149+
// If a suitable shard is found, it is returned along with no error.
150+
// If no suitable shard is found, an error is returned indicating the failure reason.
151+
//
152+
// Parameters:
153+
// - clid: The client ID.
154+
// - key: The shard key.
155+
// - hosts: The list of hosts to traverse.
156+
//
157+
// Returns:
158+
// - shard.Shard: The selected read-write shard host.
159+
// - error: An error if no suitable shard host is found.
119160
func (s *InstancePoolImpl) SelectReadWriteShardHost(
120161
clid uint,
121162
key kr.ShardKey, hosts []string) (shard.Shard, error) {
@@ -154,6 +195,19 @@ func (s *InstancePoolImpl) SelectReadWriteShardHost(
154195
// Returns:
155196
// - shard.Shard: The acquired shard connection.
156197
// - error: An error if the connection cannot be established.
198+
//
199+
// Connection acquires a new instance connection for a client to a shard with target session attributes.
200+
// It selects a shard host based on the target session attributes and returns a shard connection.
201+
// If no connection can be established, it returns an error.
202+
//
203+
// Parameters:
204+
// - clid: The client ID.
205+
// - key: The shard key.
206+
// - targetSessionAttrs: The target session attributes.
207+
//
208+
// Returns:
209+
// - shard.Shard: The acquired shard connection.
210+
// - error: An error if the connection cannot be established.
157211
func (s *InstancePoolImpl) Connection(
158212
clid uint,
159213
key kr.ShardKey,
@@ -204,6 +258,15 @@ func (s *InstancePoolImpl) Connection(
204258
}
205259
}
206260

261+
// InitRule initializes the backend rule in the instance pool.
262+
// It takes a pointer to a BackendRule as a parameter and returns an error.
263+
//
264+
// Parameters:
265+
// - rule: A pointer to a BackendRule representing the backend rule to be initialized.
266+
//
267+
// Returns:
268+
// - error: An error if there is an error initializing the backend rule, nil otherwise.
269+
//
207270
// InitRule initializes the backend rule in the instance pool.
208271
// It takes a pointer to a BackendRule as a parameter and returns an error.
209272
//
@@ -216,17 +279,27 @@ func (s *InstancePoolImpl) InitRule(rule *config.BackendRule) error {
216279
return s.pool.InitRule(rule)
217280
}
218281

282+
// ShardMapping returns the shard mapping of the instance pool.
219283
// ShardMapping returns the shard mapping of the instance pool.
220284
func (s *InstancePoolImpl) ShardMapping() map[string]*config.Shard {
221285
return s.shardMapping
222286
}
223287

288+
// List returns a list of shards in the instance pool.
224289
// List returns a list of shards in the instance pool.
225290
func (s *InstancePoolImpl) List() []shard.Shard {
226291
/* mutex? */
227292
return s.pool.List()
228293
}
229294

295+
// ForEach iterates over each shard in the instance pool and calls the provided callback function.
296+
// It returns an error if the callback function returns an error.
297+
//
298+
// Parameters:
299+
// - cb: The callback function to be called for each shard in the instance pool.
300+
//
301+
// Returns:
302+
// - error: An error if the callback function returns an error.
230303
// ForEach iterates over each shard in the instance pool and calls the provided callback function.
231304
// It returns an error if the callback function returns an error.
232305
//
@@ -249,6 +322,15 @@ func (s *InstancePoolImpl) ForEach(cb func(sh shard.Shardinfo) error) error {
249322
//
250323
// Returns:
251324
// - error: An error if the shard is discarded or if there is an error putting the shard into the pool.
325+
// Put puts a shard into the instance pool.
326+
// It discards the shard if it is not synchronized or if it is not in idle transaction status.
327+
// Otherwise, it puts the shard into the pool.
328+
//
329+
// Parameters:
330+
// - sh: The shard to be put into the pool.
331+
//
332+
// Returns:
333+
// - error: An error if the shard is discarded or if there is an error putting the shard into the pool.
252334
func (s *InstancePoolImpl) Put(sh shard.Shard) error {
253335
if sh.Sync() != 0 {
254336
spqrlog.Zero.Error().
@@ -267,6 +349,14 @@ func (s *InstancePoolImpl) Put(sh shard.Shard) error {
267349
return s.pool.Put(sh)
268350
}
269351

352+
// ForEachPool iterates over each pool in the instance pool and calls the provided callback function.
353+
// It returns an error if the callback function returns an error.
354+
//
355+
// Parameters:
356+
// - cb: The callback function to be called for each pool in the instance pool.
357+
//
358+
// Returns:
359+
// - error: An error if the callback function returns an error.
270360
// ForEachPool iterates over each pool in the instance pool and calls the provided callback function.
271361
// It returns an error if the callback function returns an error.
272362
//
@@ -279,6 +369,14 @@ func (s *InstancePoolImpl) ForEachPool(cb func(pool Pool) error) error {
279369
return s.pool.ForEachPool(cb)
280370
}
281371

372+
// Cut removes a shard from the instance pool based on the provided host.
373+
// It returns the removed shard.
374+
//
375+
// Parameters:
376+
// - host: The host of the shard to be removed.
377+
//
378+
// Returns:
379+
// - []shard.Shard: The removed shard.
282380
// Cut removes a shard from the instance pool based on the provided host.
283381
// It returns the removed shard.
284382
//
@@ -291,6 +389,14 @@ func (s *InstancePoolImpl) Cut(host string) []shard.Shard {
291389
return s.pool.Cut(host)
292390
}
293391

392+
// Discard removes a shard from the instance pool.
393+
// It returns an error if the removal fails.
394+
//
395+
// Parameters:
396+
// - sh: The shard to be removed from the pool.
397+
//
398+
// Returns:
399+
// - error: An error if the removal fails, nil otherwise.
294400
// Discard removes a shard from the instance pool.
295401
// It returns an error if the removal fails.
296402
//
@@ -311,6 +417,7 @@ func (s *InstancePoolImpl) Discard(sh shard.Shard) error {
311417
// Parameters:
312418
// - mapping: A map containing the shard mapping, where the key is the shard name
313419
// and the value is a pointer to the corresponding Shard configuration.
420+
// - sp: A StartupParams
314421
//
315422
// Returns:
316423
// - DBPool: A DBPool interface that represents the created pool.

0 commit comments

Comments
 (0)