Giter Club home page Giter Club logo

Comments (6)

colinrtwhite avatar colinrtwhite commented on September 13, 2024 1

Unfortunately, I don't think there's a way to show a message for this issue without requiring the user to install a custom socket factory. I'm going to close this issue for the moment + post the work-around as applied to Coil. If we get duplicate Github issues, we can point users back to this thread.

Here's the suggested work-around applied to Coil:

class TaggedSocketFactory(private val delegate: SocketFactory = getDefault()) : SocketFactory() {

    override fun createSocket(): Socket {
        val socket = delegate.createSocket()
        return configureSocket(socket)
    }

    override fun createSocket(host: String, port: Int): Socket {
        val socket = delegate.createSocket(host, port)
        return configureSocket(socket)
    }

    override fun createSocket(
        host: String,
        port: Int,
        localAddress: InetAddress,
        localPort: Int
    ): Socket {
        val socket = delegate.createSocket(host, port, localAddress, localPort)
        return configureSocket(socket)
    }

    override fun createSocket(host: InetAddress, port: Int): Socket {
        val socket = delegate.createSocket(host, port)
        return configureSocket(socket)
    }

    override fun createSocket(
        host: InetAddress,
        port: Int,
        localAddress: InetAddress,
        localPort: Int
    ): Socket {
        val socket = delegate.createSocket(host, port, localAddress, localPort)
        return configureSocket(socket)
    }

    private fun configureSocket(socket: Socket): Socket {
        TrafficStats.tagSocket(socket)
        return socket
    }
}

val imageLoader = ImageLoader(context) {
    val client = OkHttpClient.Builder().socketFactory(TaggedSocketFactory()).build()
    okHttpClient(client)
}

// If you use the Coil singleton...
Coil.setDefaultImageLoader(imageLoader)

from coil.

colinrtwhite avatar colinrtwhite commented on September 13, 2024

Thanks for the report. Do you have an example of other libraries working around this issue? I'm a little hesitant to work around this issue in the base Coil package. You can set a custom OkHttpClient which applies the work around like so:

Coil.setDefaultImageLoader {
    ImageLoader(this) {
         okHttpClient {
             // Apply the socket factory work-around.
         }
    }
}

from coil.

benjamin-bader avatar benjamin-bader commented on September 13, 2024

If you decide not to add a tag in the base package, it might be nice to log a warning instead if the default, untagged, client is in use and strict-mode is on. If nothing else, it will save library users from googling the error and blaming Coil.

from coil.

colinrtwhite avatar colinrtwhite commented on September 13, 2024

@benjamin-bader Strict mode will already log a warning, which references OkHttp and not Coil.

from coil.

benjamin-bader avatar benjamin-bader commented on September 13, 2024

This is true, but will lead people to think that coil is the problem. A helpful message would perhaps avert a number of spurious github issues, but if you think the existing message form strict mode is fine, then that's all that matters.

from coil.

Kolyall avatar Kolyall commented on September 13, 2024

See https://stackoverflow.com/a/36840978/2425851
By this answer we need also before TrafficStats.tagSocket(socket) call TrafficStats.setThreadStatsTag . And call untagSocket after transfer of data

TrafficStats.setThreadStatsTag(0xF00D);
TrafficStats.tagSocket(outputSocket);
// Transfer data using socket
TrafficStats.untagSocket(outputSocket);

How to make it with Coil?

from coil.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.