[swift-server-dev] [HTTP] Value vs Reference Types

Helge Heß me at helgehess.eu
Mon Dec 12 08:14:18 CST 2016


On 12 Dec 2016, at 13:08, Chris Bailey via swift-server-dev <swift-server-dev at swift.org> wrote:
> Its probably worth also adding some information on the approach taken by Foundation today for URLRequest/Response.
> 
> For outbound requests, Foundation provides both NSURLRequest (class) and URLRequest (struct).

I thought that both are available on Linux might be more of an oversight aka a bug (not removing NSURLRequest when URLRequest was introduced).

> This is presumably because you shouldn't need to modify the stream for an outbound request

Well, somehow I need to send my data aka write to the stream? This obviously doesn’t matter for a HEAD or GET request, or a small JSON payload, but if you are uploading a photo or video, you somehow need to stream the data.

Also note that in many frameworks the stream itself can change (both in- and outbound). The common example is deflate style filters or middleware.
Now in the context of the discussion that middleware could of course also create a new request with the respective encoding headers removed and the new wrapped stream being attached.

> For responses from outbound requests, only NSURLResponse (class) is provided as the body is read from the stream.
> 
> This effectively give us a model where:
>         outbound        can be a struct (you shouldn't need to touch the stream)
>         inbound        class (reading from stream)
> For the server case, we need the mirror opposite of this - with incoming requests being a class and the outgoing response potentially being a struct.

Is this confusing immutability with reference types here? A class’ ivars can be all r/o and a struct can be passed around by reference (inout) and have r/w fields. Maybe I’m just stating the obvious :-)

I think we all agree that while a request or response is passed through the server, separate modules (middleware or other approaches) are going to change the data (changing headers, adding cookies, decoding payloads, etc etc)?
Now this can be done by creating a new, derived object representing the HTTP message, or it can be done by modifying the object which is passed in (either struct or class).
This goes both directions, client and server.


