-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gzip schema and metadata to avoid ballooning memory requirements #139
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
package main | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
|
@@ -96,8 +98,14 @@ func emitSchema(pkgSpec schema.PackageSpec, outDir string) error { | |
} | ||
|
||
func emitMetadata(metadata *provider.AzureApiMetadata, outDir string) error { | ||
raw, err := json.Marshal(metadata) | ||
compressedMeta := bytes.Buffer{} | ||
compressedWriter := gzip.NewWriter(&compressedMeta) | ||
err := json.NewEncoder(compressedWriter).Encode(metadata) | ||
if err != nil { | ||
return errors.Wrap(err, "marshaling metadata") | ||
} | ||
|
||
if err = compressedWriter.Close(); err != nil { | ||
return err | ||
} | ||
|
||
|
@@ -108,7 +116,7 @@ func emitMetadata(metadata *provider.AzureApiMetadata, outDir string) error { | |
|
||
err = ioutil.WriteFile("./provider/cmd/pulumi-resource-azurerm/metadata.go", []byte(fmt.Sprintf(`package main | ||
var azureApiResources = %#v | ||
`, raw)), 0600) | ||
`, compressedMeta.Bytes())), 0600) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -120,14 +128,19 @@ func emitSchemaBytes(pkgSpec schema.PackageSpec, version string) error { | |
// Ensure the spec is stamped with a version. | ||
pkgSpec.Version = version | ||
|
||
bytes, err := json.Marshal(pkgSpec) | ||
compressedSchema := bytes.Buffer{} | ||
compressedWriter := gzip.NewWriter(&compressedSchema) | ||
err := json.NewEncoder(compressedWriter).Encode(pkgSpec) | ||
if err != nil { | ||
return errors.Wrap(err, "marshaling metadata") | ||
} | ||
if err = compressedWriter.Close(); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to make a helper method here somewhere in utils? I don't have a string opinion though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can consider it as a subsequent step if this seems like a sufficient workaround. I have a feeling we might have to do some more maneuvering before a release here. |
||
return err | ||
} | ||
|
||
return ioutil.WriteFile("./provider/cmd/pulumi-resource-azurerm/schema.go", []byte(fmt.Sprintf(`package main | ||
var pulumiSchema = %#v | ||
`, bytes)), 0600) | ||
`, compressedSchema.Bytes())), 0600) | ||
} | ||
|
||
func generate(ppkg *schema.Package, language string) (map[string][]byte, error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if we return the error above? Should we close in
defer
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was initially considering a defered close but a the close actually performs a final flush so we need it to be flushed before writing the serialized content. Also, if we return early, GC should be able to reclaim the buffers created earlier.