Skip to content

Commit 59fa469

Browse files
Merge pull request #156 from dispatch/close-client-before-configure
Implement Http.closeAndConfigure, start migration path for 0.13.x
2 parents 59ab241 + 419ceb0 commit 59fa469

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

core/src/main/scala/execution.scala

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,70 @@ case class Http(
1414
) extends HttpExecutor {
1515
import AsyncHttpClientConfig.Builder
1616

17-
/** Replaces `client` with a new instance configured using the withBuilder
18-
function. The current client config is the builder's prototype. */
19-
def configure(withBuilder: Builder => Builder) =
17+
/**
18+
* Returns a new instance replacing the underlying `client` with a new instance that is configured
19+
* using the `withBuilder` provided. The current client config is the builder's prototype.
20+
*
21+
* As of Dispatch 0.12.2, it is recommended that you use [[closeAndConfigure]] instead to prevent
22+
* the automatic resource link that using this method will cause. However, if you expect to be
23+
* able to ''continue'' using this Http instance after
24+
*
25+
* In Dispatch 0.13.x, this will be changed such that it only causes a resource link if you've
26+
* actually used the Http client, but the method is still deprecated and is one that we're
27+
* planning to remove. If you need this functionality in the long term, it is recommended that you
28+
* change your code to invoke the `.copy` method on the `Http` case class directly.
29+
*/
30+
@deprecated("This method is deprecated and will be removed in a future version of dispatch. This method is known to cause a resource leak in Dispatch 0.12.x. If you don't need to continue using the original Http instance after invoking this, you should switch to using closeAndConfigure.", "0.12.2")
31+
def configure(withBuilder: Builder => Builder): Http = {
32+
unsafeConfigure(withBuilder)
33+
}
34+
35+
// Internal, unsafe method that wraps the previous behavior of configure s othat we can invoke
36+
// it from closeAndConfigure without triggering our own deprecation warning.
37+
private[this] def unsafeConfigure(withBuilder: Builder => Builder): Http ={
2038
copy(client =
2139
new AsyncHttpClient(withBuilder(
2240
new AsyncHttpClientConfig.Builder(client.getConfig)
2341
).build)
2442
)
43+
}
44+
45+
/**
46+
* Returns a new instance replacing the underlying `client` with a new instance that is configured
47+
* using the `withBuilder` provided. The current client config is the builder's prototype. The
48+
* underlying client for this instance is closed before the new instance is created.
49+
*/
50+
def closeAndConfigure(withBuilder: Builder => Builder): Http = {
51+
this.shutdown()
52+
unsafeConfigure(withBuilder)
53+
}
2554
}
2655

2756
/** Singleton default Http executor, can be used directly or altered
2857
* with its case-class `copy` */
2958
object Http extends Http(
3059
InternalDefaults.client
31-
)
60+
) {
61+
/**
62+
* The default executor. Invoking this val will shutdown the client created by the Http singleton.
63+
*/
64+
lazy val default = {
65+
this.closeAndConfigure(builder => builder)
66+
}
67+
68+
@deprecated("Using the Http singleton directly is deprecated and will be removed in a future version of dispatch. Please switch to invoking Http.default for using a globally accessible default Http client.", "0.12.2")
69+
override def apply(req: Req)
70+
(implicit executor: ExecutionContext): Future[Response] = super.apply(req)
71+
72+
@deprecated("Using the Http singleton directly is deprecated and will be removed in a future version of dispatch. Please switch to invoking Http.default for using a globally accessible default Http client.", "0.12.2")
73+
override def apply[T](pair: (Request, AsyncHandler[T]))
74+
(implicit executor: ExecutionContext): Future[T] = super.apply(pair)
75+
76+
@deprecated("Using the Http singleton directly is deprecated and will be removed in a future version of dispatch. Please switch to invoking Http.default for using a globally accessible default Http client.", "0.12.2")
77+
override def apply[T]
78+
(request: Request, handler: AsyncHandler[T])
79+
(implicit executor: ExecutionContext): Future[T] = super.apply(request, handler)
80+
}
3281

3382
trait HttpExecutor { self =>
3483
def client: AsyncHttpClient
@@ -61,4 +110,3 @@ trait HttpExecutor { self =>
61110
client.close()
62111
}
63112
}
64-

0 commit comments

Comments
 (0)