Skip to content

Commit

Permalink
Better performance: F# 6 task
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorium committed Jun 17, 2022
1 parent a5f36bb commit 3999fe6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 1.0.25 - June 17 2022
* Reference component update
* Better performance via F# 6.0 tasks

### 1.0.24 - March 22 2022
* Fix for avoiding double compression
Expand Down
8 changes: 4 additions & 4 deletions src/Owin.Compression.Standard/AssemblyInfo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ open System.Reflection
[<assembly: AssemblyTitleAttribute("Owin.Compression.Standard")>]
[<assembly: AssemblyProductAttribute("Owin.Compression")>]
[<assembly: AssemblyDescriptionAttribute("Compression (Deflate / GZip) module for Microsoft OWIN Selfhost filesystem pipeline.")>]
[<assembly: AssemblyVersionAttribute("1.0.24")>]
[<assembly: AssemblyFileVersionAttribute("1.0.24")>]
[<assembly: AssemblyVersionAttribute("1.0.25")>]
[<assembly: AssemblyFileVersionAttribute("1.0.25")>]
do ()

module internal AssemblyVersionInformation =
let [<Literal>] AssemblyTitle = "Owin.Compression.Standard"
let [<Literal>] AssemblyProduct = "Owin.Compression"
let [<Literal>] AssemblyDescription = "Compression (Deflate / GZip) module for Microsoft OWIN Selfhost filesystem pipeline."
let [<Literal>] AssemblyVersion = "1.0.24"
let [<Literal>] AssemblyFileVersion = "1.0.24"
let [<Literal>] AssemblyVersion = "1.0.25"
let [<Literal>] AssemblyFileVersion = "1.0.25"
8 changes: 4 additions & 4 deletions src/Owin.Compression/AssemblyInfo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ open System.Reflection
[<assembly: AssemblyTitleAttribute("Owin.Compression")>]
[<assembly: AssemblyProductAttribute("Owin.Compression")>]
[<assembly: AssemblyDescriptionAttribute("Compression (Deflate / GZip) module for Microsoft OWIN Selfhost filesystem pipeline.")>]
[<assembly: AssemblyVersionAttribute("1.0.24")>]
[<assembly: AssemblyFileVersionAttribute("1.0.24")>]
[<assembly: AssemblyVersionAttribute("1.0.25")>]
[<assembly: AssemblyFileVersionAttribute("1.0.25")>]
do ()

module internal AssemblyVersionInformation =
let [<Literal>] AssemblyTitle = "Owin.Compression"
let [<Literal>] AssemblyProduct = "Owin.Compression"
let [<Literal>] AssemblyDescription = "Compression (Deflate / GZip) module for Microsoft OWIN Selfhost filesystem pipeline."
let [<Literal>] AssemblyVersion = "1.0.24"
let [<Literal>] AssemblyFileVersion = "1.0.24"
let [<Literal>] AssemblyVersion = "1.0.25"
let [<Literal>] AssemblyFileVersion = "1.0.25"
44 changes: 21 additions & 23 deletions src/Owin.Compression/CompressionModule.fs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ module OwinCompression =
let DefaultCompressionSettingsWithPathAndCache(path,cachetime) =
{DefaultCompressionSettings with ServerPath = path; CacheExpireTime = Some (cachetime) }

let internal awaitTask = Async.AwaitIAsyncResult >> Async.Ignore
let private defaultBufferSize = 81920

let internal compress (context:IOwinContext) (settings:CompressionSettings) (mode:ResponseMode) =
Expand Down Expand Up @@ -132,9 +131,9 @@ module OwinCompression =
| false when settings.AllowUnknonwnFiletypes -> ()
| _ -> failwith "Invalid resource type"

