-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
73 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/maypok86/otter" | ||
) | ||
|
||
var lock = &sync.Mutex{} | ||
|
||
type single struct { | ||
context context.Context | ||
cache otter.Cache[string, string] | ||
r *Remote | ||
} | ||
|
||
var remoteCache map[*Remote]*single = make(map[*Remote]*single) | ||
|
||
func getContext(r *Remote) *single { | ||
|
||
if remoteCache[r] == nil { | ||
lock.Lock() | ||
defer lock.Unlock() | ||
if remoteCache[r] == nil { | ||
remoteCache[r] = &single{} | ||
|
||
remoteCache[r].context = context.Background() | ||
remoteCache[r].r = r | ||
|
||
cache, err := otter.MustBuilder[string, string](10_000). | ||
CollectStats(). | ||
Cost(func(key string, value string) uint32 { | ||
return 1 | ||
}).DeletionListener(func(key, value string, cause otter.DeletionCause) { | ||
log.Infof("Evicted %s %s %v ", key, value, cause) | ||
|
||
parts := strings.Split(value, ";") | ||
if len(parts) < 3 { | ||
log.Info("Should have had at least three parts") | ||
} else { | ||
msg, err := os.ReadFile("/tmp/" + key + ".mail") | ||
if err != nil { | ||
log.Errorf("cannot read file %s", key+".mail") | ||
|
||
} else { | ||
from := parts[1] | ||
to := parts[2:] | ||
SendMail(r, from, to, msg) | ||
os.Remove("/tmp/" + key + ".mail") | ||
} | ||
} | ||
}). | ||
WithTTL(time.Minute). | ||
Build() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
remoteCache[r].cache = cache | ||
} | ||
} | ||
return remoteCache[r] | ||
} |
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 |
---|---|---|
|
@@ -123,8 +123,12 @@ listen = 127.0.0.1:1025 | |
;remotes = smtp://user:pass@server:2525/[email protected]?auth=login | ||
|
||
; Multiple remotes, space delimited | ||
remotes = smtp://127.0.0.1:2525?rate=99/21m | ||
; remotes = smtp://127.0.0.1:2525?rate=1/1m | ||
;remotes = smtp://127.0.0.1:1025 starttls://user:[email protected]:587 | ||
|
||
; rate limit | ||
; remotes = smtp://127.0.0.1:2525?rate=99/21m | ||
remotes = smtp://127.0.0.1:2525?rate=1/1m smtp://127.0.0.1:2527?rate=6/1m | ||
|
||
|
||
; Pipe messages to external command | ||
;command = /usr/local/bin/script |