Global state and local address in OkHttp -


we using apache httpclient in our custom load test framework. have implement support websocket testing, , since httpclient not support websockets, looking alternatives. so, i've been playing okhttp , async http client. in general, use both replacement, either 1 gives me few challenges need tackle. here's ones i'm facing okhttp:

  • okhttp uses global static state in com.squareup.okhttp.internal.internal seems little bit scary me. in our framework, launch lots of threads in turn execute call request flows. each thread own httpclient instance. seems, okhttp cannot have multiple independent okhttpclient instances in 1 vm because interfere due static stuff. seem able work around problem using 1 okhttpclient instance cookiehandler delegates thread-scoped cookiehandlers. might have same connectionpools. on right track here?
  • i need able set local address. have client machines multiple nics have evenly used. that's no problem apache httpclient , asynchttpclient. how can done okhttp?

update/solution:

digging through code again after jesse posted comments, realized internal singleton not keep state. guess irritated fact found singleton in codebase though not harm.

i able set local address using custom socket factory jesse suggested:

public class localaddresssocketfactory extends socketfactory {     private static final string error_msg = "on unconnected sockets supported";      private final socketfactory delegate;     private final provider<inetaddress> localaddressprovider;      public localaddresssocketfactory(final socketfactory delegate,              final provider<inetaddress> localaddressprovider) {         this.delegate = delegate;         this.localaddressprovider = localaddressprovider;     }      @override     public socket createsocket() throws ioexception {         socket socket = delegate.createsocket();         socket.bind(new inetsocketaddress(localaddressprovider.get(), 0));         return socket;     }      @override     public socket createsocket(final string remoteaddress, final int remoteport)              throws ioexception, unknownhostexception {         throw new unsupportedoperationexception(error_msg);     }      @override     public socket createsocket(final inetaddress remoteaddress, final int remoteport)              throws ioexception {         throw new unsupportedoperationexception(error_msg);     }      @override     public socket createsocket(final string remoteaddress, final int remoteport,              final inetaddress localaddress, final int localport)             throws ioexception, unknownhostexception {         throw new unsupportedoperationexception(error_msg);     }      @override     public socket createsocket(final inetaddress remoteaddress, final int remoteport,              final inetaddress localaddress, final int localport)             throws ioexception {         throw new unsupportedoperationexception(error_msg);     } } 

this class relies on fact okhttp creates unconnected sockets. i'd appreciate if documented. okhttpclient allows custom socketfactory set, have find out how used. submitted pull request enhancing javadocs: https://github.com/square/okhttp/pull/1626.


Comments