Skip to content

Commit

Permalink
xorm client list with goroutine added
Browse files Browse the repository at this point in the history
  • Loading branch information
HiranmoyChowdhury committed Apr 29, 2024
1 parent 9f8990c commit b13c296
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
5 changes: 3 additions & 2 deletions pgbouncer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type XormClient struct {
}

type XormClientList struct {
list []XormClient
mu sync.Mutex
list []*XormClient
mutex sync.Mutex
message string
}
54 changes: 33 additions & 21 deletions pgbouncer/kubedb_client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func (o *KubeDBClientBuilder) GetPgBouncerXormClient() (*XormClient, error) {
}
_, err = engine.Query("SELECT 1")
if err != nil {
err = engine.Close()
if err != nil {
return nil, err
err2 := engine.Close()
if err2 != nil {
return nil, err2
}
return nil, err
}
Expand Down Expand Up @@ -174,35 +174,47 @@ func (o *KubeDBClientBuilder) getConnectionString() (string, error) {
}

func GetXormClientList(kc client.Client, pb *api.PgBouncer, ctx context.Context) (*XormClientList, error) {
Clientlist := &XormClientList{
list: []XormClient{},
clientlist := &XormClientList{
list: []*XormClient{},
}

podList := &corev1.PodList{}
err := kc.List(context.Background(), podList, client.MatchingLabels(pb.PodLabels()))

if err != nil {
return nil, fmt.Errorf("failed get pod list for XormClientList")
}
ch := make(chan string)
for _, postgresRef := range pb.Spec.Databases {
for _, pods := range podList.Items {

for _, pod := range podList.Items {
go clientlist.addXormClient(kc, pb, ctx, pod.Name, &postgresRef, ch, len(podList.Items))
}

}
return Clientlist, nil
message := <-ch
if message == "" {
return clientlist, nil
}
return nil, fmt.Errorf(message)
}
func (l *XormClientList) getXormClient(kc client.Client, pb *api.PgBouncer, ctx context.Context, podName string, postgresRef *api.Databases) {
NewKubeDBClientBuilder(kc, pb).WithContext(ctx).WithDatabaseRef(postgresRef).WithPod(podName).GetPgBouncerXormClient()
func (l *XormClientList) addXormClient(kc client.Client, pb *api.PgBouncer, ctx context.Context, podName string, postgresRef *api.Databases, c chan string, pgReplica int) {
xormClient, err := NewKubeDBClientBuilder(kc, pb).WithContext(ctx).WithDatabaseRef(postgresRef).WithPod(podName).GetPgBouncerXormClient()

l.mutex.Lock()
defer l.mutex.Unlock()

if err != nil {
klog.V(5).ErrorS(err, fmt.Sprintf("failed to create xorm client for pgbouncer %v to make pool with postgres pod %s/%s", key, postgresRef.DatabaseRef.Namespace, postgresRef.DatabaseRef.Name))
if hcs.HasFailed(health.HealthCheckClientFailure, err) {
// Since the client was unable to connect the database,
// update "AcceptingConnection" to "false".
// update "Ready" to "false"
if err := c.updateConditionsForUnhealthy(ctx, db, err); err != nil {
return
}
klog.V(5).ErrorS(err, fmt.Sprintf("failed to create xorm client for pgbouncer %s/%s to make pool with postgres pod %s/%s", pb.Namespace, pb.Name, postgresRef.DatabaseRef.Namespace, postgresRef.DatabaseRef.Name))
l.list = append(l.list, nil)
if l.message == "" {
l.message = fmt.Sprintf("failed to create xorm client for: pgbouncer %s/%s to make pool with postgres pod %s/%s;", pb.Namespace, pb.Name, postgresRef.DatabaseRef.Namespace, postgresRef.DatabaseRef.Name)
} else {
l.message = fmt.Sprintf("%s pgbouncer %s/%s to make pool with postgres pod %s/%s;", l.message, pb.Namespace, pb.Name, postgresRef.DatabaseRef.Namespace, postgresRef.DatabaseRef.Name)
}
klog.Error(err)
return
} else {
l.list = append(l.list, xormClient)
}
if (pgReplica * len(pb.Spec.Databases)) <= len(l.list) {
c <- l.message
}
dbXormClient = append(dbXormClient, *XormClient)
}

0 comments on commit b13c296

Please sign in to comment.