1, in orient function language, function is basic unit(variable)
2, function can be passed as parameter, you can implement aspect oriented programming
3, seq design(linked list), recursive
4, lazy design
5, cache with timeout and wait timeout, when timeout, cache should be reloaded to sync several servers' local cache
(defonce memoized-all-sessions
(atom (lang/memoize-expire
get-all-sessions-map
(* 10 60 1000)
(* 10 60 1000)))) ;;; (@memoized-all-sessions) (get-all-sessions-map)
(defn reset-memoized-all-sessions
[]
(reset! memoized-all-sessions
(lang/memoize-expire
get-all-sessions-map
(* 10 60 1000)
(* 10 60 1000))))
6, thread local varible
7, proxy, stream statistics
(defn counting-stream
"An inpuStream that counts the number of bytes that go through it.
Deref the stream @x to get the current count value. "
[^java.io.InputStream underlying-inputstream]
(let[count (java.util.concurrent.atomic.AtomicLong.)]
(proxy [java.io.InputStream clojure.lang.IDeref] []
(read
([]
(let [x (.read underlying-inputstream)]
(if (not= -1 x) (.incrementAndGet count))
x))
([b]
(let [x (.read underlying-inputstream b)]
(if (not= -1 x) (.addAndGet count x))
x))
([b off len]
(let [x (.read underlying-inputstream b off len)]
(if (not= -1 x) (.addAndGet count x))
x)))
(available [] (.available underlying-inputstream))
(close [] #_ (println "counting:closed") (.close underlying-inputstream))
(deref[] (.get count)))))
8, timeout design
(defn seq-with-timeout
"INTERNAL FUNCTION.
Returns a lazy sequence that wraps the existing lazy sequence. However
will also excute function <ARG2> when the sequence has been fully consumed.
If *100* items have been retrieved for <ARG3> MS, then the seq is closed and <ARG2> is called.
Attempting to read from a closed seq will throw an exception."
([sq fn-action]
(seq-with-timeout sq fn-action 180000))
([sq fn-action timeout]
(let [ finalize (fn[]
(if
(try
(java.lang.Thread/sleep timeout)
false
(catch InterruptedException e true))
(recur)
(do
(println "lang:seq-with-timeout:timeout:" timeout ":" (java.util.Date.) ":fn:" fn-action)
;(println "lang, no action, timeout: " timeout +"f" fn-action)
(fn-action)
)))
t (new java.lang.Thread finalize)
]
(.start t)
(seq-with-timeout sq fn-action timeout t 0)))
([sq fn-action timeout ^java.lang.Thread thread n]
(let [head (first sq)
tail (rest sq)
]
(when (= (rem n 100) 0)
(.interrupt thread))
(cond
(and (not (nil? head)) (.isAlive thread))
(cons head (lazy-seq (seq-with-timeout tail fn-action timeout thread (+ n 1))))
(not (.isAlive thread))
(throw (Exception. "Cannot read data from already timed-out Seq."))
:otherwise-reached-end-of-seq
(do
(try
[]
(finally
(.stop thread)
(println "seq-with-timeout:end-of-seq" (java.util.Date.) )
(fn-action))))))))
9, interface design
10, pipe
11, map
1, separation between function and business implementation
2, layout of util class
3, log collection and analyze
4, thread sync
5, when update data, db first, then cache
5, authentication and entitlement
6, user behavior and server status information(server should keep sth in cache)
7, access control(blacklist and simultaneous control)
8, token system for access server
9, composable rest api design
10, long connection
11, async request anf sync request
12, stream transfer data
13, reflection
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/125193.html
標籤:網絡通信
上一篇:服務器網址不同電腦無法鏈接問題