In my opinion an `HTTPMessage` (regardless which) is w/o question a reference type. It is a living object which progresses as the HTTP stream is being processed. There are events associated with it (like shutdown, ‘EOF' etc) and the meta data associated with it can change, say headers (chunked trailing headers, or via middleware, e.g. a cookie based proxy routing one). Even the method can change (e.g. X-HTTP-Method-Override …).
An interesting question is whether that `HTTPMessage` object should be provided by the Swift server lib or whether that is part of the framework on top. If it is being wrapped anyways, it may make sense to just provide lower level primitives.

There could be immutable structs called `HTTPRequestHead` and `HTTPResponseHead`, but I would definitely call them like that. Not `HTTPRequest`. Putting (user visible) reference types into a struct seems like a strong anti pattern to me (an immutable thing annotating a thing that can change).


> This leads to two questions:
> 1. Do we think the Foundation model is correct?

To me the Foundation model is more like a framework on top. If you are developing an iOS app you just fire off a request like ‘download that resource and tell me when you have it fully cached for me, deal with all else’.
But that is not what you (usually) want on the server where (per user) memory and disk is scarce.

(Presumably this is where the "don’t touch the stream” is coming from. Its details are handled by the ‘client framework’ aka Foundation for you. You don’t usually want that in a server side HTTP client.)

IMO making `URLRequest` a struct is incorrect. As outlined above I think this should either be called `URLRequestHead` and have the stream removed, or it should be a reference type (if only because the headers can change while a chunked HTTP stream is read).

Answer: No. But this should be fixed in Foundation too :-)


> 2. Do we think it would be confusing to switch been classes and structs when working with inbound vs. outbound requests, or do we think it would enforce correct behaviour?

To me there is really no difference between in- and outbound HTTP requests. Both are streams of arbitrary size with some meta data attached.

Say you write a BlueMix document translation service. The client would `POST` a jigabytes big Word document in language A to your service, which would decode and translate it on the fly and stream out jigabytes of translated Word in language B.

Answer: Yes that would be confusing. They are both the same kind of streams.

hh


> 
> Chris
> 
> 
> 
> 
> From:        Ben Cohen via swift-server-dev <swift-server-dev at swift.org>
> To:        Dan Appel <dan.appel00 at gmail.com>
> Cc:        "swift-server-dev at swift.org" <swift-server-dev at swift.org>
> Date:        08/12/2016 02:04
> Subject:        Re: [swift-server-dev] [HTTP] Value vs Reference Types
> Sent by:        swift-server-dev-bounces at swift.org
> 
> 
> 
> Hi Dan,
> 
> Thanks for spelling out these questions, I think they are a great starting point for a discussion. A few comments inline.
> 
> On Nov 23, 2016, at 1:19 PM, Dan Appel via swift-server-dev <swift-server-dev at swift.org> wrote:
> 
> 
> My own responses:
> 
> 
> >1. Do we want to use concrete types or protocols for Request/Response?
> 
> 
> When working on Open Swift, this was a hot topic since we believed that it would be unsafe to have a protocol that would allow both value and reference types.
> 
> Bear in mind that a protocol is more than just the methods and types it declares – it is also its documentation. For example, a number of protocols in the standard library state things like complexity requirements in their documenting comments. The language has no way of enforcing these, but your type does not truly “conform” to the protocol unless you adhere to them. Value semantics are similar – you can document that it is invalid to implement a protocol with reference semantics. So I don’t think this is a blocker to wanting to use protocols.
> 
> We arrived upon the `{Request|Response}Representable` pattern which worked but was a bit of a mess. Because of this, I would prefer concrete Request/Response types.
> 
> 
> You could think of there as being 3 purposes to using protocols here, roughly in order of importance:
> 	• being able to write generic code
> 	• allowing different frameworks to interoperate
> 	• documenting what you need to implement
> 
> The first one is the only reason why a protocol must be included in a library, and the key question to ask when considering defining a protocol like this is “What common algorithms do you want to write across multiple different conforming types in your program”?  (such as generic functions, including protocol extensions, or functions that take an existential if the protocol has no associated types).
> 
> This is distinct from wanting to be able to be able to choose from different library implementations of Request. You might want to choose between the Acme Inc Web Framework’s Request type, or some Swift-Server “official" Request type, but you never need to use both at once and write code spanning them. You just want to make sure that one can serve as a source-compatible “drop-in” replacement for the other in your code. This doesn’t mean you can’t write your own extensions – but you would extend the concrete Request not a RequestRepresentable protocol.
> 
> Next, it’s possible that there might be a collection of 3rd-party frameworks out there that don’t define Request, but want to be able to write methods that take or extend multiple possible Request implementations. This seems a bit unlikely in the case of these types, more likely in other cases like networking, so it’s kind of a what-if scenario where there are both multiple popular implementations of Request, and various frameworks that want to interact with them. Anyone can add a conformance to anything, so those frameworks can define a protocol of their own with a subset of the functionality they need, and then just extend the popular implementations to conform to it. If this gets really common, at that point it might be worth creating an official protocol for everyone to share – but this can be done later, doesn’t have to be done up-front.
> 
> Finally, if you do expect multiple implementations and want people to be able to swap them in and out when they choose, the protocol can serve to document what methods and properties you are expected to implement to be “source compatible". This can be done in documentation instead, the benefit of the protocol being it helps the library developer ensure they’ve got all the signatures right etc. But this isn’t something you expose to users, it’s something on the side to help implementors.
> 
> Based on all the above, it seems like there isn’t a pressing need for a protocol for these types right now and initial designs should focus on a concrete implementation until one emerges, if only to avoid premature generalization. Useful protocols tend to be discovered, rather than designed, through a desire to share common operations on different concrete types.
> 
> >2. If we use concrete types, do we want value or reference semantics?
> 
> 
> What I think makes this easier is that the "big four" have each taken a slightly different approach that can be used as a reference.
> Zewo - struct, value semantics
> Vapor - closed class, reference semantics
> Kitura - closed class + has-a pattern, reference semantics
> Perfect - class protocol, reference semantics
> 
> Zewo is the outlier here, but I would like to note as a contributor to Zewo that we have not ran into situations where value semantics create an impassable roadblock.
> 
> To me, it makes sense to pass them around as values since they don't have any logic of their own. Requests/Responses can't send themselves, they can only read and modified. It also gives me as a user more safety to pass them around since I know that they won't be modified implicitly.
> 
> Take the following pseudo-code as an example:
> 
> HTTPServer.onRequest { request in
>     print(request.sourceIp)
>     HTTPClient.send(request)
>     print(request.sourceIp)
> 
> }
> 
> With reference semantics, there is no guarantee that sourceIp will be the same before and after sending off the request. After all, it could make sense for the HTTPClient to modify the sourceIp before sending off the request. This of course a contrived example, but the point stands.
> 
> 
> Not contrived at all, this is a perfect illustration of why reference semantics make it harder to reason about your code and identify the cause of bugs.
> 
> Anyway, I think it would be great if we could have people talk about their own experiences.
> 
> 
> >3. When is it more convenient to have reference semantics?
> 
> 
> 
> Convenience is a double-edged thing. Pointers with possibly-null values, or integer indexes into Unicode strings, are often considered convenient. But that convenience comes with a hidden cost to correctness – unexpected nulls, accidentally indexing into the middle of a grapheme cluster etc. When making a proper effort to handle these things correctly, code quickly becomes less convenient, and less readable, compared to the alternatives.
> 
> It’s generally the style in Swift that correctness shouldn't be sacrificed for convenience, but when things work out well, convenience and ergonomics can be mutually reinforcing – the code is nice to use correctly, awkward to use incorrectly. For example, optionals that force you to handle nil help with correctness, but they have sugar like ?? or optional chaining to handle common patterns clearly and idiomatically, ! as a shorthand for asserting something is non-nil etc.
> 
> In the middleware chain architecture that we decided on in Zewo (the other ones have something similar), it can be convenient to modify requests in the responder and have that reflect in the middleware. I think this problem is best solved with `inout` parameters rather than reference types, but that is my personal opinion.
> 
> 
> FWIW, this design view is also strongly held by those of us working on the Swift Standard Library. I also brought this up with several members of the Core Team and they also strongly felt that inout and value types was the general approach we should take with such types in Swift. The consensus there was that reference types really should be mostly used when identity of the value is important.
> 
> 
> >4. Are there problems that can't be solved with value semantics?
> 
> 
> I haven't found any, but I'm sure others can bring something interesting to the table.
> 
> 
> 
> Shared mutable state is one. With an unavoidably-shared resource, like a network connection or a handle to a window on a screen, reference semantics are often what you want.
> 
> 
> On Wed, Nov 23, 2016 at 1:07 PM Dan Appel <dan.appel00 at gmail.com> wrote:
> Hello everyone!
> 
> I was unable to make the kick-off meeting for the HTTP sub-team, but I looked over the meeting notes and found some topics that I think could use some more on-the-record discussion.
> 
> A few questions that I wanted to raise:
> 
> 1. Do we want to use concrete types or protocols for Request/Response?
> 2. If we use concrete types, do we want value or reference semantics?
> 3. When is it more convenient to have reference semantics?
> 4. Are there problems that can't be solved with value semantics?
> 
> I would like to avoid bike-shedding, and I think this can be done by providing real examples rather than just talking about the pros and cons.
> --
> Dan Appel
> --
> Dan Appel
> _______________________________________________
> swift-server-dev mailing list
> swift-server-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-server-dev
> _______________________________________________
> swift-server-dev mailing list
> swift-server-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-server-dev
> 
> 
> 
> Unless stated otherwise above:
> IBM United Kingdom Limited - Registered in England and Wales with number 741598.
> Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU
> _______________________________________________
> swift-server-dev mailing list
> swift-server-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-server-dev

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 842 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://lists.swift.org/pipermail/swift-server-dev/attachments/20161212/0bb8aff7/attachment.sig>


More information about the swift-server-dev mailing list