OkHttp3套接字失败:EMFILE(打开的文件过多)

时间:2016-06-13 17:05:20

标签: android retrofit2 okhttp3 okio

我正在使用Retrofit 2.0.0。 OkHttp 3.1.2。 我的一些用户遇到以下异常:

  

java.net.SocketException:套接字失败:EMFILE(打开的文件太多)       在libcore.io.IoBridge.socket(IoBridge.java:619)〜[na:0.0]       在java.net.PlainSocketImpl.create(PlainSocketImpl.java:198)〜[na:0.0]       在java.net.Socket.checkOpenAndCreate(Socket.java:689)〜[na:0.0]       在java.net.Socket.setSoTimeout(Socket.java:543)〜[na:0.0]       在com.android.org.conscrypt.OpenSSLSocketImpl.setSoTimeout(OpenSSLSocketImpl.java:1140)〜[na:0.0]       在com.android.org.conscrypt.OpenSSLSocketImplWrapper.setSoTimeout(OpenSSLSocketImplWrapper.java:111)〜[na:0.0]       在okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:155)〜[na:0.0]       在okhttp3.internal.io.RealConnection.connect(RealConnection.java:111)〜[na:0.0]       在okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:188)〜[na:0.0]       在okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:127)〜[na:0.0]       在okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)〜[na:0.0]       在okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:289)〜[na:0.0]       在okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)〜[na:0.0]       在okhttp3.RealCall.getResponse(RealCall.java:240)〜[na:0.0]       在okhttp3.RealCall $ ApplicationInterceptorChain.proceed(RealCall.java:198)~ [na:0.0]

当我看到crashlytics时,我看到很多正在运行的线程:

  

at com.android.org.conscrypt.NativeCrypto.SSL_read(NativeCrypto.java)          在com.android.org.conscrypt.OpenSSLSocketImpl $ SSLInputStream.read(OpenSSLSocketImpl.java:811)          在okio.Okio $ 2.read(Okio.java:139)          在okio.AsyncTimeout $ 2.read(AsyncTimeout.java:211)          at okio.RealBufferedSource.request(RealBufferedSource.java:71)          at okio.RealBufferedSource.require(RealBufferedSource.java:64)          在okio.RealBufferedSource.readInt(RealBufferedSource.java:232)          在okhttp3.internal.framed.Spdy3 $ Reader.nextFrame(Spdy3.java:129)          在okhttp3.internal.framed.FramedConnection $ Reader.execute(FramedConnection.java:600)          在okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)          在java.lang.Thread.run(Thread.java:818)

我在我的应用程序中只启动了一次Okhttp:

OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
    clientBuilder.connectTimeout(3 * 1000, TimeUnit.MILLISECONDS);
    clientBuilder.readTimeout(25 * 1000, TimeUnit.MILLISECONDS);

    File cacheDir = new File(mWeakContext.get().getFilesDir().getAbsolutePath(), "http-cache");
    Cache cache = new Cache(cacheDir, 16 * 1024 * 1024); // 16MB
    clientBuilder.cache(cache);

    // set proxy
    Properties properties = System.getProperties();
    if (properties.containsKey("https.proxyHost")) {

        String proxyHost = properties.getProperty("https.proxyHost");
        int proxyPort = Integer.parseInt(properties.getProperty("https.proxyPort"));

        clientBuilder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
                proxyHost,
                proxyPort)
        ));
    }
    clientBuilder.addInterceptor(mHeadersInterceptor);
    clientBuilder.addInterceptor(mSignInterceptor);
    clientBuilder.addInterceptor(mLoggingInterceptor);
    mClient = clientBuilder.build();

我在这里很无能,因为它不会发生很多事情,我无法在我的设备上重现它。

0 个答案:

没有答案