-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.go
More file actions
81 lines (70 loc) · 1.84 KB
/
example.go
File metadata and controls
81 lines (70 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"time"
"github.com/dolphindb/api-go/v3/api"
"github.com/dolphindb/api-go/v3/example/apis"
)
func main() {
// 配置连接池选项
opt := &api.PoolOption{
Address: apis.TestAddr,
UserID: apis.User,
Password: apis.Password,
PoolSize: 5,
// 开启后若同时配置 LoadBalanceAddresses,
// 连接会在 Address、LoadBalanceAddresses、HighAvailabilitySites
// 去重后平均分配。
LoadBalance: false,
EnableHighAvailability: false,
Reconnect: true,
TryReconnectNums: new(int),
}
fmt.Printf(
"Connecting to %s with request Timeout=%s and NetTimeout=%s\n",
opt.Address,
describeTimeout(opt.Timeout, time.Minute),
describeTimeout(opt.NetTimeout, 3*time.Second),
)
// 创建连接池
pool, err := api.NewDBConnectionPool(opt)
if err != nil {
fmt.Printf("Failed to create connection pool: %s\n", err.Error())
return
}
defer pool.Close()
// 创建任务
singleTask := &api.Task{Script: "1..10"}
err = pool.ExecuteTask(singleTask)
if err != nil {
fmt.Printf("Failed to execute task: %s\n", err.Error())
return
}
fmt.Printf("Single task result: %v\n", singleTask.GetResult())
// 创建批量任务
tasks := []*api.Task{
{Script: "sleep(100)", Args: nil},
{Script: "a=1+1", Args: nil},
}
// 执行任务
err = pool.Execute(tasks)
if err != nil {
fmt.Printf("Failed to execute tasks: %s\n", err.Error())
return
}
// 输出任务结果
for _, task := range tasks {
fmt.Println("Task succeeded:", task.IsSuccess())
if task.GetError() != nil {
fmt.Printf("Task failed: %s\n", task.GetError().Error())
} else {
fmt.Printf("Task result: %v\n", task.GetResult())
}
}
}
func describeTimeout(value, defaultValue time.Duration) string {
if value == 0 {
return fmt.Sprintf("default(%s)", defaultValue)
}
return value.String()
}