-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ulimit to check the open files soft limit (#66)
**What is changing**: Use the value returned from `ulimit -n` as the open files soft limit instead of the value returned from the syscall `getrlimit`. **Why this change is being made**: Since Go 1.19, during initialization, Go programs unconditionally set a high open files soft limit for themselves without modifying the system-wide defaults. This soft limit also does not apply to subprocesses run via the `exec` package. Because `wkhtmltopdf` is run as a subprocess, the soft limit set by the Go program during initialization does not give us any information about the limits that will be applied to `wkhtmltopdf`. Two approaches would be to 1. always call `setrlimit` on every run so that the soft limit will apply to subprocesses, or 2. use `ulimit` to check the soft limit that will be applied to `wkhtmltopdf`. I chose to implement approach 2. to reduce the number of syscalls. More details: - https://go.dev/src/syscall/rlimit.go - golang/go#46279 - https://stackoverflow.com/q/73640931/5403337 - https://www.perplexity.ai/search/explain-why-golang-returns-an-tho.xk6ARhOs6ZSPr4kx8A **Related issue(s)**: Fixes #63 **Follow-up changes needed**: None **Is the change completely covered by unit tests? If not, why not?**: Yes
- Loading branch information
Showing
8 changed files
with
208 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,7 @@ func TestWriteFile(t *testing.T) { | |
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"), | ||
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"), | ||
ofMock.EXPECT().Stage(), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256, nil), | ||
ofMock.EXPECT().Flush(), | ||
) | ||
}, | ||
|
@@ -81,7 +81,7 @@ func TestWriteFile(t *testing.T) { | |
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"), | ||
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"), | ||
ofMock.EXPECT().Stage(), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256, nil), | ||
ofMock.EXPECT().Flush(), | ||
) | ||
}, | ||
|
@@ -110,7 +110,7 @@ func TestWriteFile(t *testing.T) { | |
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"), | ||
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"), | ||
ofMock.EXPECT().Stage().Return(500, nil), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256, nil), | ||
osMock.EXPECT().SetOpenFilesLimit(1000), | ||
ofMock.EXPECT().Flush(), | ||
) | ||
|
@@ -140,7 +140,7 @@ func TestWriteFile(t *testing.T) { | |
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"), | ||
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"), | ||
ofMock.EXPECT().Stage(), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256, nil), | ||
ofMock.EXPECT().Flush(), | ||
) | ||
}, | ||
|
@@ -170,7 +170,7 @@ func TestWriteFile(t *testing.T) { | |
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"), | ||
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"), | ||
ofMock.EXPECT().Stage(), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256, nil), | ||
ofMock.EXPECT().Flush(), | ||
) | ||
}, | ||
|
@@ -274,6 +274,32 @@ func TestWriteFile(t *testing.T) { | |
}, | ||
wantErr: `stage chat file "messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]" for writing: this is a staging error`, | ||
}, | ||
{ | ||
msg: "get open files limit fails", | ||
pdf: true, | ||
setupMocks: func(dbMock *mock_chatdb.MockChatDB, osMock *mock_opsys.MockOS, ofMock *mock_opsys.MockOutFile) { | ||
gomock.InOrder( | ||
osMock.EXPECT().MkdirAll("messages-export/friend", os.ModePerm), | ||
osMock.EXPECT().Create("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]").Return(chatFile, nil), | ||
osMock.EXPECT().NewPDFOutFile(chatFile, gomock.Any(), false).Return(ofMock), | ||
dbMock.EXPECT().GetMessage(1, nil).Return("message1", true, nil), | ||
ofMock.EXPECT().WriteMessage("message1"), | ||
dbMock.EXPECT().GetMessage(2, nil).Return("message2", true, nil), | ||
ofMock.EXPECT().WriteMessage("message2"), | ||
osMock.EXPECT().FileExist("attachment1.heic").Return(true, nil), | ||
osMock.EXPECT().HEIC2JPG("attachment1.heic").Return("attachment1.jpeg", nil), | ||
ofMock.EXPECT().WriteAttachment("attachment1.jpeg"), | ||
osMock.EXPECT().FileExist("attachment2.jpeg").Return(true, nil), | ||
osMock.EXPECT().HEIC2JPG("attachment2.jpeg").Return("attachment2.jpeg", nil), | ||
ofMock.EXPECT().WriteAttachment("attachment2.jpeg"), | ||
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"), | ||
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"), | ||
ofMock.EXPECT().Stage().Return(500, nil), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(0, errors.New("this is a ulimit error")), | ||
) | ||
}, | ||
wantErr: `this is a ulimit error`, | ||
}, | ||
{ | ||
msg: "open files limit increase fails", | ||
pdf: true, | ||
|
@@ -295,7 +321,7 @@ func TestWriteFile(t *testing.T) { | |
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"), | ||
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"), | ||
ofMock.EXPECT().Stage().Return(500, nil), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256, nil), | ||
osMock.EXPECT().SetOpenFilesLimit(1000).Return(errors.New("this is a syscall error")), | ||
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"), | ||
) | ||
|
@@ -323,7 +349,7 @@ func TestWriteFile(t *testing.T) { | |
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"), | ||
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"), | ||
ofMock.EXPECT().Stage(), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256, nil), | ||
ofMock.EXPECT().Flush().Return(errors.New("this is a flush error")), | ||
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"), | ||
) | ||
|
@@ -349,7 +375,7 @@ func TestWriteFile(t *testing.T) { | |
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"), | ||
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"), | ||
ofMock.EXPECT().Stage(), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256, nil), | ||
ofMock.EXPECT().Flush(), | ||
) | ||
}, | ||
|
@@ -456,7 +482,7 @@ func TestWriteFile(t *testing.T) { | |
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"), | ||
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"), | ||
ofMock.EXPECT().Stage(), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256, nil), | ||
ofMock.EXPECT().Flush(), | ||
) | ||
}, | ||
|
@@ -501,7 +527,7 @@ func TestWriteFile(t *testing.T) { | |
ofMock.EXPECT().Name().Return("messages-export/friend/iMessage;-;[email protected];;;iMessage;-;[email protected]"), | ||
ofMock.EXPECT().ReferenceAttachment("att3transfer.png"), | ||
ofMock.EXPECT().Stage(), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256, nil), | ||
ofMock.EXPECT().Flush(), | ||
) | ||
}, | ||
|
@@ -575,7 +601,7 @@ func TestWriteFile(t *testing.T) { | |
osMock.EXPECT().Create("friend/iMessage;-;heresareallylongemailaddress.heresareallylongemailaddress.heresareallylongemailaddress.heresareallylongemailaddress.heresareallylongemailaddress.heresareallylongemailaddress.heresareallylongemailaddress.heresareallylongemailaddress@gmail.c.txt").Return(chatFile, nil), | ||
osMock.EXPECT().NewTxtOutFile(chatFile).Return(ofMock), | ||
ofMock.EXPECT().Stage(), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256, nil), | ||
ofMock.EXPECT().Flush(), | ||
) | ||
|
||
|
@@ -617,7 +643,7 @@ func TestWriteFile(t *testing.T) { | |
mockCalls = append( | ||
mockCalls, | ||
ofMock1.EXPECT().Stage(), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256, nil), | ||
ofMock1.EXPECT().Flush(), | ||
osMock.EXPECT().Create("messages-export/friend/iMessage;-;[email protected]").Return(chatFile2, nil), | ||
osMock.EXPECT().NewPDFOutFile(chatFile2, gomock.Any(), false).Return(ofMock2), | ||
|
@@ -634,7 +660,7 @@ func TestWriteFile(t *testing.T) { | |
mockCalls = append( | ||
mockCalls, | ||
ofMock2.EXPECT().Stage(), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256), | ||
osMock.EXPECT().GetOpenFilesLimit().Return(256, nil), | ||
ofMock2.EXPECT().Flush(), | ||
) | ||
gomock.InOrder(mockCalls...) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.