async {
task {
use strm = File.OpenText unpacked
let! txt = strm.ReadToEndAsync() |> Async.AwaitTask
let! txt = strm.ReadToEndAsync()
let bytes = txt |> System.Text.Encoding.UTF8.GetBytes
match FileInfo(unpacked).Length < settings.MinimumSizeToCompress with
| true ->
Expand All @@ -159,13 +158,13 @@ module OwinCompression =

match mode with
| File ->
async {
task {
if cancellationToken.IsCancellationRequested then ()
use output = new MemoryStream()
let! awaited = getFile()
let shouldskip, bytes = awaited
if(shouldskip) then
return! context.Response.WriteAsync(bytes, cancellationToken) |> awaitTask
return! context.Response.WriteAsync(bytes, cancellationToken)
else

if not(context.Response.Headers.ContainsKey "Vary") then
Expand All @@ -178,15 +177,15 @@ module OwinCompression =
| GZip ->
context.Response.Headers.Add("Content-Encoding", [|"gzip"|])
new GZipStream(output, CompressionMode.Compress) :> Stream
let! t1 = zipped.WriteAsync(bytes, 0, bytes.Length, cancellationToken) |> awaitTask
let! t1 = zipped.WriteAsync(bytes, 0, bytes.Length, cancellationToken)
t1 |> ignore
zipped.Close()
let op = output.ToArray()
return! context.Response.WriteAsync(op, cancellationToken) |> awaitTask
} |> Async.StartAsTask :> Task
return! context.Response.WriteAsync(op, cancellationToken)
} :> Task

| ContextResponseBody(next) ->
async {
task {
let compressableExtension() =
match context.Request.Path.ToString() with
| null -> true
Expand All @@ -196,21 +195,21 @@ module OwinCompression =
| _ -> false

if cancellationToken.IsCancellationRequested then
do! next.Invoke() |> awaitTask
do! next.Invoke()
()
else

use stream = context.Response.Body
use buffer = new MemoryStream()

let! usecompress =
async {
task {
if compressableExtension() || not(context.Request.Path.ToString().Contains("/signalr/")) then
context.Response.Body <- buffer // stream
do! next.Invoke() |> awaitTask
do! next.Invoke()
return true
else
do! next.Invoke() |> awaitTask
do! next.Invoke()
if compressableExtension() then // non-stream, but Invoke can change "/" -> "index.html"
context.Response.Body <- buffer
return true
Expand Down Expand Up @@ -242,7 +241,7 @@ module OwinCompression =
if context.Response.Body.CanSeek then
context.Response.Body.Seek(0L, SeekOrigin.Begin) |> ignore

do! context.Response.Body.CopyToAsync(stream, defaultBufferSize, cancellationToken) |> awaitTask
do! context.Response.Body.CopyToAsync(stream, defaultBufferSize, cancellationToken)
| false ->

let canStream = String.Equals(context.Request.Protocol, "HTTP/1.1", StringComparison.Ordinal)
Expand All @@ -260,11 +259,11 @@ module OwinCompression =
| GZip ->
context.Response.Headers.Add("Content-Encoding", [|"gzip"|])
new GZipStream(output, CompressionMode.Compress) :> Stream
//let! t1 = zipped.WriteAsync(bytes, 0, bytes.Length, cancellationToken) |> awaitTask
//let! t1 = zipped.WriteAsync(bytes, 0, bytes.Length, cancellationToken)
if context.Response.Body.CanSeek then
context.Response.Body.Seek(0L, SeekOrigin.Begin) |> ignore

do! context.Response.Body.CopyToAsync(zipped, defaultBufferSize, cancellationToken) |> awaitTask
do! context.Response.Body.CopyToAsync(zipped, defaultBufferSize, cancellationToken)

zipped.Close()
let op = output.ToArray()
Expand All @@ -283,19 +282,19 @@ module OwinCompression =
if tmpOutput.CanSeek then
tmpOutput.Seek(0L, SeekOrigin.Begin) |> ignore

do! tmpOutput.CopyToAsync(stream, defaultBufferSize, cancellationToken) |> awaitTask
do! tmpOutput.CopyToAsync(stream, defaultBufferSize, cancellationToken)
return ()
} |> Async.StartAsTask :> Task
} :> Task

let encodeTask() =
let WriteAsyncContext() =
match mode with
| File ->
async{
task {
let! comp, r = getFile()
if comp then return context.Response.WriteAsync(r, cancellationToken) |> Async.AwaitTask
else return Async.Sleep 50
} |> Async.StartAsTask :> Task
if comp then return! context.Response.WriteAsync(r, cancellationToken)
else return! Task.Delay 50
} :> Task
| ContextResponseBody(next) ->
next.Invoke()
if String.IsNullOrEmpty(encodings) then WriteAsyncContext()
Expand Down Expand Up @@ -327,7 +326,6 @@ type CompressionExtensions =
app.Map(path, fun ap ->
ap.Run(fun context ->
(compress context settings ResponseMode.File)()
|> awaitTask |> Async.StartAsTask :> _
))

/// You can set a path that is url that will be captured.
Expand Down

0 comments on commit 3999fe6

Please sign in to comment.