Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to cleanly pin a network #8286

Open
yschimke opened this issue Mar 15, 2024 · 4 comments
Open

How to cleanly pin a network #8286

yschimke opened this issue Mar 15, 2024 · 4 comments
Labels
enhancement Feature not a bug

Comments

@yschimke
Copy link
Collaborator

WorkManager provides a required network for scheduled jobs with network constraints

https://developer.android.com/reference/androidx/work/WorkerParameters#getNetwork()

Using this specific network is possible with a custom Dns and SocketFactory. However the naive implementation would probably result in defeating connection pooling in some cases.

Is there a supported pattern for this, if Dns and SocketFactory both support equality checks, would pooling be correctly retained?

cc @swankjesse

@yschimke yschimke added the enhancement Feature not a bug label Mar 15, 2024
@swankjesse
Copy link
Member

How about this:

/**
 * Returns a DNS that uses this network’s `getAllByName` function.
 * 
 * Different objects returned from this will be equal if their networks are equal.
 */
val Network.dns: Dns

And maybe also this:

/**
 * Returns a client that uses [network] for its socket factory and DNS. The
 * client will only be able to make calls to hosts that are reachable on [network].
 */
fun OkHttpClient.Builder.network(network: Network)

Plus @JvmName stuff to make the Java APIs reasonable.

Do we need to hide these in the okhttp-android module? It’d be nice to do it in the main okhttp module.

@yschimke
Copy link
Collaborator Author

@swankjesse for discussion in #8376, I think I want to move away from recreating the OkHttpClient instance, and instead configure Dns, SocketFactory and Address matcher? that listen for a Request tag (NetworkPin) that callers or interceptors set.

@appdev
Copy link

appdev commented May 8, 2024

@swankjesse for discussion in #8376, I think I want to move away from recreating the OkHttpClient instance, and instead configure Dns, SocketFactory and Address matcher? that listen for a Request tag (NetworkPin) that callers or interceptors set. 对于 #8376 中的讨论,我想我不想重新创建 OkHttpClient 实例,而是配置 Dns、SocketFactory 和地址匹配器?侦听调用者或拦截器设置的请求标记 (NetworkPin)。

Hello, have you solved this problem?

@yschimke
Copy link
Collaborator Author

yschimke commented May 8, 2024

No, the approach requires a new client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature not a bug
Projects
None yet
Development

No branches or pull requests

3 participants