You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@iotdb.apache.org by "citrusreticulata (via GitHub)" <gi...@apache.org> on 2023/02/17 09:07:02 UTC
[GitHub] [iotdb-client-go] citrusreticulata commented on a diff in pull request #75: [To rel/0.13] Add SessionPool and example
citrusreticulata commented on code in PR #75:
URL: https://github.com/apache/iotdb-client-go/pull/75#discussion_r1109436680
##########
client/sessionpool.go:
##########
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 client
+
+import (
+ "errors"
+ "log"
+ "runtime"
+ "time"
+)
+
+var errTimeout = errors.New("get session timeout")
+var errPoolClosed = errors.New("sessionPool has closed")
+var defaultMultiple = 5
+
+type SessionPool struct {
+ config *PoolConfig
+ maxSize int
+ waitToGetSessionTimeoutInMs int
+ enableCompression bool
+ connectionTimeoutInMs int
+ ch chan Session
+ sem chan int8
+}
+
+type PoolConfig struct {
+ Host string
+ Port string
+ NodeUrls []string
+ UserName string
+ Password string
+ FetchSize int32
+ TimeZone string
+ ConnectRetryMax int
+}
+
+func NewSessionPool(conf *PoolConfig, maxSize, connectionTimeoutInMs, waitToGetSessionTimeoutInMs int,
+ enableCompression bool) SessionPool {
+ if maxSize <= 0 {
+ maxSize = runtime.NumCPU() * defaultMultiple
+ }
+ var ch = make(chan Session, maxSize)
+ var sem = make(chan int8, maxSize)
+ return SessionPool{
+ config: conf,
+ maxSize: maxSize,
+ waitToGetSessionTimeoutInMs: waitToGetSessionTimeoutInMs,
+ connectionTimeoutInMs: connectionTimeoutInMs,
+ enableCompression: enableCompression,
+ ch: ch,
+ sem: sem,
+ }
+}
+
+func (spool *SessionPool) GetSession() (session Session, err error) {
+ for {
+ select {
+ case spool.sem <- 1:
+ select {
+ case session, ok := <-spool.ch:
+ if ok {
+ return session, nil
+ } else {
+ log.Println("sessonpool has closed")
Review Comment:
```suggestion
log.Println("sessionpool has closed")
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org