Skip to content

Commit

Permalink
Extract mustCloseGRPCConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
askolesov committed Jul 13, 2022
1 parent e03e37a commit bffbbc3
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions services/ledger_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,13 @@ func (ls LedgerService) QueryDIDDoc(did string) (cheqd.Did, cheqd.Metadata, bool
}

conn, err := ls.openGRPCConnection(serverAddr)
defer func(conn *grpc.ClientConn) {
if conn == nil {
return
}
err := conn.Close()
if err != nil {
log.Panic().Err(err).Msg("QueryDIDDoc: failed to close connection")
panic(err)
}
}(conn)
if err != nil {
log.Error().Err(err).Msg("QueryDIDDoc: failed connection")
return cheqd.Did{}, cheqd.Metadata{}, false, err
}

defer mustCloseGRPCConnection(conn)

log.Info().Msgf("Querying did doc: %s", did)
client := cheqd.NewQueryClient(conn)
didDocResponse, err := client.Did(context.Background(), &cheqd.QueryGetDidRequest{Id: did})
Expand All @@ -79,21 +71,13 @@ func (ls LedgerService) QueryResource(did string, resourceId string) (resource.R
}

conn, err := ls.openGRPCConnection(serverAddr)
defer func(conn *grpc.ClientConn) {
if conn == nil {
return
}
err := conn.Close()
if err != nil {
log.Panic().Err(err).Msg("QueryResource: failed to close connection")
panic(err)
}
}(conn)
if err != nil {
log.Error().Err(err).Msg("QueryResource: failed connection")
return resource.Resource{}, false, err
}

defer mustCloseGRPCConnection(conn)

log.Info().Msgf("Querying did resource: %s, %s", did, resourceId)

client := resource.NewQueryClient(conn)
Expand Down Expand Up @@ -146,6 +130,17 @@ func (ls LedgerService) openGRPCConnection(addr string) (conn *grpc.ClientConn,
return conn, nil
}

func mustCloseGRPCConnection(conn *grpc.ClientConn) {
if conn == nil {
return
}
err := conn.Close()
if err != nil {
log.Panic().Err(err).Msg("QueryDIDDoc: failed to close connection")
panic(err)
}
}

func (ls LedgerService) GetNamespaces() []string {
keys := make([]string, 0, len(ls.ledgers))
for k := range ls.ledgers {
Expand Down

0 comments on commit bffbbc3

Please sign in to comment.