Skip to content

Commit

Permalink
fix: Shut the channel down when the init plugin call fails #14
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Aug 21, 2023
1 parent aee0cc1 commit 7ea2504
Showing 1 changed file with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import io.pact.plugin.PactPluginGrpc
import io.pact.plugin.PactPluginGrpc.newBlockingStub
import io.pact.plugin.Plugin
import io.pact.plugins.jvm.core.Utils.fromProtoValue
import io.pact.plugins.jvm.core.Utils.handleWith
import io.pact.plugins.jvm.core.Utils.jsonToValue
import io.pact.plugins.jvm.core.Utils.mapToProtoStruct
import io.pact.plugins.jvm.core.Utils.structToJson
Expand Down Expand Up @@ -881,17 +880,27 @@ object DefaultPluginManager: KLogging(), PluginManager {
}

private fun tryInitPlugin(plugin: PactPlugin, address: String): Result<PactPlugin, Exception> {
return handleWith<PactPlugin> {
try {
val channel = ManagedChannelBuilder.forTarget(address)
.usePlaintext()
.build()
val stub = newBlockingStub(channel).withCallCredentials(BearerCredentials(plugin.serverKey))
plugin.stub = stub
plugin.channel = channel

initPlugin(plugin)
try {
initPlugin(plugin)
} catch (e: Exception) {
plugin.stub = null
plugin.channel = null
channel.shutdownNow()
throw e
}

plugin
return Result.Ok(plugin)
} catch (e: Exception) {
logger.error(e) { "Failed to initialise the plugin" }
return Result.Err(e)
}
}

Expand Down Expand Up @@ -977,11 +986,11 @@ object DefaultPluginManager: KLogging(), PluginManager {
}

private fun logLevel() = when {
logger.isTraceEnabled -> "trace"
logger.isDebugEnabled -> "debug"
logger.isInfoEnabled -> "info"
logger.isWarnEnabled -> "warn"
logger.isErrorEnabled -> "error"
logger.isTraceEnabled() -> "trace"
logger.isDebugEnabled() -> "debug"
logger.isInfoEnabled() -> "info"
logger.isWarnEnabled() -> "warn"
logger.isErrorEnabled() -> "error"
else -> ""
}

Expand Down

0 comments on commit 7ea2504

Please sign in to comment.