forked from twitter/finagle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote branch 'origin/master' into native
Conflicts: finagle-core/src/main/scala/com/twitter/finagle/builder/ServerBuilder.scala
- Loading branch information
Showing
61 changed files
with
1,709 additions
and
543 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
finagle-core/src/main/scala/com/twitter/finagle/builder/ChannelFactory.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.twitter.finagle.builder | ||
|
||
import org.jboss.netty.channel.{ | ||
ChannelPipeline, ChannelFactory, ServerChannelFactory, | ||
ServerChannel} | ||
|
||
class ReferenceCountedChannelFactory(underlying: ChannelFactory) | ||
extends ChannelFactory | ||
{ | ||
private[this] var refcount = 0 | ||
|
||
def acquire() = synchronized { | ||
refcount += 1 | ||
} | ||
|
||
def newChannel(pipeline: ChannelPipeline) = underlying.newChannel(pipeline) | ||
|
||
// TODO: after releasing external resources, we can still use the | ||
// underlying factory? (ie. it'll create new threads, etc.?) | ||
def releaseExternalResources() = synchronized { | ||
refcount -= 1 | ||
if (refcount <= 0) | ||
underlying.releaseExternalResources() | ||
} | ||
} | ||
|
||
/** | ||
* A "revivable" and lazy ChannelFactory that allows us to revive | ||
* ChannelFactories after they have been released. | ||
*/ | ||
class LazyRevivableChannelFactory(make: () => ChannelFactory) | ||
extends ChannelFactory | ||
{ | ||
@volatile private[this] var underlying: ChannelFactory = null | ||
|
||
def newChannel(pipeline: ChannelPipeline) = { | ||
if (underlying eq null) synchronized { | ||
if (underlying eq null) | ||
underlying = make() | ||
} | ||
|
||
underlying.newChannel(pipeline) | ||
} | ||
|
||
def releaseExternalResources() = synchronized { | ||
if (underlying ne null) { | ||
underlying.releaseExternalResources() | ||
underlying = null | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Yikes. This monstrosity is needed to wrap server channel factories | ||
* generically. Netty only checks the actual channel factory types for | ||
* servers dynamically, so using a ChannelFactory in the server | ||
* results in a runtime error. However, the above wrappers are generic | ||
* and should be shared among the different types of factories. We are | ||
* in effect exchanging the possibility of one runtime error for | ||
* another. Another approach would be to use dynamic proxies, but this | ||
* seems a little simpler for such a simple interface. | ||
*/ | ||
class ChannelFactoryToServerChannelFactory(underlying: ChannelFactory) | ||
extends ServerChannelFactory | ||
{ | ||
def newChannel(pipeline: ChannelPipeline) = | ||
underlying.newChannel(pipeline).asInstanceOf[ServerChannel] | ||
def releaseExternalResources() = | ||
underlying.releaseExternalResources() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